Golang 字符串
在Go语言中,字符串与其他语言如Java、C++、Python等不同。它是一个可变宽度的字符序列,每个字符都由一个或多个使用UTF-8编码的字节表示。或者换句话说,字符串是不可改变的任意字节链(包括零值的字节),或者说 字符串是一个只读的字节片 ,字符串的字节可以用UTF-8编码来表示Unicode文本。
由于UTF-8编码,Golang字符串可以包含世界上任何语言的混合文本,没有任何混乱和页面的限制。一般来说,字符串是用双引号””括起来的,如下面的例子所示。
例子
// Go program to illustrate
// how to create strings
package main
import "fmt"
func main() {
// Creating and initializing a
// variable with a string
// Using shorthand declaration
My_value_1 := "Welcome to GeeksforGeeks"
// Using var keyword
var My_value_2 string
My_value_2 = "GeeksforGeeks"
// Displaying strings
fmt.Println("String 1: ", My_value_1)
fmt.Println("String 2: ", My_value_2)
}
输出
String 1: Welcome to GeeksforGeeks
String 2: GeeksforGeeks
注意: 字符串可以是空的,但它们不是nil。
字符串字元
在Go语言中,字符串字面意义的创建有两种不同的方式。
- 使用双引号(“”):这里,使用双引号(“”)创建字符串字面。这种类型的字符串支持转义字符,如下表所示,但不跨越多行。这种类型的字符串字面意义在Golang程序中被广泛使用。
Escape character | Description |
---|---|
\ | 反斜线( |
\000 | Unicode字符,具有给定的3位8位八进制代码点 |
\” | 单引号(’)。它只允许在字符字面内使用 |
\” | 双引号(”)。只允许在解释的字符串字面内使用 |
\a | ASCII铃铛(BEL)。 |
\b | ASCII退格符 (BS) |
\f | ASCII formfeed (FF) |
\n | ASCII换行符 (LF) |
\r | ASCII 回车 (CR) |
\t | ASCII tab (TAB) |
\uhhhh | Unicode字符,具有给定的4位16位十六进制代码点。 |
具有给定的8位32位十六进制代码点的Unicode字符。 | |
\v | ASCII垂直制表符 (VT) |
\xhh | 具有给定的2位8位十六进制代码点的Unicode字符。 |
- 使用backticks(“):这里,字符串字头是用backticks(“)创建的,也被称为raw literals。原始字元不支持转义字符,可以跨越多行,并且可以包含除回车键之外的任何字符。一般来说,它用于编写多行信息,在正则表达式和HTML中使用。
示例:
// Go program to illustrate string literals
package main
import "fmt"
func main() {
// Creating and initializing a
// variable with a string literal
// Using double-quote
My_value_1 := "Welcome to GeeksforGeeks"
// Adding escape character
My_value_2 := "Welcome!\nGeeksforGeeks"
// Using backticks
My_value_3 := `Hello!GeeksforGeeks`
// Adding escape character
// in raw literals
My_value_4 := `Hello!\nGeeksforGeeks`
// Displaying strings
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 GeeksforGeeks
String 2: Welcome!
GeeksforGeeks
String 3: Hello!GeeksforGeeks
String 4: Hello!\nGeeksforGeeks
关于字符串的重要观点
- 字符串是不可变的:在Go语言中,字符串是不可变的,一旦一个字符串被创建,该字符串的值就不能被改变。或者换句话说,字符串是只读的。如果你试图改变,那么编译器会抛出一个错误。
示例:
// Go program to illustrate
// string are immutable
package main
import "fmt"
// Main function
func main() {
// Creating and initializing a string
// using shorthand declaration
mystr := "Welcome to GeeksforGeeks"
fmt.Println("String:", mystr)
/* if you trying to change
the value of the string
then the compiler will
throw an error, i.e,
cannot assign to mystr[1]
mystr[1]= 'G'
fmt.Println("String:", mystr)
*/
}
输出:
String: Welcome to GeeksforGeeks
- 如何迭代一个字符串?你可以使用for rang循环迭代字符串。这个循环可以遍历一个字符串的Unicode代码点。
语法:
for index, chr:= range str{
// Statement..
}
这里,index是存储UTF-8编码的第一个字节的变量,chr存储给定字符串的字符,str是一个字符串。
示例:
// Go program to illustrate how
// to iterate over the string
// using for range loop
package main
import "fmt"
// Main function
func main() {
// String as a range in the for loop
for index, s := range "GeeksForGeeKs" {
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 program to illustrate how to
// access the bytes of the string
package main
import "fmt"
// Main function
func main() {
// Creating and initializing a string
str := "Welcome to GeeksforGeeks"
// Accessing the bytes of the given string
for c := 0; c < len(str); c++ {
fmt.Printf("\nCharacter = %c Bytes = %v", str, str)
}
}
输出:
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
- 如何从分片中创建一个字符串?在Go语言中,你可以从字节的分片中创建一个字符串。
示例:
// Go program to illustrate how to
// create a string from the slice
package main
import "fmt"
// Main function
func main() {
// Creating and initializing a slice of byte
myslice1 := []byte{0x47, 0x65, 0x65, 0x6b, 0x73}
// Creating a string from the slice
mystring1 := string(myslice1)
// Displaying the string
fmt.Println("String 1: ", mystring1)
// Creating and initializing a slice of rune
myslice2 := []rune{0x0047, 0x0065, 0x0065,
0x006b, 0x0073}
// Creating a string from the slice
mystring2 := string(myslice2)
// Displaying the string
fmt.Println("String 2: ", mystring2)
}
输出:
String 1: Geeks
String 2: Geeks
- 如何找到字符串的长度?在Golang字符串中,你可以使用两个函数找到字符串的长度,一个是len(),另一个是RuneCountInString()。RuneCountInString()函数是由UTF-8包提供的,这个函数返回字符串中符文的总数。而len()函数返回字符串的字节数。
示例:
// Go program to illustrate how to
// find the length of the string
package main
import (
"fmt"
"unicode/utf8"
)
// Main function
func main() {
// Creating and initializing a string
// using shorthand declaration
mystr := "Welcome to GeeksforGeeks ??????"
// Finding the length of the string
// Using len() function
length1 := len(mystr)
// Using RuneCountInString() function
length2 := utf8.RuneCountInString(mystr)
// Displaying the length of the string
fmt.Println("string:", mystr)
fmt.Println("Length 1:", length1)
fmt.Println("Length 2:", length2)
}
输出:
string: Welcome to GeeksforGeeks ??????
Length 1: 31
Length 2: 31