In this tutorial, I’ll share with you four ways to split a string into a slice of substrings in Go.
We’ll be using these 4 functions from Go’s strings package for the slicing task:-
func Split(s, sep string) []string
func SplitN(s, sep string, n int) []string
func SplitAfter(s, sep string) []string
func SplitAfterN(s, sep string, n int) []string
In the sections that follow, we’ll discuss what these functions are and learn how to use them.
1. Split String using strings.Split()
For most cases, func Split(s, sep string) []string
is all you’ll need. This function splits input string s using sep as the separator and returns the substrings as a slice.
Similar to split/explode functions from languages such as Python and PHP, strings.Split() does not return the separators as part of the slice of substrings.
When the separator is a blank string “”, the function will split the string by UTF-8 character.
[Example]
import (
"fmt"
"strings"
)
str := "Hello+World+How+Are+You"
substrs := strings. Split(str, "+")
fmt.Printf("%q\n", substrs)
[Output]
["Hello" "World" "How" "Are" "You"]
2. Slice String (Partially) using strings.SplitN()
If you don’t want to slice the entire string completely, take a look at func SplitN(s, sep string, n int) []string
.
What makes it different from the regular Split() is that you can specify how many substrings you want the function to return through n.
Say we have a string, s = “Split the Atom”. If we slice it using the regular Split(), we’ll get a slice of three substrings: [“Split”, “the”, “Atom”]
If we use SplitN() and set n=2, SplitN() will only return a slice that has two elements in it, i.e. [“Split”, “the Atom”]. The first element is substring delimited by the separator, “Split”. The n-1 element (turns out the second element in this example) is the remainder of the string which is “the Atom”.
When n=-1, SplitN() behaves exactly like Split() which means it will process all substrings.
When n=0, SplitN() will simply return a zero-length slice.
Still confused? Here are more examples to help demonstrate how the function works.
[Example]
str := "Hello+World+How+Are+You"
fmt.Printf("SplitN, n=-1: %q\n", strings.SplitN(str, "+", -1))
fmt.Printf("SplitN, n=0: %q\n", strings.SplitN(str, "+", 0))
fmt.Printf("SplitN, n=2: %q\n", strings.SplitN(str, "+", 2))
fmt.Printf("SplitN, n=3: %q\n", strings.SplitN(str, "+", 3))
[Output]
SplitN, n=-1: ["Hello" "World" "How" "Are" "You"]
SplitN, n=0: []
SplitN, n=2: ["Hello" "World+How+Are+You"]
SplitN, n=3: ["Hello" "World" "How+Are+You"]
3. String to Slice using strings.SplitAfter()
Two aspects of func SplitAfter(s, sep string) []string
set it apart from Split().
First, for SplitAfter(), the split happens after the separator. Second, the substrings include the separators. Unlike Split() which eliminates the separators, the separators are NOT destroyed by SplitAfter().
To demonstrate these differences, we’ll slice the same string using the same separator (++) using both SplitAfter() and Split() in the following example.
[Example]
str := "Hello++World++How++Are++You"
fmt.Printf("SplitAfter: %q\n", strings.SplitAfter(str, "++"))
fmt.Printf("Split: %q\n", strings. Split(str, "++"))
[Output]
SplitAfter: ["Hello++" "World++" "How++" "Are++" "You"]
Split: ["Hello" "World" "How" "Are" "You"]
As you can observe from the output, SplitAfter() returns the separators but Split() does not.
4. Partial Slicing using strings.SplitAfterN()
Similar to SplitN() func SplitAfterN(s, sep string, n int) []string
gives the caller the option to partially slice a string. Unlike SplitAfter(), SplitAfterN() returns the separators.
To find out how n works, read the section above for SplitN().
Here are a few examples of SplitAfterN in action:-
str := "Hello++World++How++Are++You"
fmt.Printf("n=-1: %q\n", strings.SplitAfterN(str, "++", -1))
fmt.Printf("n=0: %q\n", strings.SplitAfterN(str, "++", 0))
fmt.Printf("n=1: %q\n", strings.SplitAfterN(str, "++", 1))
fmt.Printf("n=2: %q\n", strings.SplitAfterN(str, "++", 2))
fmt.Printf("n=3: %q\n", strings.SplitAfterN(str, "++", 3))
[Output]
n=-1: ["Hello++" "World++" "How++" "Are++" "You"]
n=0: []
n=1: ["Hello++World++How++Are++You"]
n=2: ["Hello++" "World++How++Are++You"]
n=3: ["Hello++" "World++" "How++Are++You"]
Discussion
- My initial reaction after seeing SplitAfter in the documentation for strings package was “Is there a SplitBefore function?” And the answer is no, as of Go 1.19, there isn’t an official SplitBefore function.
- As discussed earlier, when the separator is empty, the split functions will slice the string by UTF-8 character. This is a lot like converting the characters in the string to runes using []rune(str), with one major difference: Split functions from strings package family always return a String slice, where as []rune(str) creates a Rune slice.