One such task of string manipulation is string splitting. When you have a long string, either from user input, a file, database, or environment variables, you may need to split into smaller chunks and perform operations on each. An example would be to split a string from a comma-separated file with the commas as the delimiter.
Using this guide, you will learn various splitting a string in the go programming language.
Strings.Split() Function
Go is a vast programming language that comes with a lot of tools and functionalities out of the box. Using the strings package from the standard library, we can use the Split() function to split a string based on a specific delimiter.
The syntax of the function is as shown:
The function takes the string and the delimiter as the arguments. It then returns a slice of the strings, with each element of the slice being a substring separated by the delimiter.
Let us take the example below:
import (
"fmt"
"strings"
func main() {
path_str := "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:"
path := strings.Split(path_str, ":")
fmt.Println(path)
}
In the example above, we have a variable path_str which holds the output of the path environment variable in Linux.
We then use the Split() method to separate each value of the path with the delimiter as the colon (:)
Once we run the code above, we should get an output as shown:
Once we have the slice of strings, we can use index notation to access each element as shown:
The above should return the element at index 0 as shown in the output below:
Due to its simplicity and diversity, this is probably the one you will require when you need to split a string in Go.
Strings.SplitN()
The SplitN() method from the strings package allows you to separate a string based on the specified delimiter. However, unlike the Split() method, the splitN method takes three arguments, as shown by the syntax below:
The SplitN() function allows you to specify the string to separate, the delimiter, and the number of strings to include in the slice.
If the number of substrings to include in the slice equals 0, the function returns nil.
Consider the example below:
import (
"fmt"
"strings"
)
func main() {
path_str := "Samsung,Apple,Microsoft,Amazon,Google"
path := strings.SplitN(path_str, ",", 3)
fmt.Println(path)
}
In this example, we use a comma-separated string. We then use the SplitN() method to extract the substring based on the comma. We also specify the number of items in the slice.
The resulting output is as shown:
Note that the function includes the string after the number of items as a single string.
Strings.SplitAfter
The strings package is very diverse. It also offers the SplitAfter() function. It works similarly to the Split() method but retains the delimiters after each substring in the slice.
Turn your attention to the code below:
import (
"fmt"
"strings"
)
func main() {
path_str := "Samsung,Apple,Microsoft,Amazon,Google"
path := strings.SplitAfter(path_str, ",")
fmt.Println(path)
}
The above example uses the SplitAfter() method to split the string based on the comma as the delimiter. We also preserve the delimiter after each element as shown in the output below:
Strings.SplitAfterN()
You guessed it. This function works similarly to the strings.SplitN() method. This allows you to specify the number of the substring to include in the slice.
An example is shown below:
import (
"fmt"
"strings"
)
func main() {
path_str := "Samsung,Apple,Microsoft,Amazon,Google"
path := strings.SplitAfter(path_str, ",")
fmt.Println(path)
}
The resulting output:
Strings.Fields()
The strings.Fields() method allows you to split strings by the available white spaces. Unlike SplitAfter() and SplitAfterN(), this method does not include the whitespaces in the slice.
If you do not care about which whitespace, this is probably the best method for you.
Consider an example as shown:
import (
"fmt"
"strings"
)
func main() {
path_str := "\nSamsung \t Apple \v Microsoft \r Amazon Google"
path := strings.Fields(path_str)
fmt.Println(path)
}
The function takes the string and splits it in all the available whitespaces.
An example output is as shown:
Conclusion
Strings are incredible in the Go programming language. Therefore, it is to understand how to perform various actions on them, as shown in this guide.
Keep practicing & Happy Coding!