Golang String Replace

Saturday, December 19, 2020 • One minute to read

Golang package strings provides Replace and ReplaceAll functions to replace the substring in the string.

Replace

Syntax

func Replace(s, old, new string, n int) string

n is the number of first occurrence of the substring.
Replace n first occurrence of the substring.

If n < 0, then it will replace all the occurrence.

package main

import (
	"fmt"
	"strings"
)

func main() {
    str := "Th0is is an en0c0oded 0000 message"

	newStr := strings.Replace(str, "0", "", 3)

	fmt.Printf("Original String: %s\nAfter Replace: %s\n", str, newStr)

	// n = -1
	newStr =  strings.Replace(str, "0", "", -1)
	fmt.Printf("After Replace with -1: %s", newStr)
}

Output

Original String: Th0is is an en0c0oded 0000 message
After Replace: This is an encoded 0000 message
After Replace with -1: This is an encoded  message

ReplaceAll

Syntax

func ReplaceAll(s, old, new string) string
str := "Th0is is an en0c0oded 0000 message"

newStr := strings.ReplaceAll(str, "0", "")

fmt.Printf("Original String: %s\nAfter Replace: %s\n", str, newStr)

Output

Original String: Th0is is an en0c0oded 0000 message
After Replace: This is an encoded  message

Final Words

It was always suggested to use ReplaceAll instead of Replace with -1. To support your -1 you might need to add a comment.
Keep the code self explanatory.


Golang2020to-the-pointgolang

Check if Key Exists in Map in Golang

Check if a character is a number in Golang