Strings and Text Processing
Replace Text
strings.ReplaceAll returns a new string with every matching piece replaced.
replace
Replacing text creates a changed string while leaving the original value available.
Replace Text
replace_text.go
Replay: real traced execution (multi-file project)
package main
import (
"fmt"
"strings"
)
func main() {
var phrase = "go is fun"
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 = "go is fast"
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 = "java is fun"
updated := strings.ReplaceAll(phrase, "go", "Go")
changed := phrase != updated
fmt.Println("phrase=", phrase)
fmt.Println("updated=", updated)
fmt.Println("changed=", changed)
}
phrase ← "go is fun", updated ← "Go is fun", changed ← true
8func main() {9 var phrase→ "go is fun" = "go is fun" //@phrase="go is fast", "java is fun"10 updated→ "Go is fun" := strings.ReplaceAll(phrase"go is fun", "go", "Go")11 changed→ true := phrase"go is fun" != updated"Go is fun"1213 fmt.Println("phrase=", phrase"go is fun")14 fmt.Println("updated=", updated"Go is fun")15 fmt.Println("changed=", changedtrue)16}outputphrase= go is fun updated= Go is fun changed= true
phrase ← "go is fast", updated ← "Go is fast", changed ← true
8func main() {9 var phrase→ "go is fast" = "go is fast"10 updated→ "Go is fast" := strings.ReplaceAll(phrase"go is fast", "go", "Go")11 changed→ true := phrase"go is fast" != updated"Go is fast"1213 fmt.Println("phrase=", phrase"go is fast")14 fmt.Println("updated=", updated"Go is fast")15 fmt.Println("changed=", changedtrue)16}outputphrase= go is fast updated= Go is fast changed= true
phrase ← "java is fun", updated ← "java is fun", changed ← false
8func main() {9 var phrase→ "java is fun" = "java is fun"10 updated→ "java is fun" := strings.ReplaceAll(phrase"java is fun", "go", "Go")11 changed→ false := phrase"java is fun" != updated"java is fun"1213 fmt.Println("phrase=", phrase"java is fun")14 fmt.Println("updated=", updated"java is fun")15 fmt.Println("changed=", changedfalse)16}outputphrase= java is fun updated= java is fun changed= false