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 string package main import ( "fmt" "strings") // Main function func 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 string package main import ( "fmt" "strings") // Main function func 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
Recommended Posts:
- How to replace a specified element in slice of bytes in Golang?
- How to replace all the elements in slice of bytes in Golang?
- strings.Replace() Function in Golang With Examples
- Check if the given characters is present in Golang String
- How to Count the Number of Repeated Characters in a Golang String?
- How to count specific characters present in the slice in Golang?
- Program to find the Length of the longest substring without repeating characters in Golang
- string.Contains Function in Golang with Examples
- Converting a string variable into Boolean, Integer or Float type in Golang
- Golang Program that uses String Switch
- How to Trim a String in Golang?
- How to convert a string in uppercase in Golang?
- How to convert a string in lower case in Golang?
- Repeating a String for Specific Number of Times in Golang
- How to Split a String in Golang?
- How to find the index value of specified string in Golang?
- How to find the last index value of specified string in Golang?
- Check if the String starts with specified prefix in Golang
- Check if the String ends with specified suffix in Golang
- How to check String for equality under Unicode case folding in Golang?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

