比較方法:1、直接使用“==”運算子比較,語法“str1 == str2”,該方法區分大小寫。 2.利用strings套件的Compare()函數比較,語法「strings.Compare(a,b)」;傳回值為int型別,0表示兩數相等,1表示a大於b,「-1」表示a小於b 。 3.利用strings套件的EqualFold()比較,語法「strings.EqualFold(a,b)」。 本教學操作環境:windows7系統、GO 1.18版本、Dell G3電腦。 Go語言比較字串方式 在go 語言中字串比較的方式有以下三種: == 直接比較,區分大小寫 strings.Compare(a,b) 此函數傳回值為int, 0 表示兩數相等,1 表示a>b, -1 表示a strings.EqualFold(a,b) 直接回傳是否相等,不區分大小寫。 範例如下:// 1-使用等號比較-區分大消息 #func Equal(s1, s2 string) bool { return s1 == s2 } // 2-使用 compare 比较——区分大小写 func Compare(s1, s2 string) bool { return strings.Compare(s1, s2) == 0 // } //3-EqualFold 比较——不区分大小写. 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 比较——不区分大小写 func Compare2(s1, s2 string) bool { return strings.Compare(strings.ToLower(s1), strings.ToLower(s2)) == 0 } func StringCompareTest() { fmt.Println("== 区分大小写", Equal("go", "Go")) //false fmt.Println("== 忽略大小写",Equal2("go", "Go")) //true fmt.Println("compare 区分大小写",Compare("go", "Go")) //false fmt.Println("compare 忽略大小写",Compare2("go", "Go")) //true fmt.Println("EqualFold 忽略大小写",EqualFold("go", "Go")) // true } 效能比較 下面的程式碼使用Benchmark 做簡單的效能比較,測試專案的目錄結構為: 詳細程式碼: 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") } } 測試結果如下: 透過上圖可以看出,效率最高的還是== 原始碼簡單分析 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 } 如上所示,我們發現,Compare 內部也是呼叫了= = , 而且函數的註解中也說了,這個函數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 } 這個函數中做了一系列操作,將兩個字串轉換成utf -8 字串進行比較,並且在比較時忽略大小寫。 總結 透過上面的簡單總結與分析,我們發現,字串比較還是直接用== 、>、 【相關推薦:Go影片教學、程式設計教學】