欢迎光临散文网 会员登陆 & 注册

Go 标准包 strings 学习(4)EqualFold,Fields,FieldsFunc

2023-04-28 13:16 作者:取悦疾风  | 我要投稿

官网

https://pkg.go.dev/strings@go1.20.3


func EqualFold

EqualFold reports whether s and t, interpreted as UTF-8 strings, are equal under simple Unicode case-folding, which is a more general form of case-insensitivity.

翻译

EqualFold报告解释为UTF-8字符串的s和t在简单的Unicode大小写折叠下是否相等,这是一种更通用的大小写不敏感形式。

例子

理解:

就是不区分大小写的equal

func 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.

翻译

Fields围绕一个或多个连续空格字符的每个实例拆分字符串s,这是由unicode定义的。IsSpace,返回s的子字符串切片,如果s只包含空白则返回空切片。

例子

输出:

Fields are: ["foo" "bar" "baz"]

理解:

将字符串按照空格分割,并放入一个切片中

func 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.


翻译

FieldsFunc在每次运行满足f(c)的Unicode代码点c时拆分字符串s,并返回s的切片数组。如果s中的所有代码点都满足f(c)或字符串为空,则返回空切片。

FieldsFunc不保证调用f(c)的顺序,并假设对于给定的c, f总是返回相同的值。

案例:

输出

Fields are: ["foo1" "bar2" "baz3"]

理解

f就是要自定义的一个规则

然后调用FieldsFunc,传入需要分割的字符串,再传入规则f,即可获取分割后的字符串切片

结合上一个方法看,就是这个方法允许自定义分割的规则,上一个方法是通过空格分割的

Go 标准包 strings 学习(4)EqualFold,Fields,FieldsFunc的评论 (共 条)

分享到微博请遵守国家法律