Go 的strings.EqualFold 使用

15 min read

Go 的 strings.EqualFold 函数用于比较两个字符串是否相等(不区分大小写)。它的函数签名如下:

func EqualFold(s, t string) bool

其中,参数 st 分别为要比较的两个字符串。该函数返回一个 bool 类型的值,表示两个字符串是否相等(不区分大小写)。

使用示例:

package main

import (
    "fmt"
    "strings"
)

func main() {
    str1 := "hello"
    str2 := "HeLLo"

    if strings.EqualFold(str1, str2) {
        fmt.Println("Two strings are equal")
    } else {
        fmt.Println("Two strings are not equal")
    }
}

输出结果为:

Two strings are equal

在上面的示例中,我们将两个字符串进行了比较,并使用 strings.EqualFold 函数判断它们是否相等(不区分大小写)。由于两个字符串的所有字符(不区分大小写)都相同,所以该函数返回了 true,并输出了 "Two strings are equal"。