Skip to content
Related Articles

Related Articles

strings.NewReplacer() Function in Golang With Examples
  • Last Updated : 10 May, 2020

strings.NewReplacer() Function in Golang returns a new Replacer from a list of previous, new string sets. Substitutions are performed within the order they show up within the target string, without overlapping matches. The old string comparisons are done in argument order. The old string comparisons are done in argument order.

Syntax

func NewReplacer(oldnew ...string) *Replacer

Remember NewReplacer panics if given an odd number of arguments.

Example 1:




// Golang program to illustrate the
// strings.NewReplacer() Function
package main
  
import (
    "fmt"
    "strings"
)
  
func main() {
    r := strings.NewReplacer("<", "<", ">", ">")
    fmt.Println(r.Replace("Hey I am <b>GFG</b>!"))
}

Output:

Hey I am <b>GFG</b>!

Example 2:




// Golang program to illustrate the
// strings.NewReplacer() Function
package main
  
import (
    "fmt"
    "strings"
)
  
// Main function
func main() {
  
    // using the function
    r := strings.NewReplacer("(", "easy", ")", "tough;")
    fmt.Println(r.Replace("The dsa course of geeksforgeeks is ( not )"))
}

Output:

The dsa course of geeksforgeeks is easy not tough;
My Personal Notes arrow_drop_up
Recommended Articles
Page :