Golang bytes.ContainsRune Function

The bytes.ContainsRune function in Golang is part of the bytes package and is used to check if a specific Unicode code point (rune) is present within a byte slice. This function is particularly useful when you need to determine whether a particular character is found in a byte slice, which may represent a string.

Table of Contents

  1. Introduction
  2. bytes.ContainsRune Function Syntax
  3. Examples
    • Basic Usage
    • Checking for a Specific Character
    • Working with Non-ASCII Characters
  4. Real-World Use Case
  5. Conclusion

Introduction

The bytes.ContainsRune function checks whether a specific rune (Unicode code point) is present within a given byte slice. A rune represents a single Unicode character, which could be one or more bytes long. The function returns true if the rune is found and false otherwise.

bytes.ContainsRune Function Syntax

The syntax for the bytes.ContainsRune function is as follows:

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

Parameters:

  • b: The byte slice in which to search.
  • r: The rune (Unicode code point) to search for within b.

Returns:

  • bool: true if the rune is found within b, false otherwise.

Examples

Basic Usage

This example demonstrates how to use the bytes.ContainsRune function to check if a byte slice contains a specific rune.

Example

package main

import (
	"bytes"
	"fmt"
)

func main() {
	// Define the main byte slice
	data := []byte("Hello, Golang!")

	// Define the rune to search for
	r := 'G'

	// Check if the rune is contained within the byte slice
	if bytes.ContainsRune(data, r) {
		fmt.Println("The byte slice contains the rune.")
	} else {
		fmt.Println("The byte slice does not contain the rune.")
	}
}

Output:

The byte slice contains the rune.

Checking for a Specific Character

This example shows how to check for the presence of a specific character, such as a punctuation mark, within a byte slice.

Example

package main

import (
	"bytes"
	"fmt"
)

func main() {
	// Define the main byte slice
	data := []byte("Hello, Golang!")

	// Define the rune for a comma
	r := ','

	// Check if the comma is contained within the byte slice
	if bytes.ContainsRune(data, r) {
		fmt.Println("The byte slice contains a comma.")
	} else {
		fmt.Println("The byte slice does not contain a comma.")
	}
}

Output:

The byte slice contains a comma.

Working with Non-ASCII Characters

This example demonstrates how to use bytes.ContainsRune to check for the presence of non-ASCII characters, such as accented characters or symbols, within a byte slice.

Example

package main

import (
	"bytes"
	"fmt"
)

func main() {
	// Define a byte slice with non-ASCII characters
	data := []byte("Café")

	// Define the rune for the accented character 'é'
	r := 'é'

	// Check if the rune is contained within the byte slice
	if bytes.ContainsRune(data, r) {
		fmt.Println("The byte slice contains the rune 'é'.")
	} else {
		fmt.Println("The byte slice does not contain the rune 'é'.")
	}
}

Output:

The byte slice contains the rune 'é'.

Explanation:

  • bytes.ContainsRune checks whether a specific Unicode character (rune) is present in the byte slice.
  • This function works with both ASCII and non-ASCII characters, making it versatile for various text processing tasks.

Real-World Use Case

Validating Text Input for Specific Characters

In real-world applications, bytes.ContainsRune can be used to validate text input by checking for the presence of specific characters, such as ensuring that an email address contains the '@' symbol.

Example: Validating an Email Address

package main

import (
	"bytes"
	"fmt"
)

func main() {
	// Example email address
	email := []byte("user@example.com")

	// Check if the email address contains the '@' symbol
	if bytes.ContainsRune(email, '@') {
		fmt.Println("The email address is valid.")
	} else {
		fmt.Println("The email address is invalid.")
	}
}

Output:

The email address is valid.

Explanation:

  • The example demonstrates how bytes.ContainsRune can be used to check for the presence of a specific character (in this case, '@') to validate an email address.

Conclusion

The bytes.ContainsRune function in Go is used for checking the presence of specific Unicode characters within a byte slice. Whether you're working with ASCII or non-ASCII characters, this function provides a straightforward way to determine if a particular character is present. 

It is particularly useful for text processing tasks, such as validating input, searching for specific symbols, or handling internationalized text. By leveraging bytes.ContainsRune, you can efficiently work with text data and ensure it meets your specific criteria.

Comments