Golang String Replace Method
In Golang, you can use the Replace function from the strings package to perform find and replace operations. The usage of the Replace function is as follows:
strings.Replace(s, old, new, n)
In this case, s is the string on which to perform the search and replace operation, old is the substring to be replaced, new is the new substring after replacement, and n specifies the number of replacements (-1 indicates all matches to be replaced).
The sample code is as follows:
package main
import (
"fmt"
"strings"
)
func main() {
str := "Hello, World!"
newStr := strings.Replace(str, "World", "Golang", 1)
fmt.Println(newStr) // Output: Hello, Golang!
}
In this example, we will replace the word “World” in the string “Hello, World!” with “Golang” and output the updated result.