• golang 中 strings 包的 Replace 用法介绍笔记


    函数申明:

    func Replace(s, old, new string, n int) string

    官方描述为:

    返回将s中前n个不重叠old子串都替换为new的新字符串,如果n<0会替换所有old子串。

    示例代码为:

    func main() {
        // non-overlapping: "123" repeat 6 times in s
        s := "123lafaldsjglad123lkfasdf123djfal123lkdjga123lksjfla123l"
        old := "123"
        new := "888"
    
    fmt.Println("non-overlapping: ")
     // n < 0 ,用 new 替换所有匹配上的 old;n=-1:  888lafaldsjglad888lkfasdf888djfal888lkdjga888lksjfla888l
    fmt.Println("n=-1: ", strings.Replace(s, old, new, -1 ))
    
    // 不替换任何匹配的 old;n=0:  123lafaldsjglad123lkfasdf123djfal123lkdjga123lksjfla123l
    fmt.Println("n=0: ", strings.Replace(s, old, new, 0 )) 
    
    // 用 new 替换第一个匹配的 old;n=1:  888lafaldsjglad123lkfasdf123djfal123lkdjga123lksjfla123l
    fmt.Println("n=1: ", strings.Replace(s, old, new, 1 ))
    
    // 用 new 替换前 5 个匹配的 old(实际多于 5 个);n=5:  888lafaldsjglad888lkfasdf888djfal888lkdjga888lksjfla123l
    fmt.Println("n=5: ", strings.Replace(s, old, new, 5 )) 
    
    // 用 new 替换前 7 个匹配上的 old(实际没那么多);n=7:  888lafaldsjglad888lkfasdf888djfal888lkdjga888lksjfla888l
    fmt.Println("n=7: ", strings.Replace(s, old, new, 7 )) 
    
    // overlapping:
    s = "888888888888888888"
    old = "888"
    new = "666"
    fmt.Println("overlapping: ")
    
    // n < 0 ,用 new 替换所有匹配上的 old;n=-1:  666666666666666666
    fmt.Println("n=-1: ", strings.Replace(s, old, new, -1 )) 
    
    // 不替换任何匹配的 old;n=0:  888888888888888888
    fmt.Println("n=0: ", strings.Replace(s, old, new, 0 )) 
    
    // 用 new 替换第一个匹配的 old;n=1:  666888888888888888
    fmt.Println("n=1: ", strings.Replace(s, old, new, 1 )) 
    
     // 用 new 替换前 5 个匹配的 old(实际多于 5 个);n=5:  666666666666666888
    fmt.Println("n=5: ", strings.Replace(s, old, new, 5 ))
    
     // 用 new 替换前 7 个匹配上的 old(实际没那么多);n=7:  666666666666666666
    fmt.Println("n=7: ", strings.Replace(s, old, new, 7 ))
    }
    
  • 相关阅读:
    delete、truncate、drop的区别
    Java闭包
    visio 画网格图
    GPU服务器中了木马病毒
    visio 同时标注上下标
    自动缩放字体
    latex 图片
    多GPU训练
    texstudio 外部查看器错误
    Linux lsof命令详解
  • 原文地址:https://www.cnblogs.com/haima/p/12697644.html
Copyright © 2020-2023  润新知