# 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<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("<", "<", ">", ">")
fmt.Println(r.Replace("This is <b>HTML</b>!"))
```
Output:
```
This is <b>HTML</b>!
```
### 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的所有替換進行完后的拷貝。
- 庫
- package achive
- package tar
- package zip
- package bufio
- package builtin
- package bytes
- package compress
- package bzip2
- package flate
- package gzip
- package lzw
- package zlib
- package container
- package heap
- package list
- package ring
- package crypto
- package aes
- package cipher
- package des
- package dsa
- package ecdsa
- package elliptic
- package hmac
- package md5
- package rand
- package rc4
- package rsa
- package sha1
- package sha256
- package sha512
- package subtle
- package tls
- package x509
- package pkix
- package database
- package sql
- package driver
- package encoding
- package ascii85
- package asn1
- package base32
- package base64
- package binary
- package csv
- package gob
- package hex
- package json
- package pem
- package xml
- package errors
- package expvar
- package flag
- package fmt
- package go
- package doc
- package format
- package parser
- package printer
- package hash
- package adler32
- package crc32
- package crc64
- package fnv
- package html
- package template
- package image
- package color
- package palette
- package draw
- package gif
- package jpeg
- package png
- package index
- package suffixarray
- package io
- package ioutil
- package log
- package syslog
- package math
- package big
- package cmplx
- package rand
- package mime
- package multipart
- package net
- package http
- package cgi
- package cookiejar
- package fcgi
- package httptest
- package httputil
- package pprof
- package mail
- package rpc
- package jsonrpc
- package smtp
- package textproto
- package url
- package os
- package exec
- package signal
- package user
- package path
- package filepath
- package reflect
- package regexp
- package runtime
- package cgo
- package debug
- package pprof
- package race
- package sort
- package strconv
- package strings
- package sync
- package atomic
- package text
- package scanner
- package tabwriter
- package template
- package time
- package unicode
- package utf16
- package utf8
- package unsafe