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) []byteParameters & Attributes
| Name | Type | Description |
|---|---|---|
| s | []byte | Input text or sequence. |
| old | []byte | Sequence to replace. |
| new | []byte | Replacement sequence. |
| n | int | Maximum 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)))}