Menu Close

Go – How to replace all bytes in slice of bytes in go

bytes.ReplaceAll() function is used to replace all bytes in slice of bytes in go. It returns the copy of modified slice of bytes.

ReplaceAll() function of bytes package is used to replace all the matching bytes with new bytes.

Function prototype:

func ReplaceAll(s, old, new []byte) []byte

Input parameters:
s: Slice of bytes
old: Slice of bytes to be replaced in s
new: Slice of bytes will replace old bytes in s

Returns:
1: It returns a copy of the slice s with all non-overlapping instances of old replaced by new.
2: If old is empty, it matches at the beginning of the slice .

Explanation on how to replace n bytes in slice of bytes in go:

  • This function replaces all non-overlapping instances of old replaced by new.

Example:

1)
s := []byte("Hello Techie Hello Techie Hello Techie")
old := []byte("Techie")
_new := []byte("Indoor")
bytes.ReplaceAll(s, old, _new)

Output:
"Hello Indoor Hello Indoor Hello Indoor"

2)
s = []byte("Hello Techie Hello Techie Hello Techie")
old = []byte("Techie")
_new = []byte("Hello")
bytes.ReplaceAll(s, old, _new)

Output:
"Hello Hello Hello Hello Hello Hello"

3)
s = []byte("Hello Techie Hello Techie Hello Techie")
old = []byte("Techie")
_new = []byte("")
bytes.ReplaceAll(s, old, _new)

Output:
Hello  Hello  Hello "

Example with code:

package main

import (
  "fmt"
  "bytes"
)

func main() {

    s := []byte("Hello Techie Hello Techie Hello Techie")
    old := []byte("Techie")
    _new := []byte("Indoor")
    fmt.Printf("1) After replacement: %q", bytes.ReplaceAll(s, old, _new))

  
    s = []byte("Hello Techie Hello Techie Hello Techie")
    old = []byte("Techie")
    _new = []byte("Hello")
    fmt.Printf("\n2) After replacement: %q", bytes.ReplaceAll(s, old, _new))
    
    
    s = []byte("Hello Techie Hello Techie Hello Techie")
    old = []byte("Techie")
    _new = []byte("Hello")
    fmt.Printf("\n3) After replacement: %q", bytes.ReplaceAll(s, old, _new))
    

    s = []byte("Hello Techie Hello Techie Hello Techie")
    old = []byte("Techie")
    _new = []byte("John")
    fmt.Printf("\n4) After replacement: %q", bytes.ReplaceAll(s, old, _new))
    
    s = []byte("Hello Techie Hello Techie Hello Techie")
    old = []byte("")
    _new = []byte("Indoor")
    fmt.Printf("\n5) After replacement: %q", bytes.ReplaceAll(s, old, _new))
    
    
    s = []byte("Hello Techie Hello Techie Hello Techie")
    old = []byte("Techie")
    _new = []byte("")
    fmt.Printf("\n6) After replacement: %q", bytes.ReplaceAll(s, old, _new))
    
    
    s = []byte("Hello Techie Hello Techie Hello Techie")
    old = []byte("Techie")
    _new = []byte("Hello")
    fmt.Printf("\n7) After replacement: %q", bytes.ReplaceAll(s, old, _new))

}

Output:

% go run sample.go

1) After replacement: "Hello Indoor Hello Indoor Hello Indoor"
2) After replacement: "Hello Hello Hello Hello Hello Hello"
3) After replacement: "Hello Hello Hello Hello Hello Hello"
4) After replacement: "Hello John Hello John Hello John"
5) After replacement: "IndoorHIndooreIndoorlIndoorlIndooroIndoor IndoorTIndooreIndoorcIndoorhIndooriIndooreIndoor IndoorHIndooreIndoorlIndoorlIndooroIndoor IndoorTIndooreIndoorcIndoorhIndooriIndooreIndoor IndoorHIndooreIndoorlIndoorlIndooroIndoor IndoorTIndooreIndoorcIndoorhIndooriIndooreIndoor"
6) After replacement: "Hello  Hello  Hello "
7) After replacement: "Hello Hello Hello Hello Hello Hello"

To learn more about golang, Please refer given below link:

References:

https://golang.org/pkg/

Posted in bytes, golang, packages

Leave a Reply

Your email address will not be published.

Contact Us