golang

How to Use strings.Replace() Function in Golang – Examples

Strings can store large data which can be later used in the code. When it comes to manipulating strings in Golang, we have the strings.Replace() function. With this function, we can easily replace substrings in each string with other substrings, without having to manually manipulate the string yourself. This article covers a detailed description of strings.Replace() in Golang.

What Are strings.Replace() Function in Golang

The Golang programming language provides the strings.Replace() function, which can replace all instances of a specific substring within a string with another substring.

This function takes four arguments:

func Replace(s, old, new string, n int) string
  • s: is the original string that you want to perform the replacement on.
  • old: This is the substring that needs to be replaced by the new one.
  • new: The new substring replaces the old substring.
  • n: is the maximum number of replacements to perform. Code will replace all instances of old substrings with new ones if n is less than 0.

How to Use strings.Replace() in Golang

To use strings.Replace() in Golang first, we have to define a new string from which we want to replace the characters. Next, we have to pass the character which we need to replace as a parameter of this function. The following given code will use the strings.Replace() function and replaces all occurrences of a substring in a string:

package main
import (
    "fmt"
    "strings"
)
func main() {
    str := "This is Golang Tutorial on Linuxhint"
    fmt.Println("Original String: ", str)
    newStr := strings.Replace(str, "i", "X", -1)
    fmt.Println("Replaced String:", newStr)
}

This Go program replaces all occurrences of the letter i in the string str with the letter X using the strings.Replace() function. The modified string is then printed to the console using fmt.Println(). The third parameter of the strings.Replace() function is set to -1, which means that all occurrences of the specified substring will be replaced.

Replace Only the First Occurrence of a Character Using strings.Replace() in Golang

Now if we only want to replace the first occurrence of a substring, we have to set forth an argument to 1:

package main
import (
    "fmt"
    "strings"
)
func main() {
    str := "This is Golang Tutorial on Linuxhint"
fmt.Println("Original String: ", str)
    newStr := strings.Replace(str, "i", "X", 1)
    fmt.Println("Replaced String:", newStr)
}

Here in this code the first occurrence of the letter i in the string str is replaced with the letter X using the strings.Replace() function. The modified string is then printed to the console using fmt.Println(). The third parameter of the strings.Replace() function is set to 1, which means that only the first occurrence of the specified substring will be replaced. If we set the fourth parameter of the function to value -1, then all occurrences will be replaced.

Using strings.ReplaceAll() in Golang

In addition to strings.Replace(), Golang also provides a strings.ReplaceAll() function that replaces all instances of a substring in a string, without the need to specify the maximum number of replacements.

For example, the following is the code that replaces all occurrences of the letter i with the letter X without any need to specify the fourth argument, which is the number of replacements to be done.

package main
import (
    "fmt"
    "strings"
)
func main() {
    str := "This is Golang Tutorial on Linuxhint"
fmt.Println("Original String: ", str)
    newStr := strings.ReplaceAll(str, "i", "X")
    fmt.Println("Replaced String:", newStr)
}

Output

After replacement, the modified string is then printed to the console using fmt.Println().

How to Replace Complete Substring Using strings.Replace() Function in Golang

Now we will look at a Golang code that replaces a complete substring with a new substring:

package main
import (
    "fmt"
    "strings"
)
func main() {
    s := "Golang Tutorial on Linux"
    old := "Linux"
    newstring := "Linuxhint"
    n := 1
    fmt.Println("Original String: ", s)
    testresult := strings.Replace(s, old, newstring, n)
    fmt.Println("Replace Linux with Linuxhint:", testresult)
}

This Go program replaces the first occurrence of the substring Linux in the string s with the substring Linuxhint using the strings.Replace the () function. The modified string is then printed to the console using fmt.Println(). The third parameter of the strings.Replace() function is set to 1, which means that only the first occurrence of the specified substring will be replaced.

Output

The output will display the original string and the modified string with the replacement applied.

Text Description automatically generated with medium confidence

Conclusion

The strings.Replace() function can manipulate strings in Golang by replacing the desired character with a new substring. The strings.Replace() function replaces all instances of character or only replaces the first character occurrence. Similarly, we have strings.ReplaceAll() replaces all occurrences of a character without any need to specify the fourth argument.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.