英文:
Creating a substring in go creates a new kind of symbol
问题
我正在比较字符串,并且出现了以下情况:
请注意,NEW 前面的引号是不同的。
现在,当像这样调用我的函数:
my_func(a[18:], b[18:])
结果字符串出人意料地是:
我需要做什么才能去掉这个奇怪的符号,为什么会出现这种情况?
英文:
I am comparing strings and there is the following:
Please note that the " in front of NEW are different.
Now when calling my function like this:
my_func(a[18:], b[18:])
The resulting strings are surprisingly:
What do I have to do to cut this weird symbol away and why is it behaving like this?
答案1
得分: 7
因为这种引号是多字节字符,而你正在字符串的字符中间进行分割。你可以将其转换为[]rune
,然后再转换回来:
s := "H界llo"
fmt.Println(s[1:3]) // ��
fmt.Println(string([]rune(s)[1:3])) // 界l
链接:https://play.golang.org/p/pw42sEwRTZd
英文:
Because that type of quote is a multibyte character, and you are splitting the string in the middle of a character. What you could do is convert to an []rune
and then convert back:
https://play.golang.org/p/pw42sEwRTZd
s := "H界llo"
fmt.Println(s[1:3]) // ��
fmt.Println(string([]rune(s)[1:3])) // 界l
答案2
得分: 3
另一个选项是utf8string
包:
package main
import "golang.org/x/exp/utf8string"
func main() {
s := utf8string.NewString(` 'Not Available') “NEW CREDIT" FROM customers;`)
t := s.Slice(18, s.RuneCount())
println(t == `“NEW CREDIT" FROM customers;`)
}
https://pkg.go.dev/golang.org/x/exp/utf8string
英文:
Another option is the utf8string
package:
package main
import "golang.org/x/exp/utf8string"
func main() {
s := utf8string.NewString(` 'Not Available') “NEW CREDIT" FROM customers;`)
t := s.Slice(18, s.RuneCount())
println(t == `“NEW CREDIT" FROM customers;`)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论