Nella lingua Go, stringhe sono diversi da altre lingue come Giava , C++ , Pitone , ecc. È una sequenza di caratteri a larghezza variabile in cui ogni carattere è rappresentato da uno o più byte utilizzando la codifica UTF-8. Nelle stringhe Go, puoi dividere una stringa in una sezione con l'aiuto delle seguenti funzioni. Queste funzioni sono definite nel pacchetto strings, quindi devi importare il pacchetto strings nel tuo programma per accedere a queste funzioni:
1. Divisione: Questa funzione divide una stringa in tutte le sottostringhe separate dal separatore specificato e restituisce una sezione che contiene queste sottostringhe.
Sintassi:
func Split(str, sep string) []string>
Qui, stra è la stringa e sep è il separatore. Se stra non contiene il dato sett E sett non è vuoto, restituirà una sezione di lunghezza 1 che contiene solo stra . O se il sett è vuoto, verrà diviso dopo ogni sequenza UTF-8. O se entrambi stra E sett sono vuoti, verrà restituita una porzione vuota.
Esempio:
Andare
// Go program to illustrate how to split a string> package> main> import> (> >'fmt'> >'strings'> )> // Main function> func> main() {> >// Creating and initializing the strings> >str1 :=>'Welcome, to the, online portal, of techcodeview.com'> >str2 :=>'My dog name is Dollar'> >str3 :=>'I like to play Ludo'> >// Displaying strings> >fmt.Println(>'String 1: '>, str1)> >fmt.Println(>'String 2: '>, str2)> >fmt.Println(>'String 3: '>, str3)> >// Splitting the given strings> >// Using Split() function> >res1 := strings.Split(str1,>','>)> >res2 := strings.Split(str2,>''>)> >res3 := strings.Split(str3,>'!'>)> >res4 := strings.Split(>''>,>'techcodeview.com, geeks'>)> >// Displaying the result> >fmt.Println(>'
Result 1: '>, res1)> >fmt.Println(>'Result 2: '>, res2)> >fmt.Println(>'Result 3: '>, res3)> >fmt.Println(>'Result 4: '>, res4)> }> |
>
file aperto in Java
>
Produzione:
String 1: Welcome, to the, online portal, of techcodeview.com String 2: My dog name is Dollar String 3: I like to play Ludo Result 1: [Welcome to the online portal of techcodeview.com] Result 2: [M y d o g n a m e i s D o l l a r] Result 3: [I like to play Ludo] Result 4: []>
2. Dividi dopo: Questa funzione divide una stringa in tutte le sottostringhe dopo ogni istanza del separatore specificato e restituisce una sezione che contiene queste sottostringhe.
Sintassi:
func SplitAfter(str, sep string) []string>
Qui, stra è la stringa e sep è il separatore. Se stra non contiene il dato sett E sett non è vuoto, restituirà una sezione di lunghezza 1 che contiene solo stra . O se il sett è vuoto, verrà diviso dopo ogni sequenza UTF-8. O se entrambi stra E sett sono vuoti, verrà restituita una porzione vuota.
Esempio:
Andare
// Go program to illustrate how to split a string> package> main> import> (> >'fmt'> >'strings'> )> // Main function> func> main() {> >// Creating and initializing the strings> >str1 :=>'Welcome, to the, online portal, of techcodeview.com'> >str2 :=>'My dog name is Dollar'> >str3 :=>'I like to play Ludo'> >// Displaying strings> >fmt.Println(>'String 1: '>, str1)> >fmt.Println(>'String 2: '>, str2)> >fmt.Println(>'String 3: '>, str3)> >// Splitting the given strings> >// Using SplitAfter() function> >res1 := strings.SplitAfter(str1,>','>)> >res2 := strings.SplitAfter(str2,>''>)> >res3 := strings.SplitAfter(str3,>'!'>)> >res4 := strings.SplitAfter(>''>,>'techcodeview.com, geeks'>)> >// Displaying the result> >fmt.Println(>'
Result 1: '>, res1)> >fmt.Println(>'Result 2: '>, res2)> >fmt.Println(>'Result 3: '>, res3)> >fmt.Println(>'Result 4: '>, res4)> }> |
come ottenere emoji Apple su Android
>
>
Produzione:
String 1: Welcome, to the, online portal, of techcodeview.com String 2: My dog name is Dollar String 3: I like to play Ludo Result 1: [Welcome, to the, online portal, of techcodeview.com] Result 2: [M y d o g n a m e i s D o l l a r] Result 3: [I like to play Ludo] Result 4: []>
3. DividiDopoN: Questa funzione divide una stringa in tutte le sottostringhe dopo ogni istanza del separatore specificato e restituisce una sezione che contiene queste sottostringhe.
Sintassi:
func SplitAfterN(str, sep string, m int) []string>
Qui, stra è la corda, sett è il separatore e m viene utilizzato per trovare il numero di sottostringhe da restituire. Ecco, se m>0 , allora ritorna al massimo M le sottostringhe e l'ultima sottostringa della stringa non verranno divise. Se m == 0 , quindi restituirà zero. Se m<0 , restituirà tutte le sottostringhe.
Esempio:
Andare
// Go program to illustrate how to split a string> package> main> import> (> >'fmt'> >'strings'> )> // Main function> func> main() {> >// Creating and initializing the strings> >str1 :=>'Welcome, to the, online portal, of techcodeview.com'> >str2 :=>'My dog name is Dollar'> >str3 :=>'I like to play Ludo'> >// Displaying strings> >fmt.Println(>'String 1: '>, str1)> >fmt.Println(>'String 2: '>, str2)> >fmt.Println(>'String 3: '>, str3)> >// Splitting the given strings> >// Using SplitAfterN() function> >res1 := strings.SplitAfterN(str1,>','>,>2>)> >res2 := strings.SplitAfterN(str2,>''>,>4>)> >res3 := strings.SplitAfterN(str3,>'!'>,>1>)> >res4 := strings.SplitAfterN(>''>,>'techcodeview.com, geeks'>,>3>)> >// Displaying the result> >fmt.Println(>'
Result 1: '>, res1)> >fmt.Println(>'Result 2: '>, res2)> >fmt.Println(>'Result 3: '>, res3)> >fmt.Println(>'Result 4: '>, res4)> }> |
>
>
Produzione:
String 1: Welcome, to the, online portal, of techcodeview.com String 2: My dog name is Dollar String 3: I like to play Ludo Result 1: [Welcome, to the, online portal, of techcodeview.com] Result 2: [M y dog name is Dollar] Result 3: [I like to play Ludo] Result 4: []>