如何在同一个字符串中替换多个字符?

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

How to replace multiple characters in the same string?

问题

我对编程并不是很熟悉,所以请原谅我。在处理字符串中替换多个字符的过程中,是否有更优雅的方法?

strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(godiacritics.Normalize(strings.ToLower(articles[i].Name)), "-", "_"), " ", "_"), ",", "_"), ".", ""), "/", ""), "€", ""), "%", ""), "12", "halb"), "14", "viertel")
英文:

I am not very versed in programming in general so please cut me some slack here.
Is there a more elegant way to tackle this process of replacing multiple characters in a string?

strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(godiacritics.Normalize(strings.ToLower(articles[i].Name)), "-", "_"), " ", "_"), ",", "_"), ".", ""), "/", ""), "€", ""), "%", ""), "12", "halb"), "14", "viertel")

答案1

得分: 11

创建一个包含所有可替换对的单个 strings.Replacer

r := strings.NewReplacer(
    "-", "_",
    " ", "_",
    ",", "_",
    ".", "",
    "/", "",
    "€", "",
    "%", "",
    "12", "halb",
    "14", "viertel",
)

然后像这样使用它:

s2 := r.Replace(godiacritics.Normalize(strings.ToLower(articles[i].Name)))

strings.Replacer 在一步中执行所有替换(它一次迭代字符串)。它也可以安全地并发使用,只需创建一次 Replacer 并在需要时重复使用。

以下是用于测试的示例代码:

s := "test- ,./€%:12 14"
s2 := strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(s, "-", "_"), " ", "_"), ",", "_"), ".", ""), "/", ""), "€", ""), "%", ""), "12", "halb"), "14", "viertel")
fmt.Println(s2)

r := strings.NewReplacer(
    "-", "_",
    " ", "_",
    ",", "_",
    ".", "",
    "/", "",
    "€", "",
    "%", "",
    "12", "halb",
    "14", "viertel",
)

s3 := r.Replace(s)
fmt.Println(s3)

输出结果为(在 Go Playground 上尝试):

test___:halb_viertel
test___:halb_viertel
英文:

Create a single strings.Replacer which contains all replaceable pairs:

r := strings.NewReplacer(
	"-", "_",
	" ", "_",
	",", "_",
	".", "",
	"/", "",
	"€", "",
	"%", "",
	"12", "halb",
	"14", "viertel",
)

And use it like this:

s2 := r.Replace(godiacritics.Normalize(strings.ToLower(articles[i].Name)))

strings.Replacer performs all replaces in a single step (it iterates over the string once). It's also safe for concurrent use, create the Replacer onces and reuse it whenever / wherever needed.

Example code to test it:

s := "test- ,./€%:12 14"
s2 := strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(s, "-", "_"), " ", "_"), ",", "_"), ".", ""), "/", ""), "€", ""), "%", ""), "12", "halb"), "14", "viertel")
fmt.Println(s2)

r := strings.NewReplacer(
	"-", "_",
	" ", "_",
	",", "_",
	".", "",
	"/", "",
	"€", "",
	"%", "",
	"12", "halb",
	"14", "viertel",
)

s3 := r.Replace(s)
fmt.Println(s3)

Which outputs (try it on the Go Playground):

test___:halb_viertel
test___:halb_viertel

huangapple
  • 本文由 发表于 2021年10月5日 02:58:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/69441068.html
匿名

发表评论

匿名网友

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

确定