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: 诸葛亮

在这里插入图片描述

posted on 2021-12-18 17:03  运维开发玄德公  阅读(27)  评论(0编辑  收藏  举报  来源

导航