English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In the Go language,stringDifferent from Java, C ++, Python, and other languages. It is a series of variable-width characters, where each character is represented by UTF-8Encoding is represented by one or more bytes. In Go strings, you can use the following functions to split a string into a slice. These functions are defined under the string package, so you must import the string package in your program to access these functions:
1.Split:This function splits a string into all substrings separated by the given delimiter and returns a slice containing these substrings.
Syntax:
func Split(str, sep string) []string
Here, str is a string, sep is the delimiter. If str does not contain the given sep and sep is not empty, it will return a slice of length1slice containing only str. Or, if sep is empty, it will split at each UTF-8to split the sequence. Or, if both str and sep are empty, it will return an empty slice.
package main import ( "fmt" "strings" ) func main() { //Create and initialize strings str1 := "Welcome, to the, online portal, of w3codebox" str2 := "My dog name is Dollar" str3 := "I like to play Ludo" //Display string fmt.Println("String 1: "str1) fmt.Println("String 2: "str2) fmt.Println("String 3: "str3) //Split the given string //Using the Split() function res1 := strings.Split(str1, ",") res2 := strings.Split(str2, "") res3 := strings.Split(str3, "!") res4 := strings.Split("", "w3codebox, geeks" // Display result fmt.Println("\nResult 1: "res1) fmt.Println("Result 2: "res2) fmt.Println("Result 3: "res3) fmt.Println("Result 4: "res4) }
Output:
String 1: Welcome, to the, online portal, of w3codebox String 2: My dog name is Dollar String 3: I like to play Ludo Result 1: [Welcome to the online portal of w3codebox] Result 2: "My dog name is Dollar" Result 3: I like to play Ludo Result 4: []
2. SplitAfter:This function splits the string after each instance of the given separator into all substrings and returns a slice containing these substrings.
Syntax:
func SplitAfter(str, sep string) []string
Here, str is a string, sep is the delimiter. If str does not contain the given sep and sep is not empty, it will return a slice of length1slice containing only str. Or, if sep is empty, it will split at each UTF-8to split the sequence. Or, if both str and sep are empty, it will return an empty slice.
package main import ( "fmt" "strings" ) func main() { //Create and initialize strings str1 := "Welcome, to the, online portal, of w3codebox" str2 := "My dog name is Dollar" str3 := "I like to play Ludo" //Display string fmt.Println("String 1: "str1) fmt.Println("String 2: "str2) fmt.Println("String 3: "str3) //Split the given string //Using the SplitAfter() function res1 := strings.SplitAfter(str1, ",") res2 := strings.SplitAfter(str2, "") res3 := strings.SplitAfter(str3, "!") res4 := strings.SplitAfter("", "w3codebox, geeks" //Display result fmt.Println("\nResult 1: "res1) fmt.Println("Result 2: "res2) fmt.Println("Result 3: "res3) fmt.Println("Result 4: "res4) }
Output:
String 1: Welcome, to the, online portal, of w3codebox String 2: My dog name is Dollar String 3: I like to play Ludo Result 1: "Welcome, to the, online portal, of w"3codebox] Result 2: "My dog name is Dollar" Result 3: I like to play Ludo
3. SplitAfterN:This function splits the string after each instance of the given separator into all substrings and returns a slice containing these substrings.
Syntax:
func SplitAfterN(str, sep string, m int) []string
here,stris a string,sepis the separator, m is used to find the number of substrings to return. Here, ifm > 0then it will return at mostmsubstrings, and the last string substring will not be split. Ifm == 0it will return nil. Ifm < 0If m is greater than the number of substrings, it will return all substrings.
package main import ( "fmt" "strings" ) func main() { //Create and initialize strings str1 := "Welcome, to the, online portal, of w3codebox" str2 := "My dog name is Dollar" str3 := "I like to play Ludo" //Display string fmt.Println("String 1: "str1) fmt.Println("String 2: "str2) fmt.Println("String 3: "str3) //Split the given string //Use SplitAfterN() function res1 := strings.SplitAfterN(str1, ",", 2) res2 := strings.SplitAfterN(str2, "", 4) res3 := strings.SplitAfterN(str3, "!", 1) res4 := strings.SplitAfterN("", "w3codebox, geeks 3) //Display result fmt.Println("\nResult 1: "res1) fmt.Println("Result 2: "res2) fmt.Println("Result 3: "res3) fmt.Println("Result 4: "res4) }
Output:
String 1: Welcome, to the, online portal, of w3codebox String 2: My dog name is Dollar String 3: I like to play Ludo Result 1: Welcome, to the, online portal, of w3codebox] Result 2: My dog name is Dollar Result 3: I like to play Ludo Result 4: []