網站首頁 編程語言 正文
字符串比較, 可以直接使用 == 進行比較, 也可用用 strings.Compare 比較
go 中字符串比較有三種方式:
- == 比較
- strings.Compare 比較
- strings.EquslFold 比較
#### 代碼示例 ```go 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
Compare 和 EqualFold 區別
EqualFold 是比較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
要注意的是 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
忽略大小寫比較
有時候要忽略大小寫比較, 可以使用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 }
通過源碼可看到 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")) }
執行結構
true
true
原文鏈接:https://juejin.cn/post/7032887851066851342
相關推薦
- 2022-11-29 .NET中lambda表達式合并問題及解決方法_實用技巧
- 2023-03-13 React中如何設置多個className_React
- 2022-05-04 配置Spring.Net框架開發環境_實用技巧
- 2023-06-21 ProtoBuf動態拆分Gradle?Module解析_Android
- 2022-05-06 python從數據庫中取出文件保存到excel,csv表格中的辦法:
- 2023-07-13 react中的useRef的使用
- 2022-05-25 springboot踩坑日記Feign傳遞MultipartFile詳解
- 2023-02-23 GO中的條件變量sync.Cond詳解_Golang
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支