Comment diviser une chaîne en Golang ?
En langage Go, cordes sont différents des autres langues comme Java , C++ , Python , etc. Il s'agit d'une séquence de caractères de largeur variable où chaque caractère est représenté par un ou plusieurs octets à l'aide du codage UTF-8. Dans les chaînes Go, vous êtes autorisé à diviser une chaîne en tranche à l'aide des fonctions suivantes. Ces fonctions sont définies sous le package strings, vous devez donc importer le package strings dans votre programme pour accéder à ces fonctions :
1. Diviser : Cette fonction divise une chaîne en toutes les sous-chaînes séparées par le séparateur donné et renvoie une tranche contenant ces sous-chaînes.
Syntaxe:
func Split(str, sep string) []string
Ici, str est la chaîne et sep est le séparateur. Si str ne contient pas le donné sep et sep n'est pas vide, alors il renverra une tranche de longueur 1 qui contient uniquement str . Ou si le sep est vide, puis il sera divisé après chaque séquence UTF-8. Ou si les deux str et sep sont vides, alors il renverra une tranche vide.
Exemple:
Aller
// 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)> }> |
Sortir:
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. SplitAprès : Cette fonction divise une chaîne en toutes les sous-chaînes après chaque instance du séparateur donné et renvoie une tranche contenant ces sous-chaînes.
Syntaxe:
func SplitAfter(str, sep string) []string
Ici, str est la chaîne et sep est le séparateur. Si str ne contient pas le donné sep et sep n'est pas vide, alors il renverra une tranche de longueur 1 qui contient uniquement str . Ou si le sep est vide, puis il sera divisé après chaque séquence UTF-8. Ou si les deux str et sep sont vides, alors il renverra une tranche vide.
Exemple:
Aller
// 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)> }> |
Sortir:
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. SplitAprèsN : Cette fonction divise une chaîne en toutes les sous-chaînes après chaque instance du séparateur donné et renvoie une tranche contenant ces sous-chaînes.
Syntaxe:
func SplitAfterN(str, sep string, m int) []string
Ici, str est la chaîne, sep est le séparateur et m est utilisé pour trouver le nombre de sous-chaînes à renvoyer. Ici, si m>0 , alors il revient au maximum m les sous-chaînes et la dernière sous-chaîne de chaîne ne seront pas divisées. Si m == 0 , alors il reviendra nul. Si m <0 , alors il renverra toutes les sous-chaînes.
Exemple:
Aller
// 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)> }> |
Sortir:
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: []