Multi-String Replace in Golang with Replacer
![](https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*N4ePzXiF_PJD7GZn431bSw.png)
String replacements in Golang are very easy using the stri
ngs package. From their docs: “Package strings implements simple functions to manipulate UTF-8 encoded strings”. Take the following sentence:
"Hello World!"
Now let’s say my product owner thinks saying “Hello World” with an exclamation mark is just too much, and if I find that I need to replace the !
with a .
which is very easy to do in Golang:
myString := "Hello World!"
myString = strings.ReplaceAll(myString, "!", ".")
fmt.Println(myString)
This will result in Hello World.
being printed out, you can test it here. As you can see, using strings.ReplaceAll
is very straight-forward:
- Provide the string you want to operate on
- Provide the character to replace
- Provide the replacement value
And now you know how to do string replacement in Golang, congratulations! Side note, there is also a strings.Repl
ace where you can control how many times the replacement takes place in your string.
When It’s Not So Simple
Replacement is great, but what if your string looks something like this:
"Hello_World!_This.sentence.has_chars_I.want_to.remove!"
Our product owner says this sentence will not fly, and we need to replace and _
and any .
with a space. Now that we know how to use strings.ReplaceAll
we can easily do something like this:
myString := "Hello_World!_This.sentence.has_chars_I.want_to.remove!"
myString = strings.ReplaceAll(myString, "_", " ")
myString = strings.ReplaceAll(myString, ".", " ")
fmt.Println(myString)
This will result in Hello World! This sentence has chars I want to remove!
being printed out, you can test it here. I this example we still have a fairly simple sentence, with two different characters to replace. This isn’t so bad and we can achieve our goal using two strings.ReplaceAll
statements fairly easily.
But, what if we needed to replace 5 characters, or 10, or 20? We could have 20 strings.ReplaceAll
statements and it would work fine, but its cumbersome. Or what if I needed to perform this replacement in multiple functions? This could quickly become a mess. Let’s look at a better way.
Using the Replacer
The strings
package includes a strings.Repla
cer option. From their docs: “Replacer replaces a list of strings with replacements. It is safe for concurrent use by multiple goroutines”. Basically, you can specify a list of replacements to perform, then do them all at once against your target string:
myString := "Hello_World!_This.sentence.has_chars_I.want_to.remove!"
replacer := strings.NewReplacer("_", " ", ".", " ")
myString = replacer.Replace(myString)
fmt.Println(myString)
This will result in Hello World! This sentence has chars I want to remove!
being printed out, you can test it here. Using the Replacer function is very straight-forward as well:
- Create a new Replacer with all the characters you want to replace
- Apply it to your string
In this example it did not save us any lines of code, but that’s OK, it’s a simple example. But this sets us up for way more complex scenarios such as:
- Needing to replace 5, 10, 20 different characters
- Needing to pass the replacer to any function that needs to perform the exact same replacements
Wrapping Up
This post showcased as very simple usage of the Replacer functionality found in the strings
package. Using this you can very quickly replace multiple characters across a single string with ease. Hope it helps!
References
- Strings Package: https://pkg.go.dev/strings
- ReplaceAll: https://pkg.go.dev/strings#ReplaceAll
- Replace: https://pkg.go.dev/strings#Replace
- Replacer: https://pkg.go.dev/strings#Replacer
Level Up Coding
Thanks for being a part of our community! More content in the Level Up Coding publication. Follow: Twitter, LinkedIn, Newsletter Level Up is transforming tech recruiting ➡️ Join our talent collective