strings.Replace() and strings.ReplaceAll() methods in Golang

These methods help us to replace the old string with the new string and in this post, we are going to look at the Replace() and ReplaceAll() methods of the strings package in detail.

strings.Replace() method

  • Method declaration – func Replace(s, old, new string, n int) string
  • What does it do? It will make a new copy of the string s by replacing the old string with the new string for first n non-overlapping instances
  • What does it return? It will return a new string which will be the copy of the string s but in specific cases, it will be returning the same string ( s )back to avoid allocation of more space for the new string
Code Example
package main

import (
	"fmt"
	"strings"
)

func main() {

	fmt.Println(strings.Replace("hello codekru hello", "hello", "hi", 1)) // replacing only 1 instance
	fmt.Println(strings.Replace("hello codekru hello", "hello", "hi", 2)) // replacing 2 instances

}

Output –

hi codekru hello
hi codekru hi

The What If scenarios

What if we used an empty string as an “old” string in the function’s arguments?
package main

import (
	"fmt"
	"strings"
)

func main() {

	// using an empty string as the old string in the arguments
	fmt.Println(strings.Replace("abc codekru abc", "", "C", 5))

	// using a simple string as the old string
	fmt.Println(strings.Replace("abc codekru abc", "abc", "C", 5))

}

Output –

CaCbCcC Ccodekru abc
C codekru C

So, what happened here?

  • You may have noticed that for the first string, we are getting the new string after every single rune in the string. The answer for this is written in the Go documentation for this method itself

If old is empty, it matches at the beginning of the string and after each UTF-8 sequence, yielding up to k+1 replacements for a k-rune string.

  • So, the Replace() method will put the string into every alternate position ( starting from the start obviously 😛 and ending at the end )
  • And the second string was trying to replace the old string with the new string for the first 5 instances but as there were only 2 instances, so, it replaced those two

What if we use “n” as -1 in the function’s arguments?

If we use -1 in the function’s argument, then it will replace all of the instances of the old string with the new string as illustrated in the below program.

package main

import (
	"fmt"
	"strings"
)

func main() {

	fmt.Println(strings.Replace("abc codekru abc", "", "C", -1))
	fmt.Println(strings.Replace("abc codekru abc", "abc", "C", -1))

}

Output –

CaCbCcC CcCoCdCeCkCrCuC CaCbCcC
C codekru C

Here, all of the instances of the old string have been replaced with the new string.

What if we used an empty string as the “new” string in the function’s arguments?

This will eliminate the old string from the string s. So, this can help in scenarios where we have to eliminate a particular string.

package main

import (
	"fmt"
	"strings"
)

func main() {

	// using an empty string as the new string
	fmt.Println(strings.Replace("abccodekruabc", "abc", "", -1))

}

Output –

codekru

strings.ReplaceAll() method

  • Method declaration – func ReplaceAll(s, old, new string) string
  • What does it do? It will make a new copy of the string s by replacing all of the instances of old string with the new string
package main

import (
	"fmt"
	"strings"
)

func main() {

	// replacing all "abc" with "Hi"
	fmt.Println(strings.ReplaceAll("abc codekru abc", "abc", "Hi"))

}

Output –

Hi codekru Hi

The internal implementation of the ReplaceAll() method –

ReplaceAll() method internally uses the Replace() method where n = -1.

func ReplaceAll(s, old, new string) string {
	return Replace(s, old, new, -1)
}

Please visit this link if you want to know more about the Strings in Go language and its other functions or methods.

Hope you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at [email protected]

Liked the article? Share this on

Leave a Comment

Your email address will not be published.