In Go-taal, snaren zijn anders dan andere talen zoals Java , C++ , Python , enz. Het is een reeks tekens met variabele breedte waarbij elk teken wordt weergegeven door een of meer bytes met behulp van UTF-8-codering. In Go strings kun je een string in een segment splitsen met behulp van de volgende functies. Deze functies worden gedefinieerd onder het strings-pakket, dus u moet het strings-pakket in uw programma importeren om toegang te krijgen tot deze functies:
1. Splitsen: Deze functie splitst een tekenreeks op in alle subtekenreeksen, gescheiden door het opgegeven scheidingsteken, en retourneert een segment dat deze subtekenreeksen bevat.
Syntaxis:
func Split(str, sep string) []string>
Hier, str is de tekenreeks en sep is het scheidingsteken. Als str bevat niet het gegeven sep En sep niet leeg is, retourneert het een segment met lengte 1 dat alleen bevat str . Of als de sep leeg is, wordt het na elke UTF-8-reeks gesplitst. Of als beide str En sep leeg zijn, retourneert het een leeg segment.
Voorbeeld:
Gaan
// 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)> }> |
>
annotaties in springlaars
>
Uitgang:
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. Splitsen na: Deze functie splitst een tekenreeks op in alle subtekenreeksen na elke instantie van het opgegeven scheidingsteken en retourneert een segment dat deze subtekenreeksen bevat.
Syntaxis:
func SplitAfter(str, sep string) []string>
Hier, str is de tekenreeks en sep is het scheidingsteken. Als str bevat niet het gegeven sep En sep niet leeg is, retourneert het een segment met lengte 1 dat alleen bevat str . Of als de sep leeg is, wordt het na elke UTF-8-reeks gesplitst. Of als beide str En sep leeg zijn, retourneert het een leeg segment.
Voorbeeld:
Gaan
// 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)> }> |
>
>
Uitgang:
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. SplitsenAfterN: Deze functie splitst een tekenreeks op in alle subtekenreeksen na elke instantie van het opgegeven scheidingsteken en retourneert een segment dat deze subtekenreeksen bevat.
Syntaxis:
func SplitAfterN(str, sep string, m int) []string>
Hier, str is de string, sep is het scheidingsteken, en m wordt gebruikt om het aantal subtekenreeksen te vinden dat moet worden geretourneerd. Hier, als m>0 , dan komt het hoogstens terug M substrings en de laatste string-substring wordt niet gesplitst. Als m == 0 , dan zal het nul opleveren. Als m<0 , dan worden alle subtekenreeksen geretourneerd.
Voorbeeld:
Gaan
// 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)> }> |
js globale variabele
>
>
Uitgang:
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: []>