go Fields()

Fields()方法语法

func Fields(s string) []string

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

Fields splits the string s around each instance of one or more consecutive white space characters, as defined by unicode.IsSpace, returning a slice of substrings of s or an empty slice if s contains only white space.

即,golang的strings.Fields()方法,可以将字符串s按空格或空白(由方法unicode.IsSpace确定,空白字符可以是一个或连续的多个)进行分割,成多个字符串,并返回一个元素为字符串的切片,如果字符串全部都是空白,或是空的字符串,则返回空的切片。


strings.Fields()方法实例代码

func main() {
	var str_slice []string = strings.Fields("笨鸟  工具 x1y1z1.com")
	fmt.Println(str_slice)
	fmt.Println(len(str_slice))
}

运行go文件,得到输出:

[笨鸟 工具 x1y1z1.com]
3

全栈后端 / go语法 :













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