How to Replace Characters in Golang String?

23 Apr, 2020
In Go language, strings are different from other languages like Java, C++, Python, etc. It is a sequence of variable-width characters where each and every character is represented by one or more bytes using UTF-8 Encoding.In Go strings, you are allowed to replace characters in the given string using the given functions. These functions are defined under strings package, so you have to import strings package in your program to access these functions:
1. Replace: This function returns a copy of the string that contains a new string that is created by replacing the elements in the old string. If the given old string is empty, then it matches at the starting of the string and after each UTF-8 sequence it is yielding up to m+1 replacement for m-rune string. And if the value of the n is less than zero, then this function can replace any number of elements in the given string (without any limit).
Syntax:
func Replace(str, oldstr, newstr string, m int) string
Here, str is the original string, oldstr is the string which you wants to replace, newstr is the new string which replaces the oldstr, and n is the number of times the oldstr replace.
Example:
// Go program to illustrate how to// replace characters in the given stringpackage main  import (    "fmt"    "strings")  // Main functionfunc main() {      // Creating and initializing strings    str1 := "Welcome to Geeks for Geeks"    str2 := "This is the article of the Go string is a replacement"      fmt.Println("Original strings")    fmt.Println("String 1: ", str1)    fmt.Println("String 2: ", str2)      // Replacing strings    // Using Replace() function    res1 := strings.Replace(str1, "e", "E", 3)    res2 := strings.Replace(str2, "is", "IS", -2)    res3 := strings.Replace(str1, "Geeks", "GFG", 0)      // Displaying the result    fmt.Println("\nStrings(After replacement)")    fmt.Println("Result 1: ", res1)    fmt.Println("Result 2: ", res2)    fmt.Println("Result 3: ", res3)}
Output:
Original strings
String 1:  Welcome to Geeks for Geeks
String 2:  This is the article of the Go string is a replacement

Strings(After replacement)
Result 1:  WElcomE to GEeks for Geeks
Result 2:  ThIS IS the article of the Go string IS a replacement
Result 3:  Welcome to Geeks for Geeks

2. ReplaceAll: This function is used to replace all the old string with a new string. If the given old string is empty, then it matches at the starting of the string and after each UTF-8 sequence it is yielding up to M+1 replacement for M-rune string.
Syntax:
func ReplaceAll(str, oldstr, newstr string) string
Here, str is the original string, oldstr is the string which you wants to replace, and newstr is the new string which replaces the oldstr. Let us discuss this concept with the help of an example:
Example:
// Go program to illustrate how to// replace characters in the given stringpackage main  import (    "fmt"    "strings")  // Main functionfunc main() {      // Creating and initializing strings    str1 := "Welcome to Geeks for Geeks"    str2 := "This is the is the article of the Go string is a replacement"      fmt.Println("Original strings")    fmt.Println("String 1: ", str1)    fmt.Println("String 2: ", str2)      // Replacing strings    //  Using ReplaceAll() function    res1 := strings.ReplaceAll(str1, "Geeks", "GFG")    res2 := strings.ReplaceAll(str2, "the", "THE")      // Displaying the result    fmt.Println("\nStrings(After replacement)")    fmt.Println("Result 1: ", res1)    fmt.Println("Result 2: ", res2)  }
Output:
Original strings
String 1:  Welcome to Geeks for Geeks
String 2:  This is the is the article of the Go string is a replacement

Strings(After replacement)
Result 1:  Welcome to GFG for GFG
Result 2:  This is THE is THE article of THE Go string is a replacement

Go Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Golang Maps
Interfaces in Golang
Data Types in Go
Slices in Golang
How to Parse JSON in Golang?
How to Trim a String in Golang?
How to Assign Default Value for Struct Field in Golang?
Pointers in Golang
Structures in Golang
Constants- Go Language