go Split()

Split()方法语法

func Split(s, sep string) []string

go源码对Split()方法的介绍:

Split slices s into all substrings separated by sep and returns a slice of the substrings between those separators.

If s does not contain sep and sep is not empty, Split returns a slice of length 1 whose only element is s.

If sep is empty, Split splits after each UTF-8 sequence. If both s and sep are empty, Split returns an empty slice.

It is equivalent to SplitN with a count of -1.

即,golang的strings.Split()方法,可以将字符串s以sep为分隔符来切分字符串,其中sep分隔符在切分的过程中会被去掉,如果sep为空字符,则Split()方法会将s字符串切分成每一个字符所对应的UTF-8的编码值为元素的一个string切片,具体可参考下方实例。


strings.Split()方法实例代码

func main() {
	var strSlice []string = strings.Split("笨鸟工具,x1y1z1.com", ",")
	fmt.Println(strSlice)
	var strSlice2 []string = strings.Split("笨鸟工具,x1y1z1.com", "")
	fmt.Println(strSlice2)
}

运行go文件,得到输出:

[笨鸟工具 x1y1z1.com]
[笨 鸟 工 具 , x 1 y 1 z 1 . c o m]

全栈后端 / go语法 :













Copyright © 2022-2024 笨鸟工具 x1y1z1.com All Rights Reserved.