Standard library / strings

strings.Replace()

Replaces up to n non-overlapping matches; negative n replaces all. Empty old matches at rune boundaries and at the start. Expected stdout: 'b-b-a\n'.

Formal Syntax & Structure

go syntaxgo
func Replace(s, old, new string, n int) string

Parameters & Attributes

NameTypeDescription
sstringInput text or sequence.
oldstringSequence to replace.
newstringReplacement sequence.
nintMaximum result count or number of replacements, as specified.

Return Value

string

Replaces up to n non-overlapping matches; negative n replaces all. Empty old matches at rune boundaries and at the start.

Working Example

Example Snippetgo
package main
import (  "fmt"  "strings")
func main() {  fmt.Println(string(strings.Replace("a-a-a", "a", "b", 2)))}
Go Reference