站長資訊網(wǎng)
        最全最豐富的資訊網(wǎng)站

        go語言怎么比較字符串

        比較方法:1、直接使用“==”運(yùn)算符比較,語法“str1 == str2”,該方法區(qū)分大小寫。2、利用strings包的Compare()函數(shù)比較,語法“strings.Compare(a,b)”;返回值為int類型,0表示兩數(shù)相等,1表示a大于b,“-1”表示a小于b。3、利用strings包的EqualFold()比較,語法“strings.EqualFold(a,b)”。

        go語言怎么比較字符串

        本教程操作環(huán)境:windows7系統(tǒng)、GO 1.18版本、Dell G3電腦。

        Go語言比較字符串方式

        在 go 語言中字符串比較的方式有如下三種:

        • == 直接比較,區(qū)分大小寫
        • strings.Compare(a,b) 該函數(shù)返回值為 int, 0 表示兩數(shù)相等,1 表示 a>b, -1 表示 a<b。區(qū)分大小寫
        • strings.EqualFold(a,b) 直接返回是否相等,不區(qū)分大小寫。

        示例如下:// 1-使用等號比較——區(qū)分大消息

        func Equal(s1, s2 string) bool { 	return s1 == s2 }  // 2-使用 compare 比較——區(qū)分大小寫 func Compare(s1, s2 string) bool { 	return strings.Compare(s1, s2) == 0 // }   //3-EqualFold 比較——不區(qū)分大小寫. case-fold 即大小寫同一處理 func EqualFold(s1, s2 string) bool { 	return strings.EqualFold(s1, s2) }  // 使用等號比較——忽略大小寫 func Equal2(s1, s2 string) bool { 	return strings.ToLower(s1) == strings.ToLower(s2) }  // 使用 compare 比較——不區(qū)分大小寫 func Compare2(s1, s2 string) bool { 	return strings.Compare(strings.ToLower(s1), strings.ToLower(s2)) == 0 }   func StringCompareTest() { 	fmt.Println("== 區(qū)分大小寫", Equal("go", "Go")) 	//false 	fmt.Println("== 忽略大小寫",Equal2("go", "Go"))  //true 	fmt.Println("compare 區(qū)分大小寫",Compare("go", "Go")) //false 	fmt.Println("compare 忽略大小寫",Compare2("go", "Go")) //true 	fmt.Println("EqualFold 忽略大小寫",EqualFold("go", "Go")) // true }
        登錄后復(fù)制

        性能比較

        下面的代碼使用 Benchmark 做簡單的性能比較,測試項(xiàng)目的目錄結(jié)構(gòu)為:

        go語言怎么比較字符串

        詳細(xì)代碼:

        package test  import ( 	"../str" 	"testing" )  func BenchmarkStrEqual(b *testing.B) { 	for i := 0; i < b.N; i++ { 		str.Equal("go", "Go") 	} } func BenchmarkStrEqual2(b *testing.B) { 	for i := 0; i < b.N; i++ { 		str.Equal2("go", "Go") 	} } func BenchmarkStrCompare(b *testing.B) { 	for i := 0; i < b.N; i++ { 		str.Compare("go", "Go") 	} } func BenchmarkStrCompare2(b *testing.B) { 	for i := 0; i < b.N; i++ { 		str.Compare2("go", "Go") 	} } func BenchmarkStrEqualFold(b *testing.B) { 	for i := 0; i < b.N; i++ { 		str.EqualFold("go", "Go") 	} }
        登錄后復(fù)制

        測試結(jié)果如下:

        go語言怎么比較字符串

        通過上圖可以看出,效率最高的還是 ==

        源碼簡單分析

        1、strings.Compare

        package strings  // Compare returns an integer comparing two strings lexicographically. // The result will be 0 if a==b, -1 if a < b, and +1 if a > b. // // Compare is included only for symmetry with package bytes. // It is usually clearer and always faster to use the built-in // string comparison operators ==, <, >, and so on. func Compare(a, b string) int { 	// NOTE(rsc): This function does NOT call the runtime cmpstring function, 	// because we do not want to provide any performance justification for 	// using strings.Compare. Basically no one should use strings.Compare. 	// As the comment above says, it is here only for symmetry with package bytes. 	// If performance is important, the compiler should be changed to recognize 	// the pattern so that all code doing three-way comparisons, not just code 	// using strings.Compare, can benefit. 	if a == b { 		return 0 	} 	if a < b { 		return -1 	} 	return +1 }
        登錄后復(fù)制

        如上所示,我們發(fā)現(xiàn),Compare 內(nèi)部也是調(diào)用了 == , 而且該函數(shù)的注釋中也說了,這個函數(shù) only for symmetry with package bytes。而且推薦我們直接使用 ==><

        2、strings.EqualFold

        // EqualFold reports whether s and t, interpreted as UTF-8 strings, // are equal under Unicode case-folding, which is a more general // form of case-insensitivity. func EqualFold(s, t string) bool { 	for s != "" && t != "" { 		// Extract first rune from each string. 		var sr, tr rune 		if s[0] < utf8.RuneSelf { 			sr, s = rune(s[0]), s[1:] 		} else { 			r, size := utf8.DecodeRuneInString(s) 			sr, s = r, s[size:] 		} 		if t[0] < utf8.RuneSelf { 			tr, t = rune(t[0]), t[1:] 		} else { 			r, size := utf8.DecodeRuneInString(t) 			tr, t = r, t[size:] 		}  		// If they match, keep going; if not, return false.  		// Easy case. 		if tr == sr { 			continue 		}  		// Make sr < tr to simplify what follows. 		if tr < sr { 			tr, sr = sr, tr 		} 		// Fast check for ASCII. 		if tr < utf8.RuneSelf { 			// ASCII only, sr/tr must be upper/lower case 			if 'A' <= sr && sr <= 'Z' && tr == sr+'a'-'A' { 				continue 			} 			return false 		}  		// General case. SimpleFold(x) returns the next equivalent rune > x 		// or wraps around to smaller values. 		r := unicode.SimpleFold(sr) 		for r != sr && r < tr { 			r = unicode.SimpleFold(r) 		} 		if r == tr { 			continue 		} 		return false 	}  	// One string is empty. Are both? 	return s == t }
        登錄后復(fù)制

        這個函數(shù)中做了一系列操作,將兩個字符串轉(zhuǎn)換成 utf-8 字符串進(jìn)行比較,并且比較時忽略大小寫。

        總結(jié)

        通過上面的簡單總結(jié)和分析,我們發(fā)現(xiàn),字符串比較還是直接用 == 、>、 < 比較運(yùn)算符吧,簡單快捷效率高。

        贊(0)
        分享到: 更多 (0)
        網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號
        主站蜘蛛池模板: 国产福利精品一区二区| 2020亚洲男人天堂精品| 国产精品对白交换视频| 久久国产精品偷99| 久草视频在线这里精品| 久久影院综合精品| 青青青青久久精品国产h久久精品五福影院1421 | 国产精品国产AV片国产| 91麻豆精品国产| 精品无人码麻豆乱码1区2区| 亚洲性日韩精品国产一区二区| 精品久久久久久无码免费| 99亚洲精品视频| 日本一区二区三区精品中文字幕| 国产精品三级在线观看无码| 亚洲精品无码乱码成人| 亚洲精品第一国产综合精品99| 国语自产精品视频在线观看| 中文字幕一区二区精品区| 四虎国产精品永久地址99| 国产cosplay精品视频| 国产麻豆精品久久一二三| 老司机性色福利精品视频| 亚洲AV永久无码精品| 亚洲精品无码AV中文字幕电影网站| 久久精品国产精品亚洲艾草网美妙| 国产香蕉国产精品偷在线| 国产精品女同一区二区久久| 国产精品99久久久久久董美香| 国产高清在线精品一区二区三区| 国产精品免费大片一区二区| 国产精品视频九九九| 国产小呦泬泬99精品| 久久精品国产一区二区| 日韩精品国产自在欧美| 亚洲精品无码专区在线播放| 亚洲精品少妇30p| 奇米影视7777久久精品| 国产精品免费福利久久| 大伊香蕉精品视频在线导航| 国产精品大白天新婚身材|