×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Golang bytes.ContainsAny() Function with Examples

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

bytes.ContainsAny()

The ContainsAny() function is an inbuilt function of the bytes package which is used to check whether any of the UTF-8-encoded code points in the given chars are within byte slice b.

It accepts two parameters (b []byte, chars string) and returns true if any character from chars is within the byte slice b; false, otherwise.

Syntax

func ContainsAny(b []byte, chars string) bool

Parameters

  • b : The main byte slice in which we have to check the chars.
  • chars : The set of the characters (string) to be checked within the byte slice b.

Return Value

The return type of the bytes.ContainsAny() function is a bool, it returns true if any character from chars is within the byte slice b; false, otherwise.

Example 1

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

package main

import (
	"bytes"
	"fmt"
)

func main() {
	// Declaring byte slices
	// Assigning  UTF-8-encoded code points
	// of ABCD EF
	var a = []byte{65, 66, 67, 68, 32, 69, 70}

	// Checking whether chars are within []byte
	fmt.Println(bytes.ContainsAny(a, "ABC"))
	fmt.Println(bytes.ContainsAny(a, "D E"))
	fmt.Println(bytes.ContainsAny(a, "ABCD "))
	fmt.Println(bytes.ContainsAny(a, "XY D"))
	fmt.Println(bytes.ContainsAny(a, "XY"))
}

Output:

true
true
true
true
false

Example 2

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

package main

import (
	"bytes"
	"fmt"
)

func main() {
	fmt.Println(bytes.ContainsAny(
		[]byte("Hello, world!"), "Hi"))
	fmt.Println(bytes.ContainsAny(
		[]byte("Hello, world!"), "work!"))
	fmt.Println(bytes.ContainsAny(
		[]byte("Hello, world!"), "Hi!"))
	fmt.Println(bytes.ContainsAny(
		[]byte("Hello, world!"), "xy"))
}

Output:

true
true
true
false

Golang bytes Package »




Comments and Discussions!

Load comments ↻





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