How to replace all occurrences of substring in a string in Go

2 Answers

0 votes
package main
 
import (
    "fmt"
    "strings"
)
 
func main() {
    s := "go php PHP GO Python go GO"
     
    s = strings.ReplaceAll(s, "GO", "Java")
     
    fmt.Println(s)
} 
     
 
   
      
/*
run:
       
go php PHP Java Python go Java
   
*/

 



answered Aug 19, 2020 by avibootz
0 votes
package main
 
import (
    "fmt"
    "strings"
)
 
func main() {
    s := "go php PHP GO Python go GO"
     
    s = strings.Replace(s, "GO", "Java", -1)
     
    fmt.Println(s)
} 
     
 
   
      
/*
run:
       
go php PHP Java Python go Java
   
*/

 



answered Aug 19, 2020 by avibootz
...