Strings and Text Processing
Replace Text
strings.ReplaceAll returns a new string with every matching piece replaced.
Replace Text
replace_text.go
package main
import (
"fmt"
"strings"
)
func main() {
var phrase =
updated := strings.ReplaceAll(phrase, "go", "Go")
changed := phrase != updated
fmt.Println("phrase=", phrase)
fmt.Println("updated=", updated)
fmt.Println("changed=", changed)
}
package main
import (
"fmt"
"strings"
)
func main() {
var phrase =
updated := strings.ReplaceAll(phrase, "go", "Go")
changed := phrase != updated
fmt.Println("phrase=", phrase)
fmt.Println("updated=", updated)
fmt.Println("changed=", changed)
}
package main
import (
"fmt"
"strings"
)
func main() {
var phrase =
updated := strings.ReplaceAll(phrase, "go", "Go")
changed := phrase != updated
fmt.Println("phrase=", phrase)
fmt.Println("updated=", updated)
fmt.Println("changed=", changed)
}
replace
Replacing text creates a changed string while leaving the original value available.