golang字串的切割-strings.Split
- 语法
func SplitN(s string, sep string, n int) []string
n > 0: at most n substrings; the last substring will be the unsplit remainder.
n == 0: the result is nil (zero substrings)
n < 0: all substring
- 示例
代码
func main(){
names := "刘备 关羽 张飞 赵云 诸葛亮"
nameList := strings.Split(names," ")
for i,name := range nameList{
fmt.Printf("%d: %s\n",i,name)
}
输出
0: 刘备
1: 关羽
2: 张飞
3: 赵云
4: 诸葛亮