SoFunction
Updated on 2025-03-04

The implementation principle of Golang Chinese string intercept function

In golang, an array or string can be cut through slices, but when the intercepted string is Chinese, the problem that may arise is: since a Chinese character is not just composed of one byte, directly through slices may cut the encoding of a Chinese character in half, resulting in the last character being garbled.

For example:

Want to intercept the first four words

name := "I am Hu Bayi"
("name[:4] = ",name[:4])

The result after execution will be like this:

name[:4] = I?

Solution:
First convert it to []rune, then intercept it, transfer to string

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

Running results:

string(nameRune[:4]) =  I'm Hu Ba

OK, understand the principle, let's write a complete Golang Chinese string intercept function

func SubString(str string, begin, length int) string {
  ("Substring =", str)
  rs := []rune(str)
  lth := len(rs)
  ("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
  }
  ("begin=%d, end=%d, lth=%d\n", begin, length, lth)
  return string(rs[begin:end])
}

accomplishGolang Chinese string intercept functionIt can be achieved simply by using golang principle statements