How to determine string equality in Golang?

WBOY
Release: 2024-03-13 15:24:04
Original
969 people have browsed it

How to determine string equality in Golang?

How to determine string equality in Golang?

In Golang, determining whether two strings are equal is a common operation. In this article, we will introduce several commonly used methods to determine whether two strings are equal and give corresponding code examples.

Method 1: Use the==operator

The simplest way is to use the==operator to determine whether two strings are equal . An example is as follows:

package main import "fmt" func main() { str1 := "Hello" str2 := "Hello" if str1 == str2 { fmt.Println("str1 和 str2 相等") } else { fmt.Println("str1 和 str2 不相等") } }
Copy after login

In the above example, we created two stringsstr1andstr2and then used the==operation symbol to compare whether they are equal. If they are equal, output "str1 and str2 are equal", otherwise output "str1 and str2 are not equal".

Method 2: Usestrings.EqualFoldfunction

In addition to using the==operator, we can also usestrings.EqualFoldFunction to determine whether two strings are equal. This function compares two strings regardless of case. An example is as follows:

package main import ( "fmt" "strings" ) func main() { str1 := "Hello" str2 := "hello" if strings.EqualFold(str1, str2) { fmt.Println("str1 和 str2 相等") } else { fmt.Println("str1 和 str2 不相等") } }
Copy after login

In the above example, we use thestrings.EqualFoldfunction to compare whetherstr1andstr2are equal. The function ignores case, so the output is "str1 and str2 are equal".

Method 3: Use thestrings.Comparefunction

Another method is to use thestrings.Comparefunction to compare the sizes of two strings . If the two strings are equal, the function returns 0. The example is as follows:

package main import ( "fmt" "strings" ) func main() { str1 := "Hello" str2 := "Hello" result := strings.Compare(str1, str2) if result == 0 { fmt.Println("str1 和 str2 相等") } else { fmt.Println("str1 和 str2 不相等") } }
Copy after login

In the above example, we use thestrings.Comparefunction to compare whetherstr1andstr2are equal. The strings are equal, so the output result is "str1 and str2 are equal".

Through the above methods, we can easily determine whether two strings are equal in Golang. Choosing the appropriate method can be determined based on specific needs to achieve the best judgment.

The above is the detailed content of How to determine string equality in Golang?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!