英文:
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")))
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论