当前位置:网站首页>Go string comparison
Go string comparison
2022-06-23 09:45:00 【51CTO】
golang String comparison
String comparison , You can use it directly == Compare , It can also be used strings.Compare Compare
go There are three ways to compare strings in :
-
== Compare -
strings.Compare Compare -
strings.EquslFold Compare
The above code execution results are as follows :
Compare and EqualFold difference
-
EqualFold Is the more UTF-8 Whether the codes are equal in lowercase , Case insensitive
- It should be noted that
Compare Functions are case sensitive , == Faster execution
Ignore case comparison
Sometimes you have to ignore the case comparison , have access to strings.EqualFold Compare strings for equality
The source code to achieve
// 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.
Through the source code, you can see if 'A' <= sr && sr <= 'Z' && tr == sr+'a'-'A' You can see the case insensitive implementation .
Look at a complete test code :
// 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.
Execution structure
Get more , Pay attention to the wealth and freedom of official account programmers

official account : The path of programmer wealth and freedom
Pay attention to our , Learn more about
Reference material
- https://www.geeksforgeeks.org/strings-equalfold-function-in-golang-with-examples/
- https://cloud.tencent.com/developer/article/1651885
边栏推荐
- Servlet-02 lifecycle
- Bioinformatics | 基于相互作用神经网络的有效药物-靶标关联预测
- Jog sport mode
- UEFI源码学习3.7 - NorFlashDxe
- Redis learning notes - AOF of persistence mechanism
- 利用华为云ECS服务器搭建安防视频监控平台
- What is BFC? What problems can BFC solve
- 2022 Gdevops全球敏捷运维峰会-广州站精华回放(附ppt下载)
- Redis learning notes - redis cli explanation
- 我被提拔了,怎么和原来平级的同事相处?
猜你喜欢

Go language JSON processing

Learn SCI thesis drawing skills (E)

Bioinformatics | 基于相互作用神经网络的有效药物-靶标关联预测

Typora set up image upload service

我被提拔了,怎么和原来平级的同事相处?

安装typescript环境并开启VSCode自动监视编译ts文件为js文件

swagger UI :%E2%80%8B

ionic5錶單輸入框和單選按鈕
![[网鼎杯 2020 青龙组]AreUSerialz](/img/38/b67f7a42abec1cdaad02f2b7df6546.png)
[网鼎杯 2020 青龙组]AreUSerialz

ICLR 2022 | 视频中的动态卷积TAdaConv以及高效的卷积视频理解模型TAdaConvNeXt
随机推荐
Go language JSON processing
[GXYCTF2019]BabySQli
How should junior programmers who enter a small company improve themselves?
Difference between global shutter and roller shutter
Distributed common interview questions
Redis learning notes - transactions
安装typescript环境并开启VSCode自动监视编译ts文件为js文件
Jump game of leetcode topic analysis
12个球,有一个与其他不一样,提供一个天平,三次找出来
swagger UI :%E2%80%8B
ionic5表单输入框和单选按钮
必须知道的RPC内核细节(值得收藏)!!!
Gesture recognition based on mediapipe
AI system frontier dynamics issue 38: Google has abandoned tensorflow?; Four GPU parallel strategies for training large models; Father of llvm: modular design determines AI future
Gorm 高级查询
AI系统前沿动态第38期:谷歌已放弃TensorFlow?;训练大模型的四种GPU并行策略;LLVM之父:模块化设计决定AI前途
利用华为云ECS服务器搭建安防视频监控平台
Pet Feeder Based on stm32
A 32KB cache with direct mapping Memory exercises after class
[MRCTF2020]Ez_ bypass