In Go, if I have a string variable s:
var s string = "a,b,c,d,e"
How can I convert or split or explode it into a slice or an array of strings so that it will become:
arr[0] = "a" ... arr[4] = "e"
Let's use the split() method and split the string by a comma. In the above example, the string object is delimited by a comma. The split() method splits the string when it finds the comma as a delimiter. Let's see another example in which we will use multiple delimiters to split the string.
The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.
You should use the strings
package for that.
stringSlice := strings.Split(s, ",")
http://play.golang.org/p/UKZbcuJUPP
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With