Go 字符串比較的實(shí)現(xiàn)示例
字符串比較, 可以直接使用 == 進(jìn)行比較, 也可用用 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"))上述代碼執(zhí)行結(jié)果如下:
true
false
-1
0
true
Compare 和 EqualFold 區(qū)別
EqualFold 是比較UTF-8編碼在小寫的條件下是否相等,不區(qū)分大小寫
// EqualFold reports whether s and t, interpreted as UTF-8 strings, // are equal under Unicode case-folding. func EqualFold(s, t string) bool
要注意的是 Compare 函數(shù)是區(qū)分大小寫的, == 速度執(zhí)行更快
// 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
忽略大小寫比較
有時(shí)候要忽略大小寫比較, 可以使用strings.EqualFold 字符串比較是否相等
源碼實(shí)現(xiàn)
// 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' 可以看到不區(qū)分大小寫的實(shí)現(xiàn)。
看個(gè)完整測(cè)試代碼:
// 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"))
}執(zhí)行結(jié)構(gòu)
true
true
到此這篇關(guān)于Go 字符串比較的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)Go 字符串比較內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Go語言:打造優(yōu)雅數(shù)據(jù)庫(kù)單元測(cè)試的實(shí)戰(zhàn)指南
Go語言數(shù)據(jù)庫(kù)單元測(cè)試入門:聚焦高效、可靠的數(shù)據(jù)庫(kù)代碼驗(yàn)證!想要確保您的Go應(yīng)用數(shù)據(jù)層堅(jiān)如磐石嗎?本指南將手把手教您如何利用Go進(jìn)行數(shù)據(jù)庫(kù)單元測(cè)試,輕松揪出隱藏的bug,打造無懈可擊的數(shù)據(jù)處理邏輯,一起來探索吧!2024-01-01
Go并發(fā)編程中的錯(cuò)誤恢復(fù)機(jī)制與代碼持續(xù)執(zhí)行實(shí)例探索
這篇文章主要為大家介紹了Go并發(fā)編程中的錯(cuò)誤恢復(fù)機(jī)制與代碼持續(xù)執(zhí)行實(shí)例探索,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
Go并發(fā):使用sync.WaitGroup實(shí)現(xiàn)協(xié)程同步方式
這篇文章主要介紹了Go并發(fā):使用sync.WaitGroup實(shí)現(xiàn)協(xié)程同步方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-05-05
詳解Golang中errors包如何返回自定義error類型
這篇文章主要為大家詳細(xì)介紹了Golang中errors包如何返回自定義error類型,文中的示例代碼簡(jiǎn)潔易懂,有需要的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-09-09
淺析Golang開發(fā)中g(shù)oroutine的正確使用姿勢(shì)
很多初級(jí)的Gopher在學(xué)習(xí)了goroutine之后,在項(xiàng)目中其實(shí)使用率不高,所以這篇文章小編主要來帶大家深入了解一下goroutine的常見使用方法,希望對(duì)大家有所幫助2024-03-03
Go語言實(shí)現(xiàn)的一個(gè)簡(jiǎn)單Web服務(wù)器
這篇文章主要介紹了Go語言實(shí)現(xiàn)的一個(gè)簡(jiǎn)單Web服務(wù)器,本文先是給出一個(gè)使用http包建立的Web服務(wù)器源碼,并對(duì)比了其它編程語言,需要的朋友可以參考下2014-10-10

