×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Golang bytes.Replace() Function with Examples

Golang | bytes.Replace() Function: Here, we are going to learn about the Replace() function of the bytes package with its usages, syntax, and examples.
Submitted by IncludeHelp, on September 25, 2021

bytes.Replace()

The Replace() function is an inbuilt function of the bytes package which is used to get a copy of the byte slice (s) with the first n non-overlapping instances of old replaced by new. Where old is the byte slice to be replaced and new is the byte slice to be replaced with.

It accepts four parameters (s, old, new []byte, n int) and returns a copy of the slice s with the first n non-overlapping instances of old replaced by new.

Note:

  • If old is empty, it matches at the beginning of the slice and after each UTF-8 sequence, yielding up to k+1 replacements for a k-rune slice.
  • If n < 0, there is no limit on the number of replacements i.e., all old byte slices replace with the new byte slice.

Syntax

Replace(s, old, new []byte, n int) []byte

Parameters

  • s : The byte slice in which we have to make the replacement.
  • old : The byte slice to be replaced.
  • new : The byte slice to be replaced with.
  • n : The total number of replacements.

Return Value

The return type of the bytes.Replace() function is a []byte, it returns a copy of the slice s with the first n non-overlapping instances of old replaced by new.

Example 1

// Golang program to demonstrate the
// example of bytes.Replace() function

package main

import (
	"bytes"
	"fmt"
)

func main() {
	fmt.Printf("%s\n", bytes.Replace(
		[]byte("Alone, alone, all all alone"),
		[]byte("all"), []byte("none"), 1))

	fmt.Printf("%s\n", bytes.Replace(
		[]byte("Alone, alone, all all alone"),
		[]byte("all"), []byte("none"), 2))

	fmt.Printf("%s\n", bytes.Replace(
		[]byte("Alone, alone, all all alone"),
		[]byte("alone"), []byte("happy"), -1))
}

Output:

Alone, alone, none all alone
Alone, alone, none none alone
Alone, happy, all all happy

Example 2

// Golang program to demonstrate the
// example of bytes.Replace() function

package main

import (
	"bytes"
	"fmt"
)

func main() {
	var str string
	var old string
	var new string
	var n int
	var result []byte

	str = "Alone, alone, all all alone"
	old = "all"
	new = "none"
	n = 1
	result = bytes.Replace(
		[]byte(str), []byte(old), []byte(new), n)
	fmt.Printf("str: %s\n", str)
	fmt.Printf("old: %s\n", old)
	fmt.Printf("new: %s\n", new)
	fmt.Printf("result: %s\n", result)
	fmt.Println()

	old = "alone"
	new = "happy"
	n = -1
	result = bytes.Replace(
		[]byte(str), []byte(old), []byte(new), n)
	fmt.Printf("str: %s\n", str)
	fmt.Printf("old: %s\n", old)
	fmt.Printf("new: %s\n", new)
	fmt.Printf("result: %s\n", result)
}

Output:

str: Alone, alone, all all alone
old: all
new: none
result: Alone, alone, none all alone

str: Alone, alone, all all alone
old: alone
new: happy
result: Alone, happy, all all happy

Golang bytes Package »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.