在Go语言中进行不区分大小写的字符串比较。

huangapple go评论75阅读模式
英文:

Case insensitive string comparison in Go

问题

如何以不区分大小写的方式比较字符串?

例如,"Go" 和 "go" 应该被视为相等。

英文:

How do I compare strings in a case insensitive manner?

For example, "Go" and "go" should be considered equal.

答案1

得分: 109

https://golang.org/pkg/strings/#EqualFold 是你要找的函数。它的使用方法如下(来自链接文档的示例):

package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Println(strings.EqualFold("Go", "go"))
}
英文:

https://golang.org/pkg/strings/#EqualFold is the function you are looking for. It is used like this (example from the linked documentation):

package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Println(strings.EqualFold("Go", "go"))
}

答案2

得分: 0

有一个替代strings.EqualFold的方法,叫做bytes.EqualFold,它的工作方式相同。

package main

import (
	"bytes"
	"fmt"
)

func main() {
	fmt.Println(bytes.EqualFold([]byte("Go"), []byte("go")))
}

https://golang.org/pkg/bytes/#EqualFold

英文:

There is alternative to strings.EqualFold, there is bytes.EqualFold which work in same way

package main

import (
	"bytes"
	"fmt"
)

func main() {
	fmt.Println(bytes.EqualFold([]byte("Go"), []byte("go")))
}

https://golang.org/pkg/bytes/#EqualFold

huangapple
  • 本文由 发表于 2015年5月13日 00:38:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/30196780.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定