How to use go language to realize string comparison
In this article, the editor introduces in detail "how to use go language to achieve string comparison". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to use go language to achieve string comparison" can help you solve your doubts. Let's follow the editor's ideas to learn new knowledge.
Go language to compare strings: 1, use the "= =" operator, syntax "string 1" = string 2 "; 2, use the" strings.ToLower () "function; 3, use the" strings.Compare () "function; 4, use the" strings.EqualFold () "function.
Method 1: use the "=" operator (case sensitive)
Package mainimport "fmt" func main () {fmt.Println ("go" = = "go") fmt.Println ("GO" = = "go")}
Output:
Truefalse
Method 2: use strings.ToLower (case-insensitive)
Package mainimport ("fmt"strings") func main () {srcString: = "This a string" destString: = "this a string" if strings.ToLower (srcString) = = strings.ToLower (destString) {fmt.Println ("equal")} else {fmt.Println ("unequal")}}
Output:
Equal
Method 3: use strings.Compare (case sensitive)
Package mainimport ("fmt"strings") func main () {fmt.Println (strings.Compare ("GO", "go")) fmt.Println (strings.Compare ("go", "go"))}
Output:
-10
The Compare function, case-sensitive, is faster than the self-built method "=". Here are the comments
/ 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
Method 4: use strings.EqualFold (case-insensitive)
Package mainimport ("fmt"strings") func main () {fmt.Println (strings.EqualFold ("GO", "go")) fmt.Println (strings.EqualFold ("go", "go"))}
Output:
Truetrue has read this article, "how to use the go language to achieve string comparison" article has been introduced, want to master the knowledge of this article still need to be hands-on practice to understand, if you want to know more related articles, welcome to follow the industry information channel.