Menu Close

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

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

Replace() function of bytes package is used to replace the bytes with new bytes within matching bytes. In this function, you pass n i.e number of matching places to be replaced with new bytes.

Function prototype:

func Replace(s, old, new []byte, n int) []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
n: Number of old matching slice of bytes to be replaced.

Returns:
1: It returns a copy of the slice s with the first n non-overlapping instances of old replaced by new.
2: If n < 0, Then all the matching bytes in s will be replaced with new one.
3: 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 first n 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.Replace(s, old, _new, 2)

Output:
"Hello Indoor Hello Indoor Hello Techie"

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

Output:
"Hello Hello Hello Hello Hello Hello"

3)
s = []byte("Hello Techie Hello Techie Hello Techie")
old = []byte("Techie")
_new = []byte("Hello")

Output:
"Hello Hello Hello 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.Replace(s, old, _new, 2))

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

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

}

Output:

% go run sample.go

1) After replacement: "Hello Indoor Hello Indoor Hello Techie"
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 Techie"
5) After replacement: "IndoorHIndoorello Techie Hello Techie Hello Techie"
6) After replacement: "Hello  Hello  Hello "
7) After replacement: "Hello Techie Hello Techie Hello Techie"

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. Required fields are marked *