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.