go FieldsFunc()

FieldsFunc()方法语法

func FieldsFunc(s string, f func(rune) bool) []string

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

FieldsFunc splits the string s at each run of Unicode code points c satisfying f(c) and returns an array of slices of s. If all code points in s satisfy f(c) or the string is empty, an empty slice is returned.

FieldsFunc makes no guarantees about the order in which it calls f(c) and assumes that f always returns the same value for a given c.

即,golang的strings.FieldsFunc()方法,可以将字符串s按符合f函数(返回true)的编码值对应的字符来进行分割成多个字符串,返回值为元素为字符串的切片,如果字符串为空白,或为空字符串,则返回一个空切片。


strings.FieldsFunc()方法实例代码

func UnicodeValue(arg rune) bool {
	if arg == 65292 { 
		return true
	} else {
		return false
	}
}

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

运行go文件,得到输出:

[笨鸟工具 x1y1z1.com]

提示:其中65292是中文逗号“,”的Unicode编码值,字符所对应的Unicode编码值,可参考:unicode编码转换工具在线



全栈后端 / go语法 :













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