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:
- First parameter is the actual_string.
- Second parameter is the sub_string/character to be replaced.
- Third parameter is the replacing_string or character that replaces the sub_string/character.
- n takes an integer value that specifies number of characters/substring that you want to replace.
Cases:
- If it is 1, first substring/character is replaced.
- If it is 2, first 2 substrings/characters are replaced.
- If it is less than 0, all substrings/characters matched with replacing_string are replaced.
- 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
- Replace "com" with "yes" only for first substring.
- Replace "com" with "yes" only for two substrings.
- Replace "com" with "yes" only for all the existing substrings.
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:
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:
- Only first substring is replaced with "yes".
- First two substrings are replaced with "yes".
- All substrings are replaced with "yes".
Example 2:
Let us consider the string - "Welcome to gkindex.com a computer science community" and
- Replace "m" with "M" only for first character.
- Replace "m" with "M" only for two characters.
- Replace "m" with "M" only for all the existing characters.
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:
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.
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:
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.