Standard library / bytes

bytes.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 []byte, n int) []byte

Parameters & Attributes

NameTypeDescription
s[]byteInput text or sequence.
old[]byteSequence to replace.
new[]byteReplacement sequence.
nintMaximum result count or number of replacements, as specified.

Return Value

[]byte

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 (  "bytes"  "fmt")
func main() {  fmt.Println(string(bytes.Replace([]byte("a-a-a"), []byte("a"), []byte("b"), 2)))}
Go Reference