本文摘自php中文网,作者青灯夜游,侵删。
go语言比较字符串的方法:1、使用“==”运算符,语法“字符串1==字符串2”;2、使用“strings.ToLower()”函数;3、使用“strings.Compare()”函数;4、使用“strings.EqualFold()”函数。
本教程操作环境:Windows10系统、GO 1.11.2、Dell G3电脑。
方法1:使用“==”运算符(区分大小写)
1 2 3 4 5 6 7 8 |
|
输出:
1 2 |
|
方法2:使用strings.ToLower(不区分大小写)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
输出:
1 |
|
方法3:使用strings.Compare(区分大小写)
1 2 3 4 5 6 7 8 9 10 11 |
|
输出:
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
方法4:使用strings.EqualFold(不区分大小写)
1 2 3 4 5 6 7 8 9 10 11 |
|
输出:
1 2 |
|
推荐学习:Golang教程
以上就是用go语言怎么实现字符串比较的详细内容,更多文章请关注木庄网络博客!!
相关阅读 >>
更多相关阅读请进入《Go语言》频道 >>
Go语言101
一个与时俱进的Go编程知识库。