Go 语言中的 EqualFold 函数是什么?


Go 语言中的 **EqualFold()** 函数是 strings 包的内置函数之一,它用于检查给定的字符串(UTF-8 字符串)是否相等。此比较不区分大小写。它接受两个 string 参数,如果在 Unicode 折叠(即,不区分大小写)下两个字符串相等,则返回 True,否则返回 False。

语法

func EqualFold(s, t string) bool

其中 s 和 t 为字符串,会返回一个布尔值。

示例

以下示例演示如何在 Go 程序中使用 **EqualFold()** −

package main
import (
   "fmt"
   "strings"
)
func main() {

   // Intializing the Strings
   R := "Welcome to Tutorialspoint"
   S := "WELCOME TO TUTORIALSPOINT"
   fmt.Println("First string:", R)
   fmt.Println("Second string:", S)

   // using the EqualFold function
   if strings.EqualFold(R, S) == true {
      fmt.Println("Both the strings are equal")
   } else {
      fmt.Println("The strings are not equal.")
   }
}

输出

它会生成以下输出 −

First string: Welcome to Tutorialspoint
Second string: WELCOME TO TUTORIALSPOINT
Both the strings are equal

注意,比较不区分大小写。

示例

我们再看另一个示例 −

package main
import (
   "fmt"
   "strings"
)
func main() {

   // Initializing the Strings
   x := "Prakash"
   y := "PRAKASH"
   z := "Vijay"
   w := "Pruthvi"

   // Display the Strings
   fmt.Println("First string:", x)
   fmt.Println("Second string:", y)
   fmt.Println("Third string:", z)
   fmt.Println("Fourth string:", w)

   // Using the EqualFold Function
   test1 := strings.EqualFold(x, y)
   test2 := strings.EqualFold(z, w)
   
   // Display the EqualFold Output
   fmt.Println("First Two Strings are Equal? :", test1)
   fmt.Println("Last Two Strings are Equal? :", test2)
}

输出

它会生成以下输出 −

First string: Prakash
Second string: PRAKASH
Third string: Vijay
Fourth string: Pruthvi
First Two Strings are Equal? : true
Last Two Strings are Equal? : false

更新日期: 2022 年 3 月 10 日

537 次浏览

开启你的 职业生涯

完成课程获得认证

开始吧
广告
© . All rights reserved.