在Go语言中创建一个子字符串会创建一种新的符号。

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

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:

在Go语言中创建一个子字符串会创建一种新的符号。

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:

在Go语言中创建一个子字符串会创建一种新的符号。

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;`)
}

https://pkg.go.dev/golang.org/x/exp/utf8string

huangapple
  • 本文由 发表于 2021年6月4日 06:18:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/67829290.html
匿名

发表评论

匿名网友

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

确定