Golang bytes.ContainsRune() Function

Golang bytes.ContainsRune() function is “used to check whether the rune r contained in the UTF-8-encoded byte slice b.”

Syntax

func ContainsRune(b []byte, r rune) bool

Parameters

  1. b: The main byte slice in which we have to check the rune value.
  2. r: The rune value to be checked within the byte slice b.

Return value

It returns true if rune r is within the byte slice b; false, otherwise.

Example 1

package main

import (
  "bytes"
  "fmt"
)

func main() {
  fmt.Println(bytes.ContainsRune([]byte("I like seafood."), 'f'))
  fmt.Println(bytes.ContainsRune([]byte("I like seafood."), 'ö'))
}

Output

true
false

Example 2

package main

import (
  "bytes"
  "fmt"
)

func main() {
  b := []byte("hello world")

  // Check for the presence of the rune 'l'
  if bytes.ContainsRune(b, 'l') {
    fmt.Println("The byte slice contains the rune 'l'.")
  } else {
    fmt.Println("The byte slice doesn't contain the rune 'l'.")
  }

  // Check for the presence of the rune 'z'
  if bytes.ContainsRune(b, 'z') {
    fmt.Println("The byte slice contains the rune 'z'.")
  } else {
    fmt.Println("The byte slice doesn't contain the rune 'z'.")
  }
}

Output

The byte slice contains the rune 'l'.
The byte slice doesn't contain the rune 'z'.

That’s it!

Related posts

Golang bytes.Contains()

Golang bytes.Clone()

Golang bytes.Equal()

Golang bytes.Compare()

Golang bytes.containsAny()

Golang bytes.ContainsFunc()