How to Replace Characters 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 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 which 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, oldsre, 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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)
}

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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)
  
}

chevron_right


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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.