如何使用定界符对字符串进行切片 [英] How to slice a string using a delimiter
本文介绍了如何使用定界符对字符串进行切片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Go中,如果我有一个字符串变量s:
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"
推荐答案
您应使用 strings
包。
stringSlice := strings.Split(s, ",")
http://play.golang.org/p/UKZbcuJUPP
这篇关于如何使用定界符对字符串进行切片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文