go语言如何比较字符串
编程并不是一个机械性的工作,而是需要有思考,有创新的工作,语法是固定的,但解决问题的思路则是依靠人的思维,这就需要我们坚持学习和更新自己的知识。今天golang学习网就整理分享《go语言如何比较字符串》,文章讲解的知识点主要包括go语言,如果你对Golang方面的知识点感兴趣,就不要错过golang学习网,在这可以对大家的知识积累有所帮助,助力开发能力的提升。
这篇文章主要介绍“go语言如何比较字符串”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“go语言如何比较字符串”文章能帮助大家解决问题。
比较方法: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)”。
Go语言比较字符串方式
在 go 语言中字符串比较的方式有如下三种:
==直接比较,区分大小写strings.Compare(a,b)该函数返回值为 int, 0 表示两数相等,1 表示 a>b, -1 表示 astrings.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 str.Equal("go", "Go")
}
}
func BenchmarkStrEqual2(b *testing.B) {
for i := 0; i str.Equal2("go", "Go")
}
}
func BenchmarkStrCompare(b *testing.B) {
for i := 0; i str.Compare("go", "Go")
}
}
func BenchmarkStrCompare2(b *testing.B) {
for i := 0; i str.Compare2("go", "Go")
}
}
func BenchmarkStrEqualFold(b *testing.B) {
for i := 0; 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.
//
// 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 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] sr, s = rune(s[0]), s[1:]
} else {
r, size := utf8.DecodeRuneInString(s)
sr, s = r, s[size:]
}
if t[0] 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 if tr tr, sr = sr, tr
}
// Fast check for ASCII.
if tr // ASCII only, sr/tr must be upper/lower case
if '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 r = unicode.SimpleFold(r)
}
if r == tr {
continue
}
return false
}
// One string is empty. Are both?
return s == t
}
这个函数中做了一系列操作,将两个字符串转换成 utf-8 字符串进行比较,并且比较时忽略大小写。
关于“go语言如何比较字符串”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注golang学习网行业资讯频道,小编每天都会为大家更新不同的知识点。
终于介绍完啦!小伙伴们,这篇关于《go语言如何比较字符串》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!
go语言中的限流漏桶和令牌桶库怎么使用
- 上一篇
- go语言中的限流漏桶和令牌桶库怎么使用
- 下一篇
- go语言中join方法怎么使用
-
- Golang · Go教程 | 1小时前 |
- Go runtime/metrics 怎样判断描述符是否支持直方图样本
- 404浏览 收藏
-
- Golang · Go教程 | 1小时前 |
- Go crypto/tls 怎样显式启用 ML-KEM 密钥交换
- 311浏览 收藏
-
- Golang · Go教程 | 11小时前 |
- Go unicode/utf8 怎样统计 rune 数而不是字节数
- 262浏览 收藏
-
- Golang · Go教程 | 11小时前 |
- Go regexp 怎样用 LiteralPrefix 判断固定前缀
- 367浏览 收藏
-
- Golang · Go教程 | 11小时前 | 标准库 · go · Go 通配符 filepath.Match path.Match 资源名
- Go path 怎样用 Match 判断资源名通配符
- 300浏览 收藏
-
- Golang · Go教程 | 11小时前 | 标准库 · 正则表达式 · Go教程 · 正则表达式 Go regexp encoding.TextAppender Regexp.AppendText
- Go regexp.Regexp AppendText 怎样保留正则表达式文本
- 454浏览 收藏
-
- Golang · Go教程 | 11小时前 |
- Go os 怎样用 File.Chmod 维护文件权限位
- 195浏览 收藏
-
- Golang · Go教程 | 11小时前 |
- Go reflect 怎样区分无效 Value 与零值 Value
- 311浏览 收藏
-
- Golang · Go教程 | 11小时前 |
- Go text/template 怎样在模板中注册带错误返回值的函数
- 490浏览 收藏
-
- Golang · Go教程 | 11小时前 |
- Go http.Request.Write 怎样把请求写成 HTTP 报文
- 442浏览 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 485次学习
-
- PubMedQA
- 深入了解PubMedQA生物医学问答数据集,涵盖其核心功能、使用方法及在临床决策、药物研发等场景的应用,助力提升NLP模型性能。
- 670次使用
-
- H2O EvalGPT
- H2O EvalGPT是H2O.ai推出的开源LLM评估平台,提供详细的大模型性能排行榜、行业特定基准测试及A/B测试功能,助您快速选择最适合项目的高性能大语言模型。
- 689次使用
-
- LMArena
- LMArena是加州大学伯克利分校推出的AI模型匿名评测平台。通过盲测投票机制,用户可对比不同大模型回答并生成实时排行榜,助力开发者优化模型及用户选择最佳AI工具。
- 735次使用
-
- HELM
- 深入了解斯坦福推出的HELM(Holistic Evaluation of Language Models)大模型评测体系。本文解析其核心功能、安装配置步骤及应用场景,涵盖准确性、公平性、鲁棒性等多维度指标,助力开发者全面优化语言模型性能。
- 701次使用
-
- MMBench
- MMBench是由上海人工智能实验室等机构联合推出的多模态基准测试平台,提供细粒度能力评估、大规模数据集及VLMEvalKit工具。本文详细介绍其核心功能、安装使用方法及应用场景,助力开发者全面评估多模态模型性能。
- 537次使用
-
- 有关Go语言拼接URL路径的方法
- 2023-03-09 185浏览
-
- go语言能不能做后端
- 2023-03-03 460浏览
-
- go语言和java的区别是什么
- 2023-03-03 430浏览
-
- go语言如何进行强制类型转换
- 2023-03-04 450浏览
-
- go语言的beego怎么使用
- 2023-03-03 320浏览

