Go: 字符串比较的三种方法
golang字符串比較的三種常見方法
fmt.Println("go"=="go")
fmt.Println("GO"=="go")
fmt.Println(strings.Compare("GO","go")) ?//不等 返回 -1
fmt.Println(strings.Compare("go","go")) ?//相等返回 0
fmt.Println(strings.EqualFold("GO","go"))
輸出
true
false
-1
0
true
1,自建方法“==”,區(qū)分大小寫,最簡(jiǎn)單的方法
2,Compare函數(shù),區(qū)分大小寫,自建方法“==”的速度要比Compare快,下面是注釋
/ 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
3,比較UTF-8編碼在小寫的條件下是否相等,不區(qū)分大小寫,下面是注釋
// EqualFold reports whether s and t, interpreted as UTF-8 strings, // are equal under Unicode case-folding. func EqualFold(s, t string) bool
?
總結(jié)
以上是生活随笔為你收集整理的Go: 字符串比较的三种方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Go: init()执行顺序问题
- 下一篇: linux gcc编译错误:static