Hvordan deler man en streng i Golang?
På Go-sprog, strenge er anderledes end andre sprog som f.eks Java , C++ , Python osv. Det er en sekvens af tegn med variabel bredde, hvor hvert eneste tegn er repræsenteret af en eller flere bytes ved hjælp af UTF-8-kodning. I Go-strenge har du lov til at opdele en streng i en skive ved hjælp af følgende funktioner. Disse funktioner er defineret under strenge-pakken, så du skal importere streng-pakke i dit program for at få adgang til disse funktioner:
1. Opdel: Denne funktion opdeler en streng i alle understrenge adskilt af den givne separator og returnerer et udsnit, der indeholder disse understrenge.
Syntaks:
func Split(str, sep string) []string
Her, str er strengen og sep er separatoren. Hvis str indeholder ikke det givne sep og sep er ikke-tom, vil den returnere en skive med længde 1, som kun indeholder str . Eller hvis sep er tom, så opdeles den efter hver UTF-8-sekvens. Eller hvis begge dele str og sep er tomme, så returnerer den en tom skive.
Eksempel:
Gå
// 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)> }> |
Produktion:
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. SplitAfter: Denne funktion opdeler en streng i alle understrenge efter hver forekomst af den givne separator og returnerer et udsnit, der indeholder disse understrenge.
Syntaks:
func SplitAfter(str, sep string) []string
Her, str er strengen og sep er separatoren. Hvis str indeholder ikke det givne sep og sep er ikke-tom, vil den returnere en skive med længde 1, som kun indeholder str . Eller hvis sep er tom, så opdeles den efter hver UTF-8-sekvens. Eller hvis begge dele str og sep er tomme, så returnerer den en tom skive.
Eksempel:
Gå
// 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)> }> |
Produktion:
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. SplitAfterN: Denne funktion opdeler en streng i alle understrenge efter hver forekomst af den givne separator og returnerer et udsnit, der indeholder disse understrenge.
Syntaks:
func SplitAfterN(str, sep string, m int) []string
Her, str er strengen, sep er separatoren, og m bruges til at finde antallet af understrenge, der skal returneres. Her, hvis m>0 , så vender den højst tilbage m understrenge, og den sidste streng understreng vil ikke opdeles. Hvis m == 0 , så vil det returnere nul. Hvis m <0 , så returnerer den alle understrenge.
Eksempel:
Gå
// 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)> }> |
Produktion:
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: []