strings.Replacer:位置相关的bug /功能?

时间:2013-10-24 21:09:12

标签: go

我得到了输出:

Hello World
Hello 

使用以下代码:

package main

import(
    "fmt"
    "strings"
)

func main(){
    s := "Hello World"
    fmt.Println(strings.NewReplacer("Hello","").Replace(s))
    fmt.Println(strings.NewReplacer("World","").Replace(s))
}

这是一个错误吗?有没有更好的方法来删除子串?

2 个答案:

答案 0 :(得分:2)

这是一个错误。现在修正了它。

https://groups.google.com/forum/#!topic/golang-nuts/CNdpwbCSbHM

这是另一种删除子串的方法:

package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Println(strings.Replace("Hello World", "Hello", "", 1))
}

答案 1 :(得分:1)

我不是专家,但对我来说这看起来像个错误。

这有效:

package main

import(
    "fmt"
    "strings"
)

func main(){
    s := "Hello World"
    fmt.Println(strings.NewReplacer("Hello"," ").Replace(s))
    fmt.Println(strings.NewReplacer("World","").Replace(s))
}

输出:

   World 

Hello

也许有一个空字符串关键字?

即便如此:

fmt.Println(strings.NewReplacer("ello", "").Replace(s))

这也有效:

fmt.Println(strings.NewReplacer("Hello","", "Hi", "").Replace(s))

正如提到的直翅目,似乎单一替换是特殊的套装和马车。