Go 標(biāo)準(zhǔn)包 strings 學(xué)習(xí)(8)ReplaceAll,Split,SplitAfter
繼續(xù)
本次學(xué)習(xí)
ReplaceAll,Split,SplitAfter
https://pkg.go.dev/strings@go1.20.4#ReplaceAll
func?ReplaceAll
ReplaceAll returns a copy of the string s with all non-overlapping instances of old replaced by new. If old is empty, it matches at the beginning of the string and after each UTF-8 sequence, yielding up to k+1 replacements for a k-rune string.
ReplaceAll返回字符串s的副本,其中所有不重疊的old實(shí)例替換為new。如果old為空,它匹配字符串的開(kāi)頭和每個(gè)UTF-8序列之后,為k-rune字符串產(chǎn)生最多k+1個(gè)替換。
案例
輸出
moo moo moo
理解
沒(méi)啥好說(shuō)的,作用和方法名一樣用new把s里面的old全替換掉

func?Split
Split slices s into all substrings separated by sep and returns a slice of the substrings between those separators.
If s does not contain sep and sep is not empty, Split returns a slice of length 1 whose only element is s.
If sep is empty, Split splits after each UTF-8 sequence. If both s and sep are empty, Split returns an empty slice.
It is equivalent to SplitN with a count of -1.
To split around the first instance of a separator, see Cut.
????
將切片拆分為所有以sep分隔的子字符串,并返回分隔符之間的子字符串切片。
如果s不包含sep且sep不為空,Split返回一個(gè)長(zhǎng)度為1的切片,其中唯一的元素是s。
如果sep為空,Split在每個(gè)UTF-8序列之后進(jìn)行分割。如果s和sep都為空,Split返回一個(gè)空片。
它相當(dāng)于計(jì)數(shù)為-1的SplitN。
要圍繞分隔符的第一個(gè)實(shí)例進(jìn)行拆分,請(qǐng)參見(jiàn)切割。
案例
????
輸出
["a" "b" "c"]
["" "man " "plan " "canal panama"]
[" " "x" "y" "z" " "]
[""]
理解
將s用sep拆分成切片

func?SplitAfter
SplitAfter slices s into all substrings after each instance of sep and returns a slice of those substrings.
If s does not contain sep and sep is not empty, SplitAfter returns a slice of length 1 whose only element is s.
If sep is empty, SplitAfter splits after each UTF-8 sequence. If both s and sep are empty, SplitAfter returns an empty slice.
It is equivalent to SplitAfterN with a count of -1.
SplitAfter在sep的每個(gè)實(shí)例之后將s切片為所有子字符串,并返回這些子字符串的一個(gè)切片。
如果s不包含sep且sep不為空,則SplitAfter返回一個(gè)長(zhǎng)度為1的切片,其中唯一的元素是s。
如果sep為空,SplitAfter在每個(gè)UTF-8序列之后進(jìn)行分割。如果s和sep都為空,則SplitAfter返回一個(gè)空切片。
它相當(dāng)于計(jì)數(shù)為-1的SplitAfterN。
案例1
輸出1
["a," "b," "c"]
案例2
輸出2
["a," "b," "c," "," ""]