Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to slice a string using a delimiter

Tags:

arrays

slice

go

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" 
like image 361
sagit Avatar asked Jan 10 '13 17:01

sagit


People also ask

How do you delimiter a string?

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.

How do I split a string into substring?

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.


1 Answers

You should use the strings package for that.

stringSlice := strings.Split(s, ",") 

http://play.golang.org/p/UKZbcuJUPP

like image 90
Daniel Avatar answered Sep 30 '22 13:09

Daniel