English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In Go, le stringhe sono diverse dagli altri linguaggi, come Java, C++, Python, ecc. È una sequenza di caratteri variabili, in cui ogni carattere è rappresentato da uno o più byte codificati in UTF-8. O, in altre parole, una stringa è una catena immutabile di byte (inclusi i byte con valore zero) o una sezione di byte sola lettura, in cui i byte possono essere codificati in UTF-8 per rappresentare il testo Unicode.
Poiché utilizza la codifica UTF-8, le stringhe Go possono contenere testi, testi che sono una mescolanza di qualsiasi lingua del mondo, senza causare confusione e limitazioni sulla pagina. Di solito, le stringhe vengono utilizzateVirgolette doppi””Citare,come mostrato negli esempi seguenti:
//Come creare una stringa package main import "fmt" func main() { //创建并初始化 //Variabile con stringa //使用简写声明 My_value_1 := "Welcome to oldtoolbag.com" //Utilizzare la parola chiave var var My_value_2 string My_value_2 = "w3codebox" //显示字符串 fmt.Println("String 1: ", My_value_1) fmt.Println("String 2: ", My_value_2) }
输出:
Stringa 1: Welcome to oldtoolbag.com Stringa 2: w3codebox
Attenzione:Una stringa può essere vuota, ma non può essere nil.
In Go, le stringhe letterali sono create in due modi diversi:
Utilizzare virgolette doppi (“”):qui, string literal è stato creato utilizzando virgolette doppi("")。Questo tipo di string literal supporta i caratteri di escape, come indicato nella tabella sottostante, ma non può essere multilinea. Questo tipo di string literal viene ampiamente utilizzato nei programmi Go.
转义符 | 描述 |
---|---|
\\ | 反斜杠(\) |
\000 | 具有给定的3位8位八进制代码点的Unicode字符 |
\' | 单引号(')。仅允许在字符文字中使用 |
\" | 双引号("")。仅允许在解释的字符串文字中使用 |
\a | 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字符。 |
使用反引号(``):此处,字符串文字是使用反引号(``)创建的,也称为raw literals(原始文本)。原始文本不支持转义字符,可以跨越多行,并且可以包含除反引号之外的任何字符。通常,它用于在正则表达式和HTML中编写多行消息。
package main import "fmt" func main() { //创建并初始化 //带有字符串文字的变量 //使用双引号 My_value_1 := "Welcome to w3codebox" //添加转义字符 My_value_2 := "Welcome!\nw3codebox" //使用反引号 My_value_3 := `Hello!w3codebox` //添加转义字符 //原始文本 My_value_4 := `Hello! w3codebox` //显示 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 w3codebox String 2: Welcome! w3codebox String 3: Hello!w3codebox Stringa 4: Hello! w3codebox
Le stringhe sono immutabili:Nel linguaggio Go, una volta creata una stringa, questa è immutabile e non è possibile modificare il valore della stringa. In altre parole, la stringa è readonly. Se si tenta di modificarla, il compilatore genererà un errore.
//Le stringhe sono immutabili package main import "fmt" func main() { //创建和初始化字符串 //使用简写声明 mystr := "Welcome to w3codebox" fmt.Println("Stringa:", mystr) /* Se tenti di modificare il valore della stringa, il compilatore genererà un errore, ad esempio, cannot assign to mystr[1] mystr[1] = 'G' fmt.Println("Stringa:", mystr) */ }
输出:
Stringa: Welcome to w3codebox
Come esplorare una stringa? :Puoi esplorare una stringa utilizzando un ciclo for range. Questo ciclo può iterare su un punto di codice Unicode di una stringa.
Sintassi:
for index, chr := range str { //Statamento... }
In questo caso, l'indice è la variabile che memorizza il primo byte del codice di encoding UTF-8 echr èLa variabile che memorizza i caratteri della stringa data, str è una stringa.
//Esegui la scansione della stringa //Usa il ciclo for range package main import "fmt" func main() { //La stringa come intervallo in un ciclo for for index, s := range "w3codebox" { fmt.Printf("%c Indice è %d\n", s, index) } }
输出:
L'indice è 0 L'indice è 1 L'indice è 2 L'indice è 3 L'indice è 4
Come accedere a un singolo byte di una stringa?Una stringa è un byte, quindi possiamo accedere a ogni byte della stringa data.
//Accedi ai byte della stringa package main import "fmt" func main() { //Crea e inizializza una stringa str := "Welcome to w3codebox" //Accedi ai byte della stringa data for c := 0; c < len(str); c++ { fmt.Printf("\nCarattere = %c Byte = %v", str[c], str[c]) } }
输出:
Carattere = W Byte = 87 Carattere = e Byte = 101 Carattere = l Byte = 108 Carattere = c Byte = 99 字符 = o 字节 = 111 Carattere = m Byte = 109 Carattere = e Byte = 101 Carattere = Byte = 32 Carattere = t Byte = 116 字符 = o 字节 = 111 Carattere = Byte = 32 Carattere = n Byte = 110 Carattere = h Byte = 104 字符 = o 字节 = 111 字符 = o 字节 = 111 字符 = o 字节 = 111
如何从切片创建字符串?:在Go语言中,允许您从字节切片创建字符串。
//从切片创建一个字符串 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
如何查找字符串的长度?:在Golang字符串中,可以使用两个函数(一个是len(),另一个是RuneCountInString())来找到字符串的长度。UTF-8包提供了RuneCountInString()函数,该函数返回字符串中存在的符文总数。len()函数返回字符串的字节数。
//查找字符串的长度 package main import ( "fmt" "unicode/utf8" ) func main() { //创建和初始化字符串 //使用简写声明 mystr := "Welcome to w3codebox ????? //查找字符串的长度 //使用len()函数 length1 := len(mystr) //使用RuneCountInString()函数 length2 := utf8.RuneCountInString(mystr) //显示字符串的长度 fmt.Println("string:", mystr) fmt.Println("Length 1:", length1) fmt.Println("Length 2:", length2) }
输出:
string: Welcome to w3codebox ????? Length 1: 31 Lunghezza 2: 31