📌  相关文章
📜  如何在 Golang 中的 Unicode 大小写折叠下检查字符串是否相等?

📅  最后修改于: 2021-10-25 02:40:49             🧑  作者: Mango

在 Go 语言中,字符串不同于Java、C++、 Python等其他语言。它是一系列可变宽度字符,其中每个字符都由一个或多个使用 UTF-8 编码的字节表示。
在 Go字符串,您可以在EqualFold()函数的帮助下将两个字符串相互比较而不会出现任何错误,即使它们以不同的大小写(小写和大写)编写。或者说,这个函数用于检查指定的字符串是否被解释为UTF-8字符串并且在Unicode case-folding下是否相等。这个函数返回true,如果给定的字符串是Unicode的下外壳折叠或假的回报等于如果给定的字符串不Unicode的下外壳折叠相等。它是在字符串包下定义的,因此您必须在程序中导入字符串包才能访问 EqualFold函数。

句法:

func EqualFold(str1, str2 string) bool

此函数的返回类型为 bool 类型。让我们在示例的帮助下讨论这个概念:

示例 1:

// Go program to illustrate how to
// check the given strings are equal or not
package main
  
import (
    "fmt"
    "strings"
)
  
// Main function
func main() {
  
    // Creating strings
    // Using var keyword
    var username string
    var uinput string
  
    // Initializing strings
    username = "AnkitaSaini"
    uinput = "ankitasaINI"
  
    // Checking whether the given
    // strings are equal or not
    // Using EqualFold() function
    res := strings.EqualFold(username, uinput)
  
    // Displaying the result according
    // to the given condition
    if res == true {
  
        fmt.Println("!..Successfully Matched..!")
    } else {
  
        fmt.Println("!..Not Matched..!")
    }
  
}

输出:

!..Successfully Matched..!

示例 2:

// Go program to illustrate how to check
// the given strings are equal or not
package main
  
import (
    "fmt"
    "strings"
)
  
// Main function
func main() {
  
    // Creating and initializing strings
    // Using shorthand declaration
    s1 := "I am working as a Technical content writer, in GeeksforGeeks!"
    s2 := "I am currently writing articles on Go language!"
  
    // Checking the given strings are equal or not
    // Using EqualFold() function
    res1 := strings.EqualFold(s1, "I AM WORKING AS A TECHNICAL CONTENT WRITER, IN GEEKSFORGEEKS!")
    res2 := strings.EqualFold(s1, "I AM working AS A Technical CONTENT writer, IN GeeksforGeeks!")
    res3 := strings.EqualFold(s1, "Apple")
    res4 := strings.EqualFold(s2, "I am currently writing articles on Go language!")
    res5 := strings.EqualFold(s2, "I am currently writing ARTICLES on Go language!")
    res6 := strings.EqualFold("GeeksforGeeks", "geeksForgeeks")
  
    // Displaying results
    fmt.Println("Result 1: ", res1)
    fmt.Println("Result 2: ", res2)
    fmt.Println("Result 3: ", res3)
    fmt.Println("Result 4: ", res4)
    fmt.Println("Result 5: ", res5)
    fmt.Println("Result 6: ", res6)
  
}

输出:

Result 1:  true
Result 2:  true
Result 3:  false
Result 4:  true
Result 5:  true
Result 6:  true