Golang 什么是EqualFold函数
Golang中的 EqualFold() 函数是strings包中的一个内置函数,用于检查给定的字符串(UTF-8字符串)是否相等。这种比较是不分大小写的。它接受两个字符串参数,如果两个字符串在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
极客教程