How to Split a String in Golang?
In Go language, strings are different from other languages like Java, C++, Python, etc. It is a sequence of variable-width characters where each and every character is represented by one or more bytes using UTF-8 Encoding. In Go strings, you are allowed to split a string into a slice with the help of following functions. These functions are defined under strings package so, you have to import strings package in your program for accessing these functions:
1. Split: This function splits a string into all substrings separated by the given separator and returns a slice which contains these substrings.
Syntax:
func Split(str, sep string) []string
Here, str is the string and sep is the separator. If str does not contain the given sep and sep is non-empty, then it will return a slice of length 1 which contain only str. Or if the sep is empty, then it will split after each UTF-8 sequence. Or if both str and sep are empty, then it will return an empty slice.
Example:
// 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 GeeksforGeeks" 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( "" , "GeeksforGeeks, geeks" ) // Displaying the 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 GeeksforGeeks String 2: My dog name is Dollar String 3: I like to play Ludo Result 1: [Welcome to the online portal of GeeksforGeeks] Result 2: [M y d o g n a m e i s N a w a b] Result 3: [I like to play Ludo] Result 4: []
2. SplitAfter: This function splits a string into all substrings after each instance of the given separator and returns a slice which contains these substrings.
Syntax:
func SplitAfter(str, sep string) []string
Here, str is the string and sep is the separator. If str does not contain the given sep and sep is non-empty, then it will return a slice of length 1 which contain only str. Or if the sep is empty, then it will split after each UTF-8 sequence. Or if both str and sep are empty, then it will return an empty slice.
Example:
// 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 GeeksforGeeks" 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( "" , "GeeksforGeeks, geeks" ) // Displaying the 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 GeeksforGeeks String 2: My dog name is Dollar String 3: I like to play Ludo Result 1: [Welcome, to the, online portal, of GeeksforGeeks] Result 2: [M y d o g n a m e i s N a w a b] Result 3: [I like to play Ludo] Result 4: []
3. SplitAfterN: This function splits a string into all substrings after each instance of the given separator and returns a slice which contains these substrings.
Syntax:
func SplitAfterN(str, sep string, m int) []string
Here, str is the string, sep is the separator, and m is used to find the number of substrings to return. Here, if m>0, then it returns at most m substrings and the last string substring will not split. If m == 0, then it will return nil. If m<0, then it will return all substrings.
Example:
// 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 GeeksforGeeks" 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( "" , "GeeksforGeeks, geeks" , 3) // Displaying the 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 GeeksforGeeks String 2: My dog name is Dollar String 3: I like to play Ludo Result 1: [Welcome, to the, online portal, of GeeksforGeeks] Result 2: [M y dog name is Dollar] Result 3: [I like to play Ludo] Result 4: []
Recommended Posts:
- How to split a slice of bytes in Golang?
- How to split a slice of bytes after the given separator in Golang?
- How to Trim a String in Golang?
- How to check the specified rune in Golang String?
- How to find the last index value of specified string in Golang?
- How to convert a string in uppercase in Golang?
- How to find the index value of specified string in Golang?
- How to Replace Characters in Golang String?
- Golang | Splitting the string after the specified separator
- Check if the String ends with specified suffix in Golang
- Check if the String starts with specified prefix in Golang
- How to convert a string in lower case in Golang?
- Golang | Extracting a Regular Expression from the String
- Golang | Creating a string that contains regexp metacharacters
- Golang | Checking the string for the specified regular expression
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.