Go strings Package Replace()

Introduction

To replace a smaller string in a bigger string with some other string, use the Replace function.

In Go, Replace() takes a number indicating how many times to do the replacement.

Pass -1 to do it as many times as possible:

package main//from   www .  j ava  2 s  .  co m

     import ( 
         "fmt" 
         "strings" 
     ) 

     func main() { 
         // func Replace(s, old, new string, n int) string 
         fmt.Println(strings.Replace("aaaa", "a", "b", 2)) 
         // => "bbaa" 
     } 



PreviousNext

Related