Go 標(biāo)準(zhǔn)包 strings 學(xué)習(xí)(4)EqualFold,Fields,FieldsFunc
官網(wǎng)
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大小寫折疊下是否相等,這是一種更通用的大小寫不敏感形式。
例子
理解:
就是不區(qū)分大小寫的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圍繞一個或多個連續(xù)空格字符的每個實例拆分字符串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的切片數(shù)組。如果s中的所有代碼點都滿足f(c)或字符串為空,則返回空切片。
FieldsFunc不保證調(diào)用f(c)的順序,并假設(shè)對于給定的c, f總是返回相同的值。
案例:
輸出
Fields are: ["foo1" "bar2" "baz3"]
理解
f就是要自定義的一個規(guī)則
然后調(diào)用FieldsFunc,傳入需要分割的字符串,再傳入規(guī)則f,即可獲取分割后的字符串切片
結(jié)合上一個方法看,就是這個方法允許自定義分割的規(guī)則,上一個方法是通過空格分割的
Go 標(biāo)準(zhǔn)包 strings 學(xué)習(xí)(4)EqualFold,Fields,FieldsFunc的評論 (共 條)
