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

huangapple go评论75阅读模式
英文:

Golang replace array elements by array elements in string

问题

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

这样可以工作,但我需要直接使用数组,因为替换数组将动态创建。

英文:

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.

答案1

得分: 8

我相信,如果你首先将两个数组合并成一个替换数组,然后只运行一次替换器来处理目标字符串,性能会更好。这是因为strings.Replacer在各种情况下都进行了优化,并且替换算法只需要运行一次。

可以使用以下代码实现:

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

希望对你有帮助!

英文:

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

答案2

得分: 2

如下是翻译好的内容:

如何使用地图?

str := "Lorem Ipsum只是虚拟的。Lorem Ipsum是印刷文本。Lorem Ipsum是排版行业。";

replace := map[string]string{
    "Lorem": "LoremReplaced",
    "Ipsum": "IpsumReplaced",
    "printing": "printingReplaced",
}

for s, r := range replace {
    str = strings.Replace(str, s, r, -1)
}
英文:

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

答案3

得分: 1

我找到的解决方案如下:

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)

可以创建一个函数:

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
}

用法:

str := "hello world"
array1 := []string{"hello", "world"}
array2 := []string{"foo", "bar"}
str = str_replace(str, array1, array2)
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.

huangapple
  • 本文由 发表于 2016年12月30日 07:21:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/41388576.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定