当前位置:网站首页>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
      
      
#### Code example
```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"))
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

The above code execution results are as follows :

      
      
true
false
-1
0
true
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

Compare and EqualFold difference

  • ​EqualFold​​ Is the more UTF-8 Whether the codes are equal in lowercase , Case insensitive
      
      
// EqualFold reports whether s and t, interpreted as UTF-8 strings, // are equal under Unicode case-folding. func EqualFold(s, t string) bool
  • 1.
  • It should be noted that ​​Compare​​ Functions are case sensitive , ​​==​​ Faster execution

      
      
/ 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
  • 1.

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

      
      
true
true
  • 1.
  • 2.

Get more , Pay attention to the wealth and freedom of official account programmers

Go String comparison _golang

official account : The path of programmer wealth and freedom


Pay attention to our , Learn more about



Reference material


原网站

版权声明
本文为[51CTO]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230932399818.html