Reproduce the problem: Modify the string first Chinese
First make a slice of the original string, and then spliced to obtain a new string
func ModifyString(str string) string { tempStr := str[1:] str = "大" + tempStr return str } func main(){ ret := ModifyString("你好世界") fmt.Println(ret) }
running result:
Great world
Sections are cut according to the default byte, 3 byte in Chinese composition, resulting in the residue of the above two extra byte
Solution:
So when specified under sections 3 byte try:
func ModifyString(str string) string { tempStr := str[3:] str = "大" + tempStr return str }
running result:
Great world
Another way:
step:
- Becomes first string array of characters
- Then by changing the index value corresponding to an array
- The array is then transferred to a new string
func ModifyString(str string) string { strArray := []rune(str) strArray[0] = '大' str = string(strArray) return str }
running result:
Great world