Go 標(biāo)準(zhǔn)包 strings 學(xué)習(xí)(7)Map,Repeat,Replace
上次標(biāo)題太長了,后面的一個(gè)方法名沒寫出來,但是標(biāo)題還是要寫的,無非就是少寫幾個(gè)方法
后面就先給文章一個(gè)目錄吧
本次學(xué)習(xí)了
Map,Repeat,Replace
開始
https://pkg.go.dev/strings@go1.20.4#Map
func?Map
Map returns a copy of the string s with all its characters modified according to the mapping function. If mapping returns a negative value, the character is dropped from the string with no replacement.
Map返回字符串s的副本,其中所有字符都根據(jù)映射函數(shù)進(jìn)行了修改。如果映射返回負(fù)值,則從字符串中刪除該字符,不進(jìn)行替換。
沒看懂...
例子
輸出
'Gjnf oevyyvt naq gur fyvgul tbcure...
理解
案例里面演示了一個(gè)類似加密的操作
這個(gè)方法的應(yīng)用暫時(shí)沒想到

func?Repeat
Repeat returns a new string consisting of count copies of the string s.
It panics if count is negative or if the result of (len(s) * count) overflows.
Repeat返回一個(gè)由字符串s的count個(gè)副本組成的新字符串。
如果count為負(fù)或(len(s) * count)的結(jié)果溢出,則會產(chǎn)生恐慌。
舉例
輸出
banana
理解
就是復(fù)制count次,然后組裝成1個(gè)新的字符串

func?Replace
Replace returns a copy of the string s with the first n 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. If n < 0, there is no limit on the number of replacements.
Replace返回字符串s的副本,其中舊的前n個(gè)不重疊的實(shí)例替換為new。如果old為空,它匹配字符串的開頭和每個(gè)UTF-8序列之后,為k-rune字符串產(chǎn)生最多k+1個(gè)替換。如果n < 0,則替換次數(shù)沒有限制。
案例
輸出
oinky oinky oink
moo moo moo
理解
就是在s里面把old的替換成new字符串,可以指定替換多少個(gè),要換多少n就寫多少,如果要全替換就寫-1