GO语言"strings"包中"Replace"函数的用法及代码示例。
用法:
func Replace(s, old, new string, n int) string
Replace 返回字符串 s 的副本,其中前 n 个不重叠的 old 实例替换为 new。如果 old 为空,它将在字符串的开头和每个 UTF-8 序列之后匹配,产生最多 k+1 个替换 k-rune 字符串。如果 n < 0,则替换次数没有限制。
例子:
package main
import (
"fmt"
"strings"
)
func main() {
fmt.Println(strings.Replace("oink oink oink", "k", "ky", 2))
fmt.Println(strings.Replace("oink oink oink", "oink", "moo", -1))
}
输出:
oinky oinky oink moo moo moo
相关用法
- GO Replace用法及代码示例
- 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用法及代码示例
注:本文由纯净天空筛选整理自golang.google.cn大神的英文原创作品 Replace。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。