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

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.

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

  $ result  = str_replace($ str,$ array1,$ array2); 
  code>  pre> 
 
 

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

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

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

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

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

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)
}

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.

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)
}