Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use go language to realize string comparison

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report