当前位置:网站首页>Go 字符串比较
Go 字符串比较
2022-06-23 09:35:00 【51CTO】
golang 字符串比较
字符串比较, 可以直接使用 == 进行比较, 也可用用 strings.Compare 比较
go 中字符串比较有三种方式:
-
== 比较 -
strings.Compare 比较 -
strings.EquslFold 比较
上述代码执行结果如下:
Compare 和 EqualFold 区别
-
EqualFold 是比较UTF-8编码在小写的条件下是否相等,不区分大小写
- 要注意的是
Compare 函数是区分大小写的, == 速度执行更快
忽略大小写比较
有时候要忽略大小写比较, 可以使用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]
<
utf8.RuneSelf
{
sr,
s =
rune(s[0]),
s[1:]
}
else
{
r,
size
:=
utf8.DecodeRuneInString(s)
sr,
s =
r,
s[size:]
}
if
t[0]
< utf8.RuneSelf {
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
<
tr
to
simplify
what
follows.
if
tr
< sr {
tr, sr = sr, tr
}
// Fast check for ASCII.
if tr
<
utf8.RuneSelf
{
/
/
ASCII
only,
sr/tr
must
be
upper/lower
case
if
'A'
<= sr
&
& sr
<
=
'Z'
&&
tr =
=
sr+
'a'
-
'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
<
tr
{
r =
unicode.SimpleFold(r)
}
if
r =
=
tr
{
continue
}
return
false
}
/
/
One
string
is
empty.
Are
both?
return
s =
=
t
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
通过源码可看到 if 'A' <= sr && sr <= 'Z' && tr == sr+'a'-'A' 可以看到不区分大小写的实现。
看个完整测试代码:
// Golang program to illustrate the
// strings.EqualFold() Function
package main
// importing fmt and strings
import (
"fmt"
"strings"
)
// calling main method
func main() {
// case insensitive comparing and returns true.
fmt.Println(strings.EqualFold("Geeks", "Geeks"))
// case insensitive comparing and returns true.
fmt.Println(strings.EqualFold("computerscience", "computerscience"))
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
执行结构
获得更多内容,关注公众号程序员财富自由之路

公众号:程序员财富自由之路
关注我们,了解更多
参考资料
- https://www.geeksforgeeks.org/strings-equalfold-function-in-golang-with-examples/
- https://cloud.tencent.com/developer/article/1651885
边栏推荐
猜你喜欢
Redis learning notes - redis and Lua

Correspondence between three-tier architecture and SSM

三层架构与SSM之间的对应关系

ionic5表单输入框和单选按钮

给RepVGG填坑?其实是RepVGG2的RepOptimizer开源

Sequential representation and implementation of sequencelist -- linear structure

Typora set up image upload service
Redis学习笔记—发布订阅

swagger UI :%E2%80%8B
![[ciscn2019 North China Day2 web1]hack world](/img/bf/51a24fd2f9f0e13dcd821b327b5a00.png)
[ciscn2019 North China Day2 web1]hack world
随机推荐
Gesture recognition based on mediapipe
使用base64,展示图片
Redis学习笔记—数据类型:有序集合(zset)
Redis learning notes - data type: hash
Redis learning notes - single key management
UEFI 源码学习4.1 - PciHostBridgeDxe
分布式锁的三种实现方式
Redis学习笔记—redis-benchmark详解
Set the CPU to have 16 address lines and 8 data lines, and use mreq as the access control line number Connection between memory and CPU
Redis学习笔记—客户端通讯协议RESP
RGB与CMYK颜色模式
Ionic5 form input box and radio button
玩转NanoPi 2 裸机教程编程-01点亮User LED难点解析
一元函数求极限三大方法---洛必达法则,泰勒公式
[网鼎杯 2020 青龙组]AreUSerialz
Redis learning notes - transactions
[plugin:vite:import-analysis]Failed to resolve import “@/“ from ““.Does the file exist
cooding代码库的使用笔记
UEFI源码学习3.7 - NorFlashDxe
Redis学习笔记—数据类型:字符串(string)