Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

string

replace

go

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.

like image 835
SeattleOrBayArea Avatar asked Dec 11 '15 02:12

SeattleOrBayArea


People also ask

How do you replace all occurrences of substring in a string?

To replace all occurrences of a substring in a string by a new one, you can use the replace() or replaceAll() method: replace() : turn the substring into a regular expression and use the g flag. replaceAll() method is more straight forward.

How do I replace multiple characters in a string?

Use the replace() method to replace multiple characters in a string, e.g. str. replace(/[. _-]/g, ' ') . The first parameter the method takes is a regular expression that can match multiple characters.

Does replace replace all occurrences?

The replaceAll() method will substitute all instances of the string or regular expression pattern you specify, whereas the replace() method will replace only the first occurrence.


1 Answers

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

like image 165
icza Avatar answered Sep 29 '22 22:09

icza