[轉] golang 字符串比較是否相等


1 前言

strings.EqualFold不區分大小寫,"==" 區分且直觀。

2 代碼

golang字符串比較的三種常見方法

fmt.Println("go"=="go")
fmt.Println("GO"=="go")

fmt.Println(strings.Compare("GO","go"))
fmt.Println(strings.Compare("go","go"))

fmt.Println(strings.EqualFold("GO","go"))
輸出

true
false
-1
0
true
1,自建方法“==”,區分大小寫,最簡單的方法

2,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編碼在小寫的條件下是否相等,不區分大小寫,下面是注釋
// EqualFold reports whether s and t, interpreted as UTF-8 strings,
// are equal under Unicode case-folding.
func EqualFold(s, t string) bool

3 參考

轉摘:https://studygolang.com/articles/14163  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM