Golang Chinese string interception function implementation principle

  • 2020-06-12 09:31:14
  • OfStack

In golang slice intercept an array or string, but when intercepting string is Chinese, possible problem is: because the Chinese character is not only a consists of 1 byte, so directly through the section May 1 characters coding cut in half, as a result, the last one is garbled characters.

Such as:

I want to intercept the first four words


name := " I'm hu 81"
fmt.Println("name[:4] = ",name[:4])

The result will look something like this:


name[:4] =  I ?

Solutions:
Transfer to []rune, then intercept, transfer to string


nameRune := []rune(name)
fmt.Println("string(nameRune[:4]) = ",string(nameRune[:4]))

Operation results:

string(nameRune[:4]) = I am Hu 8

Now that we understand the principle, let's write a complete Golang Chinese string interception function


func SubString(str string, begin, length int) string {
  fmt.Println("Substring =", str)
  rs := []rune(str)
  lth := len(rs)
  fmt.Printf("begin=%d, end=%d, lth=%d\n", begin, length, lth)
  if begin < 0 {
    begin = 0
  }
  if begin >= lth {
    begin = lth
  }
  end := begin + length

  if end > lth {
    end = lth
  }
  fmt.Printf("begin=%d, end=%d, lth=%d\n", begin, length, lth)
  return string(rs[begin:end])
}

Golang Chinese string interception function is very simple to implement using golang principle statement


Related articles: