英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论