Golang unicode.IsOneOf() Function with Examples

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

unicode.IsOneOf()

The IsOneOf() function is an inbuilt function of the unicode package which is used to check whether the given rune r is a member of one of the ranges (given RangeTable).

It accepts two parameters (ranges []*RangeTable, r rune) and returns true if rune r is a member of one of the ranges; false, otherwise.

Note: The In() function is better than the IsOneOf() and should be used.

Syntax:

func IsOneOf(ranges []*RangeTable, r rune) bool

Parameter(s):

  • ranges : One or more range tables.
  • r : Rune type value to be checked whether it is a member or one of the ranges or not.

Return Value:

The return type of the unicode.IsOneOf() function is a bool, it returns true if rune r is a member of one of the ranges; false, otherwise.

Example 1:

// Golang program to demonstrate the
// example of unicode.IsOneOf() Function

package main

import (
	"fmt"
	"unicode"
)

func main() {
	var letter_digit = []*unicode.RangeTable{
		unicode.Letter,
		unicode.Digit,
		{R16: []unicode.Range16{{'_', '_', 1}}},
	}

	result := unicode.IsOneOf(letter_digit, 'A')
	fmt.Println(result)

	result = unicode.IsOneOf(letter_digit, 'Ɐ')
	fmt.Println(result)

	result = unicode.IsOneOf(letter_digit, '9')
	fmt.Println(result)

	result = unicode.IsOneOf(letter_digit, '&')
	fmt.Println(result)
}

Output:

true
true
true
false

Example 2:

// Golang program to demonstrate the
// example of unicode.IsOneOf() Function

package main

import (
	"fmt"
	"unicode"
)

func main() {
	var letter_digit = []*unicode.RangeTable{
		unicode.Letter,
		unicode.Digit,
		{R16: []unicode.Range16{{'_', '_', 1}}},
	}

	// constant with mixed type runes
	const mixed = "5Ὂg̀9! ℃ᾭGx989tuv"

	for _, c := range mixed {
		if unicode.IsOneOf(letter_digit, c) == true {
			fmt.Printf("%c is either letter or digit\n", c)
		} else {
			fmt.Printf("%c is neither letter nor digit\n", c)
		}
	}
}

Output:

5 is either letter or digit
Ὂ is either letter or digit
g is either letter or digit
̀ is neither letter nor digit
9 is either letter or digit
! is neither letter nor digit
  is neither letter nor digit
℃ is neither letter nor digit
ᾭ is either letter or digit
G is either letter or digit
x is either letter or digit
9 is either letter or digit
8 is either letter or digit
9 is either letter or digit
t is either letter or digit
u is either letter or digit
v is either letter or digit

Golang unicode Package »





Comments and Discussions!

Load comments ↻






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