欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Go 字符串比較的實(shí)現(xiàn)示例

 更新時(shí)間:2022年01月24日 16:40:42   作者:banjming  
本文主要介紹了Go 字符串比較的實(shí)現(xiàn)示例,主要包括三種比較方式,具有一定的參考價(jià)值,感興趣的可以了解一下

字符串比較, 可以直接使用 == 進(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編碼在小寫(xiě)的條件下是否相等,不區(qū)分大小寫(xiě)

// 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ū)分大小寫(xiě)的, == 速度執(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

忽略大小寫(xiě)比較

有時(shí)候要忽略大小寫(xiě)比較, 可以使用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
}

通過(guò)源碼可看到 if 'A' <= sr && sr <= 'Z' && tr == sr+'a'-'A'  可以看到不區(qū)分大小寫(xiě)的實(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ǔ)言實(shí)現(xiàn)圖片快遞信息識(shí)別的簡(jiǎn)易方法

    Go語(yǔ)言實(shí)現(xiàn)圖片快遞信息識(shí)別的簡(jiǎn)易方法

    這篇文章主要為大家介紹了Go語(yǔ)言實(shí)現(xiàn)圖片快遞信息識(shí)別的簡(jiǎn)易方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-10-10
  • Golang?strings包常用字符串操作函數(shù)

    Golang?strings包常用字符串操作函數(shù)

    Golang?中的字符串統(tǒng)一使用?UTF-8?(屬于Unicode編碼的一種實(shí)現(xiàn)方式)進(jìn)行編碼,本篇文章將結(jié)合具體實(shí)例對(duì)常用的字符串操作函數(shù)進(jìn)行介紹,感興趣的可以了解一下
    2021-12-12
  • Go語(yǔ)言使用MySql的方法

    Go語(yǔ)言使用MySql的方法

    這篇文章主要介紹了Go語(yǔ)言使用MySql的方法,實(shí)例分析了Go語(yǔ)言操作MySQL的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-02-02
  • Go語(yǔ)言之結(jié)構(gòu)體與方法

    Go語(yǔ)言之結(jié)構(gòu)體與方法

    這篇文章主要介紹了Go語(yǔ)言之結(jié)構(gòu)體與方法,結(jié)構(gòu)體是由一系列具有相同類型或不同類型的數(shù)據(jù)構(gòu)成的數(shù)據(jù)集合。下面我們就一起來(lái)學(xué)習(xí)什么是Go語(yǔ)言之結(jié)構(gòu)體
    2021-10-10
  • Go字符串操作深入解析

    Go字符串操作深入解析

    這篇文章主要為大家介紹了Go字符串操作深入解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • gin正確多次讀取http?request?body內(nèi)容實(shí)現(xiàn)詳解

    gin正確多次讀取http?request?body內(nèi)容實(shí)現(xiàn)詳解

    這篇文章主要為大家介紹了gin正確多次讀取http?request?body內(nèi)容實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • Go語(yǔ)言中實(shí)現(xiàn)打印堆棧的errors包的用法詳解

    Go語(yǔ)言中實(shí)現(xiàn)打印堆棧的errors包的用法詳解

    因?yàn)镚o語(yǔ)言提供的錯(cuò)誤太簡(jiǎn)單了,以至于簡(jiǎn)單的我們無(wú)法更好的處理問(wèn)題,所以誕生了很多對(duì)錯(cuò)誤處理的庫(kù),github.com/pkg/errors是比較簡(jiǎn)潔的一樣,本文就來(lái)聊聊它的具體用法吧
    2023-07-07
  • Go語(yǔ)言每天必學(xué)之switch語(yǔ)句

    Go語(yǔ)言每天必學(xué)之switch語(yǔ)句

    這篇文章主要為大家詳細(xì)介紹了Go語(yǔ)言每天必學(xué)之switch語(yǔ)句的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • Golang中omitempty關(guān)鍵字的具體實(shí)現(xiàn)

    Golang中omitempty關(guān)鍵字的具體實(shí)現(xiàn)

    本文主要介紹了Golang中omitempty關(guān)鍵字的具體實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • Go語(yǔ)言實(shí)戰(zhàn)之切片內(nèi)存優(yōu)化

    Go語(yǔ)言實(shí)戰(zhàn)之切片內(nèi)存優(yōu)化

    Go 語(yǔ)言的切片是一個(gè)動(dòng)態(tài)的數(shù)據(jù)結(jié)構(gòu),可以方便地對(duì)其進(jìn)行擴(kuò)容和縮容操作。這篇文章主要為大家詳細(xì)介紹了Go語(yǔ)言如何實(shí)現(xiàn)切片內(nèi)存優(yōu)化,需要的可以參考一下
    2023-03-03

最新評(píng)論