go SplitN()
SplitN()方法语法
func SplitN(s, sep string, n int) []string
go源码对SplitN()方法的介绍:
SplitN slices s into substrings separated by sep and returns a slice of the substrings between those separators.
The count determines the number of substrings to return:
n > 0: at most n substrings; the last substring will be the unsplit remainder.
n == 0: the result is nil (zero substrings)
即,golang的strings.SplitN()方法,可以将参数指定的字符串s以sep为分隔符切分字符串,当整数类型的n大于0,可以指定字符串按分隔符切分成几部分,如果n==0,等返回nil,如果n小于0,则返回字符串按分隔符全部切分组成的切片。
strings.SplitN()方法实例代码
func main() {
var str string = "笨鸟工具,x1y1z1.com"
var str_slice1 []string = strings.SplitN(str, "1", 2)
fmt.Println(str_slice1)
var str_slice2 []string = strings.SplitN(str, "1", -1)
fmt.Println(str_slice2)
str_slice3 := strings.SplitN(str, "1", 0)
fmt.Println(str_slice3)
}
运行go文件,得到输出:
[笨鸟工具,x y1z1.com]
[笨鸟工具,x y z .com]
[]