go strings.SplitAfterN()

SplitAfterN()方法语法

func SplitAfterN(s, sep string, n int) []string

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

SplitAfterN slices s into substrings after each instance of sep and returns a slice of those substrings.

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)
n < 0: all substrings
Edge cases for s and sep (for example, empty strings) are handled as described in the documentation for SplitAfter.

即,golang的标准库strings.SplitAfterN()方法,可以按分隔符seq的后一位将字符串s进行分割,当N大于0时,表示将字符串s切分成最多n个子字符串,当n==0时,返回nil,当n小于0时,则按所有的分隔符的后一位进行切分。返回值为string切片。


math.IsNaN()方法实例代码

func main() {
	str := "笨鸟工具,x1y1z1.com"
	var slice1 []string = strings.SplitAfterN(str, "1", 2)
	fmt.Println(slice1)
	slice2 := strings.SplitAfterN(str, "1", -1)
	fmt.Println(slice2)
	slice3 := strings.SplitAfterN(str, "1", 0)
	fmt.Println(slice3)
}

运行go文件,得到输出:

[笨鸟工具,x1 y1z1.com]
[笨鸟工具,x1 y1 z1 .com]
[]

全栈后端 / go语法 :













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