我正在尝试检查字符串中的第一个字符是否与以下字符匹配,请注意UTF-8引号字符:
c := t.Content[0]
if c != '.' && c != ',' && c != '?' && c != '“' && c != '”'{
Run Code Online (Sandbox Code Playgroud)
由于最后两次检查中的特殊字符,此代码不起作用.
这样做的正确方法是什么?
索引string索引的字节数(以UTF-8编码 - 这就是Go如何在内存中存储字符串),但是你想测试第一个字符.
所以你应该得到第一个rune而不是第一个byte.为了提高效率,您可以使用utf8.DecodeRuneInString()仅解码第一个rune.如果你需要所有的符文string,你可以使用类型转换all := []rune("I'm a string").
看这个例子:
for _, s := range []string{"asdf", ".asdf", "”asdf"} {
c, _ := utf8.DecodeRuneInString(s)
if c != '.' && c != ',' && c != '?' && c != '“' && c != '”' {
fmt.Println("Ok:", s)
} else {
fmt.Println("Not ok:", s)
}
}
Run Code Online (Sandbox Code Playgroud)
输出(在Go Playground上试试):
Ok: asdf
Not ok: .asdf
Not ok: ”asdf
Run Code Online (Sandbox Code Playgroud)
添加到 @icza\'s 很好的答案:值得注意的是,虽然字符串索引以字节为单位,range字符串的索引以符文为单位。所以下面的方法也有效:
for _, s := range []string{"asdf", ".asdf", "\xe2\x80\x9dasdf"} {\n for _, c := range s {\n if c != \'.\' && c != \',\' && c != \'?\' && c != \'\xe2\x80\x9c\' && c != \'\xe2\x80\x9d\' {\n fmt.Println("Ok:", s)\n } else {\n fmt.Println("Not ok:", s)\n }\n break // we break after the first character regardless\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n