xxxxxxxxxx
// Program using Replace() to replace strings
package main
import (
"fmt"
"strings"
)
func main() {
text := "car"
fmt.Println("Old String:", text)
// replace r with t
replacedText := strings.Replace(text, "r", "t", 1)
fmt.Println("New String:", replacedText)
}
xxxxxxxxxx
fmt.Println(strings.ReplaceAll("hello wordl", "l", "L"))
fmt.Println(strings.Replace("hello wordl", "l", "L", 3))
xxxxxxxxxx
re := regexp.MustCompile("(?m)[\r\n]+^.*substring.*$")
res := re.ReplaceAllString(s, "")
xxxxxxxxxx
// 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 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)
}
xxxxxxxxxx
fmt.Println(strings.ReplaceAll("hello wordl", "l", "L"))
fmt.Println(strings.Replace("hello wordl", "l", "L", 3))
xxxxxxxxxx
// 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)
}
xxxxxxxxxx
fmt.Println(strings.ReplaceAll("hello wordl", "l", "L"))
fmt.Println(strings.Replace("hello wordl", "l", "L", 3))
xxxxxxxxxx
fmt.Println(strings.ReplaceAll("hello wordl", "l", "L"))
fmt.Println(strings.Replace("hello wordl", "l", "L", 3))