1

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?

Beginner
  • 5,277
  • 6
  • 34
  • 71
  • 5
    Hint: UTF-8. Strings are made up of bytes; *runes* are made up of one or more bytes from a string. Slicing a string slices off bytes and if you slice off *part* of a rune, that's what you see. – torek Jun 03 '21 at 22:25

2 Answers2

7

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
dave
  • 62,300
  • 5
  • 72
  • 93
3

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

Zombo
  • 1
  • 62
  • 391
  • 407