英文:
Strings.Replacer: how to replace all substrings at once?
问题
我正在尝试使用Replacer
来替换字符串中的多个不同字符,但在替换一个字符串时遇到了问题。输出中有两个下划线而不是一个,如果我尝试使用其他的Replacer
来替换,那么它无法完全替换。
请在Go Playground上尝试以下代码:
package main
import (
"fmt"
"strings"
)
// 期望的输出是 emp_my_stats
func main() {
var input string = "/v1.0/emp/emp_1/my_stats"
replacer := strings.NewReplacer("/v1.0/", "", "/", "_", "emp_1", "")
// replacer := strings.NewReplacer("/v1.0/", "", "/", "_", "/emp_1", "")
output := replacer.Replace(input)
fmt.Printf("output %v", output)
}
我可以使用多个Replacer
等方式,但我真的希望能够在一次遍历/或一条语句中完成。有没有建议如何清晰地实现这个目标?我的目标是高效(这将经常执行,所以很重要,尽管这些字符串很短),并且不使用多个Replacer
。
英文:
I am trying to replace multiple different characters from a string using Replacer
but having issues replacing one string. Output has two underscores instead of one, and if I try replacing using other Replacer
, then it cannot replace it entirely.
Try the code on the Go Playground:
package main
import (
"fmt"
"strings"
)
//Expecting output to be emp_my_stats
func main() {
var input string = "/v1.0/emp/emp_1/my_stats"
replacer := strings.NewReplacer("/v1.0/", "", "/", "_", "emp_1", "")
// replacer := strings.NewReplacer("/v1.0/", "", "/", "_", "/emp_1", "")
output := replacer.Replace(input)
fmt.Printf("output %v", output)
}
I can use multiple Replacer
etc. but would really like to do it in one pass / or one statement.
Any suggestions how so do it cleanly? My goal is to be efficient (this will be done frequently, so important although these strings are short) and also to not use multiple Replacer
s.
答案1
得分: 27
你没有指定其他可能的输入,但从这个代码片段来看:
input := "/v1.0/emp/emp_1/my_stats"
你需要将"emp"
和"my_stats"
这两部分用下划线'_'
连接起来。根据你尝试使用的替换器,"/v1.0/"
和"/emp_1/"
这两部分是固定的,所以你可以使用一个替换器来完成:
replacer := strings.NewReplacer("/v1.0/", "", "/emp_1/", "_")
完整的示例代码如下:
input := "/v1.0/emp/emp_1/my_stats"
replacer := strings.NewReplacer("/v1.0/", "", "/emp_1/", "_")
output := replacer.Replace(input)
fmt.Println("Output:", output)
输出结果(在Go Playground上尝试):
Output: emp_my_stats
注意:
你提到你需要经常执行这个操作,并且希望它高效。所以请确保只创建一个Replacer
,并在需要进行替换的时候重复使用它(例如,你可以将它存储在一个全局变量中,在初始化时只初始化一次)。根据你的输入,它看起来像是某个URL的路径,很可能你希望在HTTP处理程序中执行这个操作,而HTTP处理程序可能会并发地在多个goroutine中运行。根据Replacer
的文档,从多个goroutine中使用Replacer
是安全的:
> 它可以被多个goroutine并发使用。
注意 #2:
如果输入中的"/v1.0/"
和"/emp_1/"
这两部分不是固定的,你就不能使用Replacer
来解决你的问题。在这种情况下,你可以使用正则表达式,或者更高效的解决方案是通过'/'
将字符串拆分,并使用'_'
将相关部分连接起来。
英文:
You haven't specified any other possible inputs, but it looks that from this:
input := "/v1.0/emp/emp_1/my_stats"
You need the "emp"
and "my_stats"
parts connected with an underscore '_'
. And judging by your attempt for the replacer, "/v1.0/"
and "/emp_1/"
parts are static, so you can simply do this with one replacer:
replacer := strings.NewReplacer("/v1.0/", "", "/emp_1/", "_")
Complete example:
input := "/v1.0/emp/emp_1/my_stats"
replacer := strings.NewReplacer("/v1.0/", "", "/emp_1/", "_")
output := replacer.Replace(input)
fmt.Println("Output:", output)
Output (try it on the Go Playground):
Output: emp_my_stats
Notes:
You mentioned you have to do this frequently and you want this to be efficient. So make sure you only create one Replacer
, and reuse it whenever you need to do the replacing (e.g. you can store it in a global variable initialized once). Judging from your input, it looks like it is the path of some URL, and most likely you want to do this is HTTP handlers, which may run concurrently on multiple goroutines. It is safe to use a Replacer
from multiple goroutines, quoting from the doc of Replacer
:
> It is safe for concurrent use by multiple goroutines.
Notes #2:
If the "/v1.0/"
and "/emp_1/"
parts in the input are not static, you can't really solve your problem with a Replacer
. In this case you may use regexp or as a more efficient solution, splitting the string by '/'
and joining the relevant parts with '_'
.
答案2
得分: 2
strings.Split可以实现你想要的效果:
package main
import (
"fmt"
"strings"
)
//期望输出为 emp_my_stats
func main() {
input := "/v1.0/emp/emp_1/my_stats"
xs := strings.Split(input, "/")
fmt.Println(xs)
fmt.Println(xs[4])
fmt.Println(xs[3] + xs[4])
fmt.Println(xs[3][:4] + xs[4])
fmt.Println(xs[3][:len(xs[3])-1] + xs[4])
}
上述代码的输出结果为:
[ v1.0 emp emp_1 my_stats]
my_stats
emp_1my_stats
emp_my_stats
emp_my_stats
你可以在Go Playground上看到它的运行效果。
英文:
strings.Split might accomplish what you want:
package main
import (
"fmt"
"strings"
)
//Expecting output to be emp_my_stats
func main() {
input := "/v1.0/emp/emp_1/my_stats"
xs := strings.Split(input, "/")
fmt.Println(xs)
fmt.Println(xs[4])
fmt.Println(xs[3] + xs[4])
fmt.Println(xs[3][:4] + xs[4])
fmt.Println(xs[3][:len(xs[3])-1] + xs[4])
}
The above produces this output:
[ v1.0 emp emp_1 my_stats]
my_stats
emp_1my_stats
emp_my_stats
emp_my_stats
You can see it here in action on the go playground.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论