English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Nel linguaggio Go,StringaDiverso da Java, C++, Python e altri linguaggi. È una serie di caratteri a larghezza variabile, ognuno dei quali è rappresentato da uno o più byte codificati in UTF-8. Nei stringhe Go, è possibile utilizzare le seguenti funzioni per dividere una stringa in una slice. Queste funzioni sono definite nel pacchetto stringa, quindi è necessario importare il pacchetto stringa nel programma per accedere a queste funzioni:
1.Split:Questa funzione divide la stringa in tutte le sottostringhe separate dal carattere di separazione specificato e restituisce un'intera lista di queste sottostringhe.
Sintassi:
func Split(str string, sep string) []string
qui, str è una stringa, sep è il carattere di separazione. Se str non contiene il sep specificato e sep non è vuoto, restituirà un'intera lista di lunghezza 1 contenente solo str. O se sep è vuoto, dividerà dopo ogni sequenza UTF-8. O se sia str che sep sono vuoti, restituirà un'intera lista vuota.
package main import ( "fmt" "strings" ) func main() { //Crea e inizializza le stringhe str1 := "Welcome, to the, online portal, of w3codebox" str2 := "My dog name is Dollar" str3 := "I like to play Ludo" //Mostra le stringhe fmt.Println("Stringa 1: ", str1) fmt.Println("Stringa 2: ", str2) fmt.Println("Stringa 3: ", str3) //Dividi la stringa data //Utilizzo della funzione Split() res1 := strings.Split(str1, ",") res2 := strings.Split(str2, "") res3 := strings.Split(str3, "!") res4 := strings.Split("", "w3codebox, geeks") //Mostra il risultato fmt.Println("\nRisultato 1: ", res1) fmt.Println("Risultato 2: ", res2) fmt.Println("Risultato 3: ", res3) fmt.Println("Risultato 4: ", res4) }
Output:
Stringa 1: [Benvenuti, nel portale online di w3codebox] Stringa 2: [Il nome del mio cane è Dollar] Stringa 3: [Mi piace giocare a Ludo] Risultato 1: [Welcome to the online portal of w3codebox] Risultato 2: [M y d o g n a m e i s D o l l a r] Risultato 3: [Mi piace giocare a Ludo] Risultato 4: []
2. SplitAfter:Questa funzione divide la stringa dopo ogni istanza del carattere di separazione specificato, restituendo un'intera lista di sottostringhe.
Sintassi:
func SplitAfter(str string, sep string) []string
qui, str è una stringa, sep è il carattere di separazione. Se str non contiene il sep specificato e sep non è vuoto, restituirà un'intera lista di lunghezza 1 contenente solo str. O se sep è vuoto, dividerà dopo ogni sequenza UTF-8. O se sia str che sep sono vuoti, restituirà un'intera lista vuota.
package main import ( "fmt" "strings" ) func main() { //Crea e inizializza le stringhe str1 := "Welcome, to the, online portal, of w3codebox" str2 := "My dog name is Dollar" str3 := "I like to play Ludo" //Mostra le stringhe fmt.Println("Stringa 1: ", str1) fmt.Println("Stringa 2: ", str2) fmt.Println("Stringa 3: ", str3) //Dividi la stringa data //Utilizzo della funzione SplitAfter() res1 := strings.SplitAfter(str1, ",") res2 := strings.SplitAfter(str2, "") res3 := strings.SplitAfter(str3, "!") res4 := strings.SplitAfter("", "w3codebox, geeks") //Mostra i risultati fmt.Println("\nRisultato 1: ", res1) fmt.Println("Risultato 2: ", res2) fmt.Println("Risultato 3: ", res3) fmt.Println("Risultato 4: ", res4) }
Output:
Stringa 1: [Benvenuti, nel portale online di w3codebox] Stringa 2: [Il nome del mio cane è Dollar] Stringa 3: [Mi piace giocare a Ludo] Risultato 1: [Welcome, to the, online portal, of w3codebox] Risultato 2: [M y d o g n a m e i s D o l l a r] Risultato 3: [Mi piace giocare a Ludo]
3. SplitAfterN:Questa funzione divide la stringa dopo ogni istanza del carattere di separazione specificato, restituendo un'intera lista di sottostringhe.
Sintassi:
func SplitAfterN(str string, sep string, m int) []string
qui,strè una stringa,sepè il carattere di separazione, m viene utilizzato per trovare il numero di sottostringhe da restituire. Qui, sem> 0,allineamento massimo restituitomsottostringhe e l'ultima sottostringa non verrà divisa. Sem == 0se m == 0, allora restituirà nil. Sem < 0se m < 0, allora restituirà tutte le sottostringhe.
package main import ( "fmt" "strings" ) func main() { //Crea e inizializza le stringhe str1 := "Welcome, to the, online portal, of w3codebox" str2 := "My dog name is Dollar" str3 := "I like to play Ludo" //Mostra le stringhe fmt.Println("Stringa 1: ", str1) fmt.Println("Stringa 2: ", str2) fmt.Println("Stringa 3: ", str3) //Dividi la stringa data //Usa la funzione SplitAfterN() res1 := strings.SplitAfterN(str1, ",", 2) res2 := strings.SplitAfterN(str2, "", 4) res3 := strings.SplitAfterN(str3, "!", 1) res4 := strings.SplitAfterN("", "w3codebox, geeks", 3) //Mostra i risultati fmt.Println("\nRisultato 1: ", res1) fmt.Println("Risultato 2: ", res2) fmt.Println("Risultato 3: ", res3) fmt.Println("Risultato 4: ", res4) }
Output:
Stringa 1: [Benvenuti, nel portale online di w3codebox] Stringa 2: [Il nome del mio cane è Dollar] Stringa 3: [Mi piace giocare a Ludo] Risultato 1: [Benvenuti, nel portale online di w3codebox] Risultato 2: [Il nome del mio cane è Dollar] Risultato 3: [Mi piace giocare a Ludo] Risultato 4: []