Strings.Replacer: how to replace all substrings at once?

14,516

Solution 1

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 '_'.

Solution 2

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.

Share:
14,516
SeattleOrBayArea
Author by

SeattleOrBayArea

Updated on July 24, 2022

Comments

  • SeattleOrBayArea
    SeattleOrBayArea almost 2 years

    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 Replacers.

  • SeattleOrBayArea
    SeattleOrBayArea over 8 years
    thanks for your detailed answer, tips about replacer, good to keep this in mind while adding handler.