How to Split a String in Golang?
In Go language, strings differ from other languages like Java, C++, and Python. A string in Go is a sequence of variable-width characters, with each character represented by one or more bytes using UTF-8 encoding. In Go, you can split a string into a slice using several functions provided in the strings
package. In this article,we will learn How to Split a String in Golang?
Example
package main
import (
"fmt"
"strings"
)
func main() {
str := "Welcome,to,GeeksforGeeks"
fmt.Println("Original String:", str)
}
Here, the string "Welcome,to,GeeksforGeeks"
will be split using different methods. First, ensure you have imported the strings
package to access the split functions.
Syntax
func Split(s, sep string) []string #UsingSplit
Function
func SplitAfter(s, sep string) []string # UsingSplitAfter
Function
func SplitN(s, sep string, n int) []string # UsingSplitN
Function
func SplitAfterN(s, sep string, n int) []string # UsingSplitAfterN
Function
Using Split
Function
The Split
function divides a string into all substrings separated by the specified separator and returns a slice with these substrings.
Syntax
func Split(s, sep string) []string
s
: The string to be split.sep
: The separator. Ifsep
is empty, it splits after each UTF-8 character. If bothstr
andsep
are empty, it returns an empty slice.
Example
package main
import (
"fmt"
"strings"
)
func main() {
s := "Welcome,to,GeeksforGeeks"
fmt.Println("", s)
result := strings.Split(s, ",")
fmt.Println("Result:", result)
}
Output
Welcome,to,GeeksforGeeks Result: [Welcome to GeeksforGeeks]
SplitAfter
Function
The SplitAfter
function splits a string after each instance of the specified separator and returns a slice.
Syntax:
func SplitAfter(s, sep string) []string
Note:
- If
sep
is empty, it splits after each UTF-8 sequence.- If
s
doesn’t containsep
, it returns a slice of length 1 withs
.
Example
package main
import (
"fmt"
"strings"
)
func main() {
s := "Welcome,to,GeeksforGeeks"
fmt.Println("", s)
result := strings.SplitAfter(s, ",")
fmt.Println("Result:", result)
}
Output
Welcome,to,GeeksforGeeks Result: [Welcome, to, GeeksforGeeks]
SplitN
Function
The SplitN
function splits a string into a maximum number of substrings.
Syntax:
func SplitN(s, sep string, n int) []string
n > 0
: Splits up ton
substrings.n == 0
: Returns an empty slice.n < 0
: Returns all substrings.
Example:
package main
import (
"fmt"
"strings"
)
func main() {
s := "Welcome,to,GeeksforGeeks"
fmt.Println("", s)
result := strings.SplitN(s, ",", 2)
fmt.Println("Result:", result)
}
Output
Welcome,to,GeeksforGeeks Result: [Welcome to,GeeksforGeeks]
SplitAfterN
Function
The SplitAfterN
function splits a string after each instance of the separator, but only up to n
substrings.
Syntax
func SplitAfterN(str, sep string, n int) []string
- n > 0: Splits up to n substrings.
- n == 0: Returns an empty slice.
- n < 0: Returns all substrings.
Example:
package main
import (
"fmt"
"strings"
)
func main() {
s := "Welcome,to,GeeksforGeeks"
fmt.Println("", s)
result := strings.SplitAfterN(s, ",", 2)
fmt.Println("Result using SplitAfterN:", result)
}
Output
Welcome,to,GeeksforGeeks Result using SplitAfterN: [Welcome, to,GeeksforGeeks]
Each of these functions provides a flexible way to split strings in Go, depending on the use case and the desired output structure. By understanding these functions, you can more effectively handle string manipulation in your Go programs.