Splitting
The strings
package offers convenient functions to split a string to get a slice of strings.
How to do it...
- To split a string into components using a delimiter, use
strings.Split
. - To split the space-separated components of a string, use
strings.Fields
.
How it works...
If you need to parse a string delimited with a fixed delimiter, use strings.Split
. If you need to parse the space-separated sections of a string, use strings.Fields
:
package main import ( "fmt" "strings" ) func main() { fmt.Println(strings.Split("a,b,c,d", ",")) // ["a", "b", "c", "d"] fmt.Println(strings.Split("a, b, c, d", ",")) // ["a", " b", " c", " d"] &...