The article directories

Go Intercepting a string 1. Intercepting a common English string 2. Intercepts a string with Chinese characters


Go intercepts a string

When you want to intercept a portion of a string, you can do so as if you want to intercept a portion of an array


1. Intercept common English strings

Example:

str := "XHelloWorldX"
content := str[1 : len(str)- 1]
fmt.Println(content)
Copy the code

The result is HelloWorld


2. Intercept the character string containing Chinese characters

A Chinese character must be more than one byte, and I have to go through every byte to determine the encoding, that would be too much trouble. We don’t have to worry about that much. There is another type, rune, besides byte, that doesn’t have to worry about Unicode bytes at all.

Example:

str := "A Chinese CD"
str = string([]rune(str)[:4])
fmt.Println(str)
Copy the code

The result is as follows: a Chinese C