avatarBrandon Atkinson

Summary

The website content discusses the use of the Replacer function in Golang's strings package to efficiently perform multiple string replacements.

Abstract

The article provides a tutorial on string manipulation in Go, emphasizing the ease of replacing characters using the strings package. It starts with simple replacements using strings.ReplaceAll and progresses to more complex scenarios where multiple characters need to be replaced. The author introduces the strings.Replacer as a solution for replacing multiple characters in a string without the need for multiple ReplaceAll calls. This approach is particularly useful for maintaining clean code when dealing with numerous replacements or when the same set of replacements must be applied across different functions. The article concludes by encouraging readers to explore the strings package further and to engage with the Level Up Coding community for more content.

Opinions

  • The author suggests that using strings.ReplaceAll for multiple replacements can become cumbersome and lead to messy code.
  • The strings.Replacer function is presented as a superior alternative for handling multiple string replacements, especially in complex scenarios.
  • The Replacer is praised for its simplicity and effectiveness in maintaining code readability and manageability, even when applied in various functions.
  • The article promotes the Level Up Coding publication and its social channels as valuable resources for the tech community, implying that these platforms offer beneficial content and opportunities for professional growth.

Multi-String Replace in Golang with Replacer

String replacements in Golang are very easy using the strings 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.Replace 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.Replacer 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

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

Golang
Go
Development
Recommended from ReadMedium