GoLang String - Replace()

In this tutorial, we will learn how to replace the specified character/substring with new character/substring up to specified number using Replace() method.

In the Go Language Replace() is used to replace the specified character/substring with new character/substring up to specified number.

let us explore the uses case with help of simple examples.

String Replace()

Replace() is used to replace the specified character/substring with new character/substring up to specified number.

Syntax:


strings.Replace(actual_string, sub_string/character,replacing_string,n)

It takes four parameters.

Parameters:

  1. First parameter is the actual_string.
  2. Second parameter is the sub_string/character to be replaced.
  3. Third parameter is the replacing_string or character that replaces the sub_string/character.
  4. n takes an integer value that specifies number of characters/substring that you want to replace.

Cases:

  1. If it is 1, first substring/character is replaced.
  2. If it is 2, first 2 substrings/characters are replaced.
  3. If it is less than 0, all substrings/characters matched with replacing_string are replaced.
  4. If it is equal to 0, no substrings/characters will be replaced.

It is important to specify the "strings" package in import.

Example 1:

Let us consider the string - "Welcome to gkindex.com a computer science community" and

  1. Replace "com" with "yes" only for first substring.
  2. Replace "com" with "yes" only for two substrings.
  3. Replace "com" with "yes" only for all the existing substrings.
CopiedCopy Code

package main

import (
    "fmt"
"strings")

func main() {
  
  // Consider the string
    var actual_string = "Welcome to gkindex.com a computer science community"
 
    fmt.Println("String: ", actual_string)

      // replace "com" with "yes" only for first substring..
     fmt.Println( strings.Replace(actual_string,"com","yes",1))
     
      // replace "com" with "yes" only for  two substrings
     fmt.Println( strings.Replace(actual_string,"com","yes",2))
     
     // replace "com" with "yes" only for all substrings
     fmt.Println( strings.Replace(actual_string,"com","yes",-1))
}

Output:

CopiedCopy Code

String:  Welcome to gkindex.com a computer science community
Welyese to gkindex.com a computer science community
Welyese to gkindex.yes a computer science community
Welyese to gkindex.yes a yesputer science yesmunity

Explanation for the above output:

  1. Only first substring is replaced with "yes".
  2. First two substrings are replaced with "yes".
  3. All substrings are replaced with "yes".

Example 2:

Let us consider the string - "Welcome to gkindex.com a computer science community" and

  1. Replace "m" with "M" only for first character.
  2. Replace "m" with "M" only for two characters.
  3. Replace "m" with "M" only for all the existing characters.
CopiedCopy Code

package main

import (
    "fmt"
"strings")

func main() {
  
  // Consider the string
    var actual_string = "Welcome to gkindex.com a computer science community"
 
    fmt.Println("String: ", actual_string)

     
      // replace "m" with "M" only for first character.
     fmt.Println( strings.Replace(actual_string,"m","M",1))
     
      // replace "m" with "M" only for two characters
     fmt.Println( strings.Replace(actual_string,"m","M",2))
     
   // replace "m" with "M" only for all characters
     fmt.Println( strings.Replace(actual_string,"m","M",-1))
}

Output:

CopiedCopy Code

String:  Welcome to gkindex.com a computer science community
WelcoMe to gkindex.com a computer science community
WelcoMe to gkindex.coM a computer science community
WelcoMe to gkindex.coM a coMputer science coMMunity

Example 3:

Let us consider the string - "Welcome to gkindex.com a computer science community" and replace 'm' with 'M' by specifying n=0.

CopiedCopy Code

package main

import (
    "fmt"
"strings")

func main() {
  
  // Consider the string
    var actual_string = "Welcome to gkindex.com a computer science community"
 
    fmt.Println("String: ", actual_string)

     
      // replace "m" with "M"
     fmt.Println( strings.Replace(actual_string,"m","M",0))
     
}

Output:

CopiedCopy Code

String:  Welcome to gkindex.com a computer science community
Welcome to gkindex.com a computer science community

Explanation for the above output:

We can see that there is no replacement done as n is 0.

Conclusion

Now we know how to replace the specified character/substring with new character/substring up to specified number using Replace() function and understood the implementation with working examples.