🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# package strings `import "strings"` strings包实现了用于操作字符的简单函数。 ## Index * [func EqualFold(s, t string) bool](#EqualFold) * [func HasPrefix(s, prefix string) bool](#HasPrefix) * [func HasSuffix(s, suffix string) bool](#HasSuffix) * [func Contains(s, substr string) bool](#Contains) * [func ContainsRune(s string, r rune) bool](#ContainsRune) * [func ContainsAny(s, chars string) bool](#ContainsAny) * [func Count(s, sep string) int](#Count) * [func Index(s, sep string) int](#Index) * [func IndexByte(s string, c byte) int](#IndexByte) * [func IndexRune(s string, r rune) int](#IndexRune) * [func IndexAny(s, chars string) int](#IndexAny) * [func IndexFunc(s string, f func(rune) bool) int](#IndexFunc) * [func LastIndex(s, sep string) int](#LastIndex) * [func LastIndexAny(s, chars string) int](#LastIndexAny) * [func LastIndexFunc(s string, f func(rune) bool) int](#LastIndexFunc) * [func Title(s string) string](#Title) * [func ToLower(s string) string](#ToLower) * [func ToLowerSpecial(_case unicode.SpecialCase, s string) string](#ToLowerSpecial) * [func ToUpper(s string) string](#ToUpper) * [func ToUpperSpecial(_case unicode.SpecialCase, s string) string](#ToUpperSpecial) * [func ToTitle(s string) string](#ToTitle) * [func ToTitleSpecial(_case unicode.SpecialCase, s string) string](#ToTitleSpecial) * [func Repeat(s string, count int) string](#Repeat) * [func Replace(s, old, new string, n int) string](#Replace) * [func Map(mapping func(rune) rune, s string) string](#Map) * [func Trim(s string, cutset string) string](#Trim) * [func TrimSpace(s string) string](#TrimSpace) * [func TrimFunc(s string, f func(rune) bool) string](#TrimFunc) * [func TrimLeft(s string, cutset string) string](#TrimLeft) * [func TrimLeftFunc(s string, f func(rune) bool) string](#TrimLeftFunc) * [func TrimPrefix(s, prefix string) string](#TrimPrefix) * [func TrimRight(s string, cutset string) string](#TrimRight) * [func TrimRightFunc(s string, f func(rune) bool) string](#TrimRightFunc) * [func TrimSuffix(s, suffix string) string](#TrimSuffix) * [func Fields(s string) []string](#Fields) * [func FieldsFunc(s string, f func(rune) bool) []string](#FieldsFunc) * [func Split(s, sep string) []string](#Split) * [func SplitN(s, sep string, n int) []string](#SplitN) * [func SplitAfter(s, sep string) []string](#SplitAfter) * [func SplitAfterN(s, sep string, n int) []string](#SplitAfterN) * [func Join(a []string, sep string) string](#Join) * [type Reader](#Reader) * [func NewReader(s string) \*Reader](#NewReader) * [func (r \*Reader) Len() int](#Reader.Len) * [func (r \*Reader) Read(b []byte) (n int, err error)](#Reader.Read) * [func (r \*Reader) ReadByte() (b byte, err error)](#Reader.ReadByte) * [func (r \*Reader) UnreadByte() error](#Reader.UnreadByte) * [func (r \*Reader) ReadRune() (ch rune, size int, err error)](#Reader.ReadRune) * [func (r \*Reader) UnreadRune() error](#Reader.UnreadRune) * [func (r \*Reader) Seek(offset int64, whence int) (int64, error)](#Reader.Seek) * [func (r \*Reader) ReadAt(b []byte, off int64) (n int, err error)](#Reader.ReadAt) * [func (r \*Reader) WriteTo(w io.Writer) (n int64, err error)](#Reader.WriteTo) * [type Replacer](#Replacer) * [func NewReplacer(oldnew ...string) \*Replacer](#NewReplacer) * [func (r \*Replacer) Replace(s string) string](#Replacer.Replace) * [func (r \*Replacer) WriteString(w io.Writer, s string) (n int, err error)](#Replacer.WriteString) ### Examples * [Contains](#example-Contains) * [ContainsAny](#example-ContainsAny) * [Count](#example-Count) * [EqualFold](#example-EqualFold) * [Fields](#example-Fields) * [FieldsFunc](#example-FieldsFunc) * [Index](#example-Index) * [IndexAny](#example-IndexAny) * [IndexFunc](#example-IndexFunc) * [IndexRune](#example-IndexRune) * [Join](#example-Join) * [LastIndex](#example-LastIndex) * [Map](#example-Map) * [NewReplacer](#example-NewReplacer) * [Repeat](#example-Repeat) * [Replace](#example-Replace) * [Split](#example-Split) * [SplitAfter](#example-SplitAfter) * [SplitAfterN](#example-SplitAfterN) * [SplitN](#example-SplitN) * [Title](#example-Title) * [ToLower](#example-ToLower) * [ToTitle](#example-ToTitle) * [ToUpper](#example-ToUpper) * [Trim](#example-Trim) * [TrimPrefix](#example-TrimPrefix) * [TrimSpace](#example-TrimSpace) * [TrimSuffix](#example-TrimSuffix) ## func [EqualFold](https://github.com/golang/go/blob/master/src/strings/strings.go#L674 "View Source") ``` func EqualFold(s, t string) bool ``` 判断两个utf-8编码字符串(将unicode大写、小写、标题三种格式字符视为相同)是否相同。 Example ``` fmt.Println(strings.EqualFold("Go", "go")) ``` Output: ``` true ``` ## func [HasPrefix](https://github.com/golang/go/blob/master/src/strings/strings.go#L371 "View Source") ``` func HasPrefix(s, prefix string) bool ``` 判断s是否有前缀字符串prefix。 ## func [HasSuffix](https://github.com/golang/go/blob/master/src/strings/strings.go#L376 "View Source") ``` func HasSuffix(s, suffix string) bool ``` 判断s是否有后缀字符串suffix。 ## func [Contains](https://github.com/golang/go/blob/master/src/strings/strings.go#L112 "View Source") ``` func Contains(s, substr string) bool ``` 判断字符串s是否包含子串substr。 Example ``` fmt.Println(strings.Contains("seafood", "foo")) fmt.Println(strings.Contains("seafood", "bar")) fmt.Println(strings.Contains("seafood", "")) fmt.Println(strings.Contains("", "")) ``` Output: ``` true false true true ``` ## func [ContainsRune](https://github.com/golang/go/blob/master/src/strings/strings.go#L122 "View Source") ``` func ContainsRune(s string, r rune) bool ``` 判断字符串s是否包含utf-8码值r。 ## func [ContainsAny](https://github.com/golang/go/blob/master/src/strings/strings.go#L117 "View Source") ``` func ContainsAny(s, chars string) bool ``` 判断字符串s是否包含字符串chars中的任一字符。 Example ``` fmt.Println(strings.ContainsAny("team", "i")) fmt.Println(strings.ContainsAny("failure", "u & i")) fmt.Println(strings.ContainsAny("foo", "")) fmt.Println(strings.ContainsAny("", "")) ``` Output: ``` false true false false ``` ## func [Count](https://github.com/golang/go/blob/master/src/strings/strings.go#L65 "View Source") ``` func Count(s, sep string) int ``` 返回字符串s中有几个不重复的sep子串。 Example ``` fmt.Println(strings.Count("cheese", "e")) fmt.Println(strings.Count("five", "")) // before & after each rune ``` Output: ``` 3 5 ``` ## func [Index](https://github.com/golang/go/blob/master/src/strings/strings.go#L127 "View Source") ``` func Index(s, sep string) int ``` 子串sep在字符串s中第一次出现的位置,不存在则返回-1。 Example ``` fmt.Println(strings.Index("chicken", "ken")) fmt.Println(strings.Index("chicken", "dmr")) ``` Output: ``` 4 -1 ``` ## func [IndexByte](https://github.com/golang/go/blob/master/src/strings/strings_decl.go#L8 "View Source") ``` func IndexByte(s string, c byte) int ``` 字符c在s中第一次出现的位置,不存在则返回-1。 ## func [IndexRune](https://github.com/golang/go/blob/master/src/strings/strings.go#L190 "View Source") ``` func IndexRune(s string, r rune) int ``` unicode码值r在s中第一次出现的位置,不存在则返回-1。 Example ``` fmt.Println(strings.IndexRune("chicken", 'k')) fmt.Println(strings.IndexRune("chicken", 'd')) ``` Output: ``` 4 -1 ``` ## func [IndexAny](https://github.com/golang/go/blob/master/src/strings/strings.go#L211 "View Source") ``` func IndexAny(s, chars string) int ``` 字符串chars中的任一utf-8码值在s中第一次出现的位置,如果不存在或者chars为空字符串则返回-1。 Example ``` fmt.Println(strings.IndexAny("chicken", "aeiouy")) fmt.Println(strings.IndexAny("crwth", "aeiouy")) ``` Output: ``` 2 -1 ``` ## func [IndexFunc](https://github.com/golang/go/blob/master/src/strings/strings.go#L537 "View Source") ``` func IndexFunc(s string, f func(rune) bool) int ``` s中第一个满足函数f的位置i(该处的utf-8码值r满足f(r)==true),不存在则返回-1。 Example ``` f := func(c rune) bool { return unicode.Is(unicode.Han, c) } fmt.Println(strings.IndexFunc("Hello, 世界", f)) fmt.Println(strings.IndexFunc("Hello, world", f)) ``` Output: ``` 7 -1 ``` ## func [LastIndex](https://github.com/golang/go/blob/master/src/strings/strings.go#L164 "View Source") ``` func LastIndex(s, sep string) int ``` 子串sep在字符串s中最后一次出现的位置,不存在则返回-1。 Example ``` fmt.Println(strings.Index("go gopher", "go")) fmt.Println(strings.LastIndex("go gopher", "go")) fmt.Println(strings.LastIndex("go gopher", "rodent")) ``` Output: ``` 0 3 -1 ``` ## func [LastIndexAny](https://github.com/golang/go/blob/master/src/strings/strings.go#L227 "View Source") ``` func LastIndexAny(s, chars string) int ``` 字符串chars中的任一utf-8码值在s中最后一次出现的位置,如不存在或者chars为空字符串则返回-1。 ## func [LastIndexFunc](https://github.com/golang/go/blob/master/src/strings/strings.go#L543 "View Source") ``` func LastIndexFunc(s string, f func(rune) bool) int ``` s中最后一个满足函数f的unicode码值的位置i,不存在则返回-1。 ## func [Title](https://github.com/golang/go/blob/master/src/strings/strings.go#L489 "View Source") ``` func Title(s string) string ``` 返回s中每个单词的首字母都改为标题格式的字符串拷贝。 BUG: Title用于划分单词的规则不能很好的处理Unicode标点符号。 Example ``` fmt.Println(strings.Title("her royal highness")) ``` Output: ``` Her Royal Highness ``` ## func [ToLower](https://github.com/golang/go/blob/master/src/strings/strings.go#L437 "View Source") ``` func ToLower(s string) string ``` 返回将所有字母都转为对应的小写版本的拷贝。 Example ``` fmt.Println(strings.ToLower("Gopher")) ``` Output: ``` gopher ``` ## func [ToLowerSpecial](https://github.com/golang/go/blob/master/src/strings/strings.go#L450 "View Source") ``` func ToLowerSpecial(_case unicode.SpecialCase, s string) string ``` 使用\_case规定的字符映射,返回将所有字母都转为对应的小写版本的拷贝。 ## func [ToUpper](https://github.com/golang/go/blob/master/src/strings/strings.go#L434 "View Source") ``` func ToUpper(s string) string ``` 返回将所有字母都转为对应的大写版本的拷贝。 Example ``` fmt.Println(strings.ToUpper("Gopher")) ``` Output: ``` GOPHER ``` ## func [ToUpperSpecial](https://github.com/golang/go/blob/master/src/strings/strings.go#L444 "View Source") ``` func ToUpperSpecial(_case unicode.SpecialCase, s string) string ``` 使用\_case规定的字符映射,返回将所有字母都转为对应的大写版本的拷贝。 ## func [ToTitle](https://github.com/golang/go/blob/master/src/strings/strings.go#L440 "View Source") ``` func ToTitle(s string) string ``` 返回将所有字母都转为对应的标题版本的拷贝。 Example ``` fmt.Println(strings.ToTitle("loud noises")) fmt.Println(strings.ToTitle("хлеб")) ``` Output: ``` LOUD NOISES ХЛЕБ ``` ## func [ToTitleSpecial](https://github.com/golang/go/blob/master/src/strings/strings.go#L456 "View Source") ``` func ToTitleSpecial(_case unicode.SpecialCase, s string) string ``` 使用\_case规定的字符映射,返回将所有字母都转为对应的标题版本的拷贝。 ## func [Repeat](https://github.com/golang/go/blob/master/src/strings/strings.go#L424 "View Source") ``` func Repeat(s string, count int) string ``` 返回count个s串联的字符串。 Example ``` fmt.Println("ba" + strings.Repeat("na", 2)) ``` Output: ``` banana ``` ## func [Replace](https://github.com/golang/go/blob/master/src/strings/strings.go#L638 "View Source") ``` func Replace(s, old, new string, n int) string ``` 返回将s中前n个不重叠old子串都替换为new的新字符串,如果n&lt;0会替换所有old子串。 Example ``` fmt.Println(strings.Replace("oink oink oink", "k", "ky", 2)) fmt.Println(strings.Replace("oink oink oink", "oink", "moo", -1)) ``` Output: ``` oinky oinky oink moo moo moo ``` ## func [Map](https://github.com/golang/go/blob/master/src/strings/strings.go#L383 "View Source") ``` func Map(mapping func(rune) rune, s string) string ``` 将s的每一个unicode码值r都替换为mapping(r),返回这些新码值组成的字符串拷贝。如果mapping返回一个负值,将会丢弃该码值而不会被替换。(返回值中对应位置将没有码值) Example ``` rot13 := func(r rune) rune { switch { case r >= 'A' && r <= 'Z': return 'A' + (r-'A'+13)%26 case r >= 'a' && r <= 'z': return 'a' + (r-'a'+13)%26 } return r } fmt.Println(strings.Map(rot13, "'Twas brillig and the slithy gopher...")) ``` Output: ``` 'Gjnf oevyyvt naq gur fyvgul tbcure... ``` ## func [Trim](https://github.com/golang/go/blob/master/src/strings/strings.go#L586 "View Source") ``` func Trim(s string, cutset string) string ``` 返回将s前后端所有cutset包含的utf-8码值都去掉的字符串。 Example ``` fmt.Printf("[%q]", strings.Trim(" !!! Achtung! Achtung! !!! ", "! ")) ``` Output: ``` ["Achtung! Achtung"] ``` ## func [TrimSpace](https://github.com/golang/go/blob/master/src/strings/strings.go#L613 "View Source") ``` func TrimSpace(s string) string ``` 返回将s前后端所有空白(unicode.IsSpace指定)都去掉的字符串。 Example ``` fmt.Println(strings.TrimSpace(" \t\n a lone gopher \n\t\r\n")) ``` Output: ``` a lone gopher ``` ## func [TrimFunc](https://github.com/golang/go/blob/master/src/strings/strings.go#L531 "View Source") ``` func TrimFunc(s string, f func(rune) bool) string ``` 返回将s前后端所有满足f的unicode码值都去掉的字符串。 ## func [TrimLeft](https://github.com/golang/go/blob/master/src/strings/strings.go#L595 "View Source") ``` func TrimLeft(s string, cutset string) string ``` 返回将s前端所有cutset包含的utf-8码值都去掉的字符串。 ## func [TrimLeftFunc](https://github.com/golang/go/blob/master/src/strings/strings.go#L508 "View Source") ``` func TrimLeftFunc(s string, f func(rune) bool) string ``` 返回将s前端所有满足f的unicode码值都去掉的字符串。 ## func [TrimPrefix](https://github.com/golang/go/blob/master/src/strings/strings.go#L619 "View Source") ``` func TrimPrefix(s, prefix string) string ``` 返回去除s可能的前缀prefix的字符串。 Example ``` var s = "Goodbye,, world!" s = strings.TrimPrefix(s, "Goodbye,") s = strings.TrimPrefix(s, "Howdy,") fmt.Print("Hello" + s) ``` Output: ``` Hello, world! ``` ## func [TrimRight](https://github.com/golang/go/blob/master/src/strings/strings.go#L604 "View Source") ``` func TrimRight(s string, cutset string) string ``` 返回将s后端所有cutset包含的utf-8码值都去掉的字符串。 ## func [TrimRightFunc](https://github.com/golang/go/blob/master/src/strings/strings.go#L518 "View Source") ``` func TrimRightFunc(s string, f func(rune) bool) string ``` 返回将s后端所有满足f的unicode码值都去掉的字符串。 ## func [TrimSuffix](https://github.com/golang/go/blob/master/src/strings/strings.go#L628 "View Source") ``` func TrimSuffix(s, suffix string) string ``` 返回去除s可能的后缀suffix的字符串。 Example ``` var s = "Hello, goodbye, etc!" s = strings.TrimSuffix(s, "goodbye, etc!") s = strings.TrimSuffix(s, "planet") fmt.Print(s, "world!") ``` Output: ``` Hello, world! ``` ## func [Fields](https://github.com/golang/go/blob/master/src/strings/strings.go#L307 "View Source") ``` func Fields(s string) []string ``` 返回将字符串按照空白(unicode.IsSpace确定,可以是一到多个连续的空白字符)分割的多个字符串。如果字符串全部是空白或者是空字符串的话,会返回空切片。 Example ``` fmt.Printf("Fields are: %q", strings.Fields(" foo bar baz ")) ``` Output: ``` Fields are: ["foo" "bar" "baz"] ``` ## func [FieldsFunc](https://github.com/golang/go/blob/master/src/strings/strings.go#L314 "View Source") ``` func FieldsFunc(s string, f func(rune) bool) []string ``` 类似Fields,但使用函数f来确定分割符(满足f的unicode码值)。如果字符串全部是分隔符或者是空字符串的话,会返回空切片。 Example ``` f := func(c rune) bool { return !unicode.IsLetter(c) && !unicode.IsNumber(c) } fmt.Printf("Fields are: %q", strings.FieldsFunc(" foo1;bar2,baz3...", f)) ``` Output: ``` Fields are: ["foo1" "bar2" "baz3"] ``` ## func [Split](https://github.com/golang/go/blob/master/src/strings/strings.go#L294 "View Source") ``` func Split(s, sep string) []string ``` 用去掉s中出现的sep的方式进行分割,会分割到结尾,并返回生成的所有片段组成的切片(每一个sep都会进行一次切割,即使两个sep相邻,也会进行两次切割)。如果sep为空字符,Split会将s切分成每一个unicode码值一个字符串。 Example ``` fmt.Printf("%q\n", strings.Split("a,b,c", ",")) fmt.Printf("%q\n", strings.Split("a man a plan a canal panama", "a ")) fmt.Printf("%q\n", strings.Split(" xyz ", "")) fmt.Printf("%q\n", strings.Split("", "Bernardo O'Higgins")) ``` Output: ``` ["a" "b" "c"] ["" "man " "plan " "canal panama"] [" " "x" "y" "z" " "] [""] ``` ## func [SplitN](https://github.com/golang/go/blob/master/src/strings/strings.go#L277 "View Source") ``` func SplitN(s, sep string, n int) []string ``` 用去掉s中出现的sep的方式进行分割,会分割到结尾,并返回生成的所有片段组成的切片(每一个sep都会进行一次切割,即使两个sep相邻,也会进行两次切割)。如果sep为空字符,Split会将s切分成每一个unicode码值一个字符串。参数n决定返回的切片的数目: ``` n > 0 : 返回的切片最多n个子字符串;最后一个子字符串包含未进行切割的部分。 n == 0: 返回nil n < 0 : 返回所有的子字符串组成的切片 ``` Example ``` fmt.Printf("%q\n", strings.SplitN("a,b,c", ",", 2)) z := strings.SplitN("a,b,c", ",", 0) fmt.Printf("%q (nil = %v)\n", z, z == nil) ``` Output: ``` ["a" "b,c"] [] (nil = true) ``` ## func [SplitAfter](https://github.com/golang/go/blob/master/src/strings/strings.go#L300 "View Source") ``` func SplitAfter(s, sep string) []string ``` 用从s中出现的sep后面切断的方式进行分割,会分割到结尾,并返回生成的所有片段组成的切片(每一个sep都会进行一次切割,即使两个sep相邻,也会进行两次切割)。如果sep为空字符,Split会将s切分成每一个unicode码值一个字符串。 Example ``` fmt.Printf("%q\n", strings.SplitAfter("a,b,c", ",")) ``` Output: ``` ["a," "b," "c"] ``` ## func [SplitAfterN](https://github.com/golang/go/blob/master/src/strings/strings.go#L286 "View Source") ``` func SplitAfterN(s, sep string, n int) []string ``` 用从s中出现的sep后面切断的方式进行分割,会分割到结尾,并返回生成的所有片段组成的切片(每一个sep都会进行一次切割,即使两个sep相邻,也会进行两次切割)。如果sep为空字符,Split会将s切分成每一个unicode码值一个字符串。参数n决定返回的切片的数目: ``` n > 0 : 返回的切片最多n个子字符串;最后一个子字符串包含未进行切割的部分。 n == 0: 返回nil n < 0 : 返回所有的子字符串组成的切 ``` Example ``` fmt.Printf("%q\n", strings.SplitAfterN("a,b,c", ",", 2)) ``` Output: ``` ["a," "b,c"] ``` ## func [Join](https://github.com/golang/go/blob/master/src/strings/strings.go#L349 "View Source") ``` func Join(a []string, sep string) string ``` 将一系列字符串连接为一个字符串,之间用sep来分隔。 Example ``` s := []string{"foo", "bar", "baz"} fmt.Println(strings.Join(s, ", ")) ``` Output: ``` foo, bar, baz ``` ## type [Reader](https://github.com/golang/go/blob/master/src/strings/reader.go#L16 "View Source") ``` type Reader struct { // 内含隐藏或非导出字段 } ``` Reader类型通过从一个字符串读取数据,实现了io.Reader、io.Seeker、io.ReaderAt、io.WriterTo、io.ByteScanner、io.RuneScanner接口。 ### func [NewReader](https://github.com/golang/go/blob/master/src/strings/reader.go#L144 "View Source") ``` func NewReader(s string) *Reader ``` NewReader创建一个从s读取数据的Reader。本函数类似bytes.NewBufferString,但是更有效率,且为只读的。 ### func (\*Reader) [Len](https://github.com/golang/go/blob/master/src/strings/reader.go#L24 "View Source") ``` func (r *Reader) Len() int ``` Len返回r包含的字符串还没有被读取的部分。 ### func (\*Reader) [Read](https://github.com/golang/go/blob/master/src/strings/reader.go#L31 "View Source") ``` func (r *Reader) Read(b []byte) (n int, err error) ``` ### func (\*Reader) [ReadByte](https://github.com/golang/go/blob/master/src/strings/reader.go#L59 "View Source") ``` func (r *Reader) ReadByte() (b byte, err error) ``` ### func (\*Reader) [UnreadByte](https://github.com/golang/go/blob/master/src/strings/reader.go#L69 "View Source") ``` func (r *Reader) UnreadByte() error ``` ### func (\*Reader) [ReadRune](https://github.com/golang/go/blob/master/src/strings/reader.go#L78 "View Source") ``` func (r *Reader) ReadRune() (ch rune, size int, err error) ``` ### func (\*Reader) [UnreadRune](https://github.com/golang/go/blob/master/src/strings/reader.go#L93 "View Source") ``` func (r *Reader) UnreadRune() error ``` ### func (\*Reader) [Seek](https://github.com/golang/go/blob/master/src/strings/reader.go#L103 "View Source") ``` func (r *Reader) Seek(offset int64, whence int) (int64, error) ``` Seek实现了io.Seeker接口。 ### func (\*Reader) [ReadAt](https://github.com/golang/go/blob/master/src/strings/reader.go#L44 "View Source") ``` func (r *Reader) ReadAt(b []byte, off int64) (n int, err error) ``` ### func (\*Reader) [WriteTo](https://github.com/golang/go/blob/master/src/strings/reader.go#L124 "View Source") ``` func (r *Reader) WriteTo(w io.Writer) (n int64, err error) ``` WriteTo实现了io.WriterTo接口。 ## type [Replacer](https://github.com/golang/go/blob/master/src/strings/replace.go#L10 "View Source") ``` type Replacer struct { // 内含隐藏或非导出字段 } ``` Replacer类型进行一系列字符串的替换。 ### func [NewReplacer](https://github.com/golang/go/blob/master/src/strings/replace.go#L31 "View Source") ``` func NewReplacer(oldnew ...string) *Replacer ``` 使用提供的多组old、new字符串对创建并返回一个\*Replacer。替换是依次进行的,匹配时不会重叠。 Example ``` r := strings.NewReplacer("<", "&lt;", ">", "&gt;") fmt.Println(r.Replace("This is <b>HTML</b>!")) ``` Output: ``` This is &lt;b&gt;HTML&lt;/b&gt;! ``` ### func (\*Replacer) [Replace](https://github.com/golang/go/blob/master/src/strings/replace.go#L78 "View Source") ``` func (r *Replacer) Replace(s string) string ``` Replace返回s的所有替换进行完后的拷贝。 ### func (\*Replacer) [WriteString](https://github.com/golang/go/blob/master/src/strings/replace.go#L83 "View Source") ``` func (r *Replacer) WriteString(w io.Writer, s string) (n int, err error) ``` WriteString向w中写入s的所有替换进行完后的拷贝。