Jak podzielić ciąg w Golang?

W języku Go, smyczki różnią się od innych języków, np Jawa , C++ , Pyton itp. Jest to sekwencja znaków o zmiennej szerokości, gdzie każdy znak jest reprezentowany przez jeden lub więcej bajtów przy użyciu kodowania UTF-8. W Go strings możesz podzielić ciąg na plasterki za pomocą następujących funkcji. Funkcje te są zdefiniowane w pakiecie strings, dlatego aby uzyskać dostęp do tych funkcji, musisz zaimportować pakiet strings do swojego programu:
1. Podział: Ta funkcja dzieli ciąg na wszystkie podciągi oddzielone podanym separatorem i zwraca wycinek zawierający te podciągi.
Składnia:

func Split(str, sep string) []string 

Tutaj, ul to ciąg znaków, a sep to separator. Jeśli ul nie zawiera danego wrzesień I wrzesień nie jest pusty, zwróci wycinek o długości 1, który zawiera tylko ul . Lub jeśli wrzesień jest pusty, wówczas zostanie podzielony po każdej sekwencji UTF-8. Lub jeśli oba ul I wrzesień są puste, wówczas zwróci pusty kawałek.
Przykład:

Iść




// 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)> }>

Wyjście:

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. PodzielPo: Ta funkcja dzieli ciąg na wszystkie podciągi po każdym wystąpieniu danego separatora i zwraca wycinek zawierający te podciągi.
Składnia:

func SplitAfter(str, sep string) []string 

Tutaj, ul to ciąg znaków, a sep to separator. Jeśli ul nie zawiera danego wrzesień I wrzesień nie jest pusty, zwróci wycinek o długości 1, który zawiera tylko ul . Lub jeśli wrzesień jest pusty, wówczas zostanie podzielony po każdej sekwencji UTF-8. Lub jeśli oba ul I wrzesień są puste, wówczas zwróci pusty kawałek.
Przykład:

Iść




// 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)> }>

Wyjście:

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. PodzielPoN: Ta funkcja dzieli ciąg na wszystkie podciągi po każdym wystąpieniu danego separatora i zwraca wycinek zawierający te podciągi.
Składnia:

func SplitAfterN(str, sep string, m int) []string 

Tutaj, ul jest ciągiem, wrzesień jest separatorem, a m służy do znalezienia liczby podciągów do zwrócenia. Tutaj, jeśli m>0 , wtedy co najwyżej powraca M podciągi, a ostatni podciąg nie zostanie podzielony. Jeśli m == 0 , wtedy zwróci zero. Jeśli m <0 , wówczas zwróci wszystkie podciągi.
Przykład:

Iść




// 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)> }>

Wyjście:

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: []