在 Go 语言中,字符串与Java等其他语言不同。它是一系列可变宽度字符,其中每个字符都使用UTF-8 编码)由一个或多个字节表示。换句话说,字符串是任意字节(包括具有零值的字节)的不可变链或者字符串是字节的只读切片,并且字符串的字节可以使用 UTF-8 编码在 Unicode 文本中表示。 由于 UTF-8 编码,Golang 字符串可以包含一个文本,它可以表示所有的语言,没有任何限制。
通常,字符串用_双引号括起来””,如下例所示:
例子:package main
import "fmt"
func main() {
My_value_1 := "Welcome to meigui"
var My_value_2 string
My_value_2 = "meigui"
fmt.Println("String 1: ", My_value_1)
fmt.Println("String 2: ", My_value_2)
}
输出:String 1: Welcome to meigui
String 2: meigui
注意:字符串可以为空,但它们不为零。在 Go 语言中,字符串文字以两种不同的方式创建:
\\ | 反斜杠(\) |
\000 | 具有给定 3 位 8 位八进制代码点的 Unicode 字符 |
\’ | 单引号 (‘)。它只允许在字符文字内 |
\” | 双引号 (")。仅允许在解释的字符串文字中使用 |
\一个 | ASCII 响铃 (BEL) |
\b | ASCII 退格 (BS) |
\F | ASCII 换页 (FF) |
\n | ASCII 换行(LF |
\r | ASCII 回车 (CR) |
\t | ASCII 选项卡 (TAB) |
\uhhhh | 具有给定 4 位 16 位十六进制代码点的 Unicode 字符。 |
具有给定 8 位 32 位十六进制代码点的 Unicode 字符。 | |
\v | ASCII 垂直制表符 (VT) |
\xhh | 具有给定 2 位 8 位十六进制代码点的 Unicode 字符。 |
package main
import "fmt"
func main() {
My_value_1 := "Welcome to meigui"
My_value_2 := "Welcome!\nmeigui"
My_value_3 := Hello!meigui
My_value_4 := Hello!\nmeigui
fmt.Println("String 1: ", My_value_1)
fmt.Println("String 2: ", My_value_2)
fmt.Println("String 3: ", My_value_3)
fmt.Println("String 4: ", My_value_4)
}
**输出:**
String 1: Welcome to meigui
String 2: Welcome!
meigui
String 3: Hello!meigui
String 4: Hello!\nmeigui
package main
import "fmt"
func main() {
mystr := "Welcome to meigui"
fmt.Println("String:", mystr)
}
**输出:**
String: Welcome to meigui
对于索引,chr:= range str{ // .. }
这里,index 是存储 UTF-8 编码代码点的第一个字节的变量,_chr_存储给定字符串的字符,str 是一个字符串。
例子:package main
import "fmt"
func main() {
for index, s := range "" {
fmt.Printf("The index number of %c is %d\n", s, index)
}
}
输出: The index number of G is 0
The index number of e is 1
The index number of e is 2
The index number of k is 3
The index number of s is 4
The index number of F is 5
The index number of o is 6
The index number of r is 7
The index number of G is 8
The index number of e is 9
The index number of e is 10
The index number of K is 11
The index number of s is 12
```
* **如何访问字符串的单个字节?**:字符串是一个字节,所以我们可以访问给定字符串的每个字节。
**例子:**
```go
package main
import "fmt"
func main() {
str := "Welcome to meigui"
for c := 0; c < len(str); c++ {
fmt.Printf("\nCharacter = %c Bytes = %v", str, str)
}
}</code></pre>
<pre><code>**输出:**
```
Character = W Bytes = 87
Character = e Bytes = 101
Character = l Bytes = 108
Character = c Bytes = 99
Character = o Bytes = 111
Character = m Bytes = 109
Character = e Bytes = 101
Character = Bytes = 32
Character = t Bytes = 116
Character = o Bytes = 111
Character = Bytes = 32
Character = G Bytes = 71
Character = e Bytes = 101
Character = e Bytes = 101
Character = k Bytes = 107
Character = s Bytes = 115
Character = f Bytes = 102
Character = o Bytes = 111
Character = r Bytes = 114
Character = G Bytes = 71
Character = e Bytes = 101
Character = e Bytes = 101
Character = k Bytes = 107
Character = s Bytes = 115
```
package main
import "fmt"
func main() {
myslice1 := []byte{0x47, 0x65, 0x65, 0x6b, 0x73}
mystring1 := string(myslice1)
fmt.Println("String 1: ", mystring1)
myslice2 := []rune{0x0047, 0x0065, 0x0065,
0x006b, 0x0073}
mystring2 := string(myslice2)
fmt.Println("String 2: ", mystring2)
}
输出: String 1: Geeks
String 2: Geeks
package main
import (
"fmt"
"unicode/utf8"
)
func main() {
mystr := "Welcome to meigui ??????"
length1 := len(mystr)
length2 := utf8.RuneCountInString(mystr)
fmt.Println("string:", mystr)
fmt.Println("Length 1:", length1)
fmt.Println("Length 2:", length2)
}
输出:string: Welcome to meigui ??????
Length 1: 31
Length 2: 31