Jak rozdělit strunu v Golangu?

V jazyce Go struny se liší od jiných jazyků jako Jáva , C++ , Krajta , atd. Jedná se o sekvenci znaků s proměnnou šířkou, kde každý znak je reprezentován jedním nebo více bajty pomocí kódování UTF-8. V řetězcích Go máte povoleno rozdělit řetězec na řez pomocí následujících funkcí. Tyto funkce jsou definovány v balíčku řetězců, takže pro přístup k těmto funkcím musíte balíček řetězců importovat do svého programu:
1. Rozdělení: Tato funkce rozdělí řetězec na všechny podřetězce oddělené daným oddělovačem a vrátí řez, který tyto podřetězce obsahuje.
Syntax:

func Split(str, sep string) []string 

Tady, str je řetězec a sep je oddělovač. Li str neobsahuje dané září a září není prázdná, vrátí řez o délce 1, který obsahuje pouze str . Nebo pokud září je prázdný, pak se po každé sekvenci UTF-8 rozdělí. Nebo jestli obojí str a září jsou prázdné, vrátí prázdný řez.
Příklad:

Jít




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

Výstup:

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. RozdělitPo: Tato funkce rozdělí řetězec na všechny podřetězce po každé instanci daného oddělovače a vrátí řez, který obsahuje tyto podřetězce.
Syntax:

func SplitAfter(str, sep string) []string 

Tady, str je řetězec a sep je oddělovač. Li str neobsahuje dané září a září není prázdná, vrátí řez o délce 1, který obsahuje pouze str . Nebo pokud září je prázdný, pak se po každé sekvenci UTF-8 rozdělí. Nebo jestli obojí str a září jsou prázdné, vrátí prázdný řez.
Příklad:

Jít




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

Výstup:

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: Tato funkce rozdělí řetězec na všechny podřetězce po každé instanci daného oddělovače a vrátí řez, který obsahuje tyto podřetězce.
Syntax:

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

Tady, str je struna, září je oddělovač a m se používá k nalezení počtu podřetězců, které mají být vráceny. Zde, pokud m>0 , pak se maximálně vrátí m podřetězce a poslední podřetězec se nerozdělí. Li m == 0 , pak se vrátí nula. Li m <0 , pak vrátí všechny podřetězce.
Příklad:

Jít




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

Výstup:

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