How to Replace Characters in Golang String?
Last Updated :
28 Sep, 2022
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:
C
// 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:
C
// 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 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)
}
C
// 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 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 article of the Go string is a replacement
Strings(After replacement)
Result 1: Welcome to GFG for GFG
Result 2: This is THE article of THE Go string is a replacement
Similar Reads
How to Generate Random String/Characters in Golang? We might want to generate random strings or even sets of characters to perform some operations or add certain string-related functionality into an application. We can randomly get a character from a set of characters, randomize the order of characters of a given string or generate a random string. W
6 min read
How to Split a String in Golang? In Go language, strings differ from other languages like Java, C++, and Python. A string in Go is a sequence of variable-width characters, with each character represented by one or more bytes using UTF-8 encoding. In Go, you can split a string into a slice using several functions provided in the str
3 min read
How to Reverse a String in Golang? Given a string and the task is to reverse the string. Here are a few examples. Approach 1: Reverse the string by swapping the letters, like first with last and second with second last and so on. Example: C // Golang program to reverse a string package main // importing fmt import "fmt" // function,
2 min read
How to check the specified rune in Golang String? 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 the Go strings, you are allowed to check the given string contain the spec
3 min read
How to convert a string in lower case in Golang? In Go, converting a string to lowercase is a common operation that can be accomplished easily using the strings package. This package provides various string manipulation functions, including the ToLower function, which converts all Unicode characters in a string to their lowercase equivalent.Exampl
2 min read
How to Create Modules in Golang? Go has included support for versioned modules as proposed here since 1.11 with the initial prototype as vgo. The concept of modules in Go was introduced to handle the problem of dependencies within your Go applications. Several packages are collected and combined to form a module which is stored in
3 min read