douwen8118
2016-12-29 23:21
浏览 741
已采纳

Golang用字符串中的数组元素替换数组元素

In PHP we can do something like :

$result = str_replace($str,$array1,$array2);

Where $array1 and $array2 are array of elements, this makes php replace all array1 elements by array2 elements. Is there any equivalent to this using the Golang? I have tried the same php approach but it did not work :

str := "hello world"
array1 :=  []string {"hello","world"}
array2 :=  []string {"foo","bar"}
r := strings.NewReplacer(array1,array2)
str = r.Replace(str)

I know I can do something like :

str := "hello world"
array1 :=  []string {"hello","world"}
array2 :=  []string {"foo","bar"}
r := strings.NewReplacer("hello","foo","world","bar")
str = r.Replace(str)

That would work but I need to use arrays directly because the arrays of replacements will be created dynamically.

图片转代码服务由CSDN问答提供 功能建议

在PHP中,我们可以执行以下操作:

  $ result  = str_replace($ str,$ array1,$ array2); 
   
 
 

其中$ array1和$ array2是元素数组,这使得php将所有array1元素替换为array2 元素。 使用Golang是否有与此等效的元素? 我尝试了相同的php方法,但没有成功:

  str:=“ hello world” 
array1:= [] string {“ hello”,“ world”}  
array2:= [] string {“ foo”,“ bar”} 
r:=字符串。NewReplacer(array1,array2)
str = r.Replace(str)
   
 \  n 

我知道我可以做类似的事情:

  str:=“ hello world” 
array1:= [] string {“ hello”,“ world”} \  narray2:= [] string {“ foo”,“ bar”} 
r:= strings.NewReplacer(“ hello”,“ foo”,“ world”,“ bar”)
str = r.Replace(str)
    
 
 

那行得通,但我需要直接使用数组,因为替换数组将动态创建。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • douguaidian8021 2016-12-30 03:03
    已采纳

    I believe performance would be much better if you first zip both arrays in a single replacement array and then run just one replacer pass over the target string because strings.Replacer is fairly optimized for various cases and because the replacement algorithm would need to be run only once.

    Something like this would do:

    func zip(a1, a2 []string) []string {
        r := make([]string, 2*len(a1))
        for i, e := range a1 {
            r[i*2] = e
            r[i*2+1] = a2[i]
        }
        return r
    }
    
    func main() {
        str := "hello world"
        array1 := []string{"hello", "world"}
        array2 := []string{"foo", "bar"}
        str = strings.NewReplacer(zip(array1, array2)...).Replace(str)
        fmt.Println(str)
    }
    
    已采纳该答案
    打赏 评论
  • dovrhg3246 2016-12-29 23:33

    The solution I have found is the following :

    str := "hello world"
    array1 :=  []string {"hello","world"}
    array2 :=  []string {"foo","bar"}
    
    for i,toreplace := range array1{
        r := strings.NewReplacer(toreplace,array2[i])
        str = r.Replace(str)
    }
    
    fmt.Println(str)
    

    A function can be created

    func str_replace(str string, original []string, replacement []string) string {
    
        for i,toreplace := range original{
            r := strings.NewReplacer(toreplace,replacement[i])
            str = r.Replace(str)
        }
    
        return str
    }
    

    Usage :

    str := "hello world"
    array1 :=  []string {"hello","world"}
    array2 :=  []string {"foo","bar"}
    str = str_replace(str,array1,array2)
    fmt.Println(str)
    

    Any more elegant solution is more than welcome.

    打赏 评论
  • doushun1870 2017-01-21 23:45

    How about a map?

    str := "Lorem Ipsum is simply dummy. Lorem Ipsum is text of the printing. Lorem Ipsum is typesetting industry.";
    
    replace := map[string]string{
        "Lorem": "LoremReplaced",
        "Ipsum": "IpsumReplaced",
        "printing": "printingReplaced",
    }
    
    for s, r := range replace {
        str = strings.Replace(str, s, r, -1)
    }
    
    打赏 评论

相关推荐 更多相似问题