【发布时间】:2017-05-14 07:15:45
【问题描述】:
在 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 := strings.NewReplacer(array1,array2)
str = r.Replace(str)
我知道我可以做类似的事情:
str := "hello world"
array1 := []string {"hello","world"}
array2 := []string {"foo","bar"}
r := strings.NewReplacer("hello","foo","world","bar")
str = r.Replace(str)
这可行,但我需要直接使用数组,因为替换数组将动态创建。
【问题讨论】:
标签: php arrays string go replace