9

golang 字符串比较是否相等

 3 years ago
source link: https://blog.csdn.net/oqqYuan1234567890/article/details/59110219
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

golang 字符串比较是否相等

皿小草 2017-03-01 16:17:47 65110

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快,下面是注释
/ 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



About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK