Creating a substring in go creates a new kind of symbol

179 Views Asked by At

I am comparing strings and there is the following:

enter image description here

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:

enter image description here

What do I have to do to cut this weird symbol away and why is it behaving like this?

2

There are 2 best solutions below

0
On BEST ANSWER

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

0
On

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