This is a creation in Article, where the information may have evolved or changed.
First look at the following examples:
Package Main
Import "FMT"
Func Main () {
S: = "I am Chinese"
For i:=0; I < Len (s); i++{
Fmt. Printf ("%c", S[i])
}
Fmt. Printf ("\ n")
For _, V: = range S {
Fmt. Printf ("%c", V)
}
Fmt. Print ("\ n")
}
Output Result:
ææ¯ä¸å½äºº
I am a Chinese
Is there any difference between using Len (s) and range traversal to access string elements?
First, to review the string representation of the go language, the Go language has the following two ways to represent a string:
1, double quotes, such as: "gogogo\n", using the escape character
2, anti-quotes, such as: ' gogogo\n ', do not use the escape character, the contents of the string will be consistent with the assignment strictly
In the go language, there is no character type, Rune is a character type
The strings in the Go language are encoded and stored in UTF-8 format, for example:
S: = "Hello world!" "
The variable s holds the UTF-8 encoding of the string, and when you use the Len (s) function to get the length of the string, the UTF-8 encoded length of the string is obtained. In general, we believe that storing an ASCII character in a computer requires a byte, and storing a non-ASCII character requires two bytes, which is considered only for ANSI coding commonly used in Windows systems, whereas in the Go language, UTF-8 encoding is used, encoded with UTF-8 To store an ASCII character still requires only one byte, and storing a non-ASCII character requires 2, 3, 4 bytes, which is not fixed.
Since the string in Go holds the UTF-8 encoding, what we get with the subscript method using S[0] is a byte in the UTF-8 encoding. For non-ASCII characters, such a byte has no practical meaning unless you want to encode or decode UTF-8 byte stream. In the Go language, there are already many ready-made ways to encode or decode the UTF-8 byte stream.
Iterate through the bytes in the string (accessed using subscript):
Func Main () {
S: = "Hello world!" "
for I, L: = 0, Len (s); I < L; i++ {
FMT. Printf ("%2v =%v\n", I, s[i])//Output single byte value
}
}
Iterate through the characters in the string (accessed using the For range statement):
Func Main () {
S: = "Hello world!" "
for I, V: = range S {//I is the byte position of the character, V is a copy of the character
FMT. Printf ("%2v =%c\n", I, v)//Output single character
}
}
In the Go language, the contents of the string can not be modified, that is, you can not use s[0] this way to modify the UTF-8 encoding in the string, if you must modify, then you could copy the contents of the string into a writable buffer, and then modify. Such buffers are generally []byte or []rune]. If you want to modify the bytes in a string, convert to []byte format, if you want to modify the characters in the string, convert to []rune format, the conversion process will automatically copy the data.]
Modify the bytes in the string (with []byte):
Func Main () {
S: = "Hello world!" "
B: = []byte (s)//convert to []byte, auto copy data
b[5] = ', '//Modify []byte
FMT. Printf ("%s\n", s)//s cannot be modified, content remains unchanged
FMT. Printf ("%s\n", b)//Modified data
}
To modify characters in a string (with []rune):
Func Main () {
S: = "Hello world!" "
r: = []rune (s)//convert to []rune, auto copy data
r[6] = ' Medium '//Modify []rune
r[7] = ' country '//modify []rune
FMT. PRINTLN (s)//s cannot be modified, content remains unchanged
FMT. Println (String (r))//Convert to string, copy data again
}
Handling Rune characters in []byte (requires a decoding function in the UTF8 package)
Func Main () {
B: = []byte ("Hello World! ")
for Len (b) > 0 {
r, N: = UTF8. Decoderune (b)//decode the first character in B
FMT. Printf ("%c\n", r)//display of read-out characters
B = b[n:]//Discard characters that have been read
}
}