golang中不区分大小写的字符串搜索 [英] Case insensitive string search in golang
本文介绍了golang中不区分大小写的字符串搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何以大小写不敏感方式在文件中搜索单词?
How do I search through a file for a word in a case insensitive manner?
例如
如果我在寻找 UpdaTe
,如果文件包含更新,搜索应选择它并将其计为匹配项。
If I'm searching for UpdaTe
in the file, if the file contains update, the search should pick it and count it as a match.
推荐答案
strings.EqualFold()
可以检查两个字符串是否相等,而忽略大小写。它甚至适用于Unicode。有关详细信息,请参阅 http://golang.org/pkg/strings/#EqualFold
strings.EqualFold()
can check if two strings are equal, while ignoring case. It even works with Unicode. See http://golang.org/pkg/strings/#EqualFold for more info.
http://play.golang.org/p / KDdIi8c3Ar
package main
import (
"fmt"
"strings"
)
func main() {
fmt.Println(strings.EqualFold("HELLO", "hello"))
fmt.Println(strings.EqualFold("ÑOÑO", "ñoño"))
}
两者都返回true。
这篇关于golang中不区分大小写的字符串搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文