GO語言"bytes"包中"Replace"函數的用法及代碼示例。
用法:
func Replace(s, old, new []byte, n int) []byte
Replace 返回切片 s 的副本,其中前 n 個不重疊的 old 實例替換為 new。如果 old 為空,它會在切片的開頭和每個 UTF-8 序列之後匹配,產生最多 k+1 個替換 k-rune 切片。如果 n < 0,則替換次數沒有限製。
例子:
package main
import (
"bytes"
"fmt"
)
func main() {
fmt.Printf("%s\n", bytes.Replace([]byte("oink oink oink"), []byte("k"), []byte("ky"), 2))
fmt.Printf("%s\n", bytes.Replace([]byte("oink oink oink"), []byte("oink"), []byte("moo"), -1))
}
輸出:
oinky oinky oink moo moo moo
相關用法
- GO ReplaceAll用法及代碼示例
- GO Repeat用法及代碼示例
- GO Regexp.FindString用法及代碼示例
- GO Regexp.FindAllIndex用法及代碼示例
- GO ResponseRecorder用法及代碼示例
- GO ReverseBytes64用法及代碼示例
- GO ReverseBytes16用法及代碼示例
- GO Regexp.ReplaceAllLiteralString用法及代碼示例
- GO Regexp.FindStringSubmatch用法及代碼示例
- GO Regexp.FindAllString用法及代碼示例
- GO ReadMessage用法及代碼示例
- GO Regexp.ExpandString用法及代碼示例
- GO ResponseWriter用法及代碼示例
- GO Regexp.FindAllStringSubmatch用法及代碼示例
- GO Reverse用法及代碼示例
- GO Read用法及代碼示例
- GO Rel用法及代碼示例
- GO Regexp.SubexpIndex用法及代碼示例
- GO Regexp.Match用法及代碼示例
- GO Remainder用法及代碼示例
注:本文由純淨天空篩選整理自golang.google.cn大神的英文原創作品 Replace。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。