Golang unicode.IsDigit() Function with Examples

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

unicode.IsDigit()

The IsDigit() function is an inbuilt function of the unicode package which is used to check whether the given rune r is a decimal digit.

It accepts one parameter (r rune) and returns true if rune r is a decimal digit; false, otherwise.

Syntax:

func IsDigit(r rune) bool

Parameter(s):

  • r : Rune type value to be checked whether it is a decimal digit or not.

Return Value:

The return type of the unicode.IsDigit() function is a bool, it returns true if rune r is a decimal digit; false, otherwise.

Example 1:

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

package main

import (
	"fmt"
	"unicode"
)

func main() {
	fmt.Println(unicode.IsDigit('9'))
	fmt.Println(unicode.IsDigit('0'))
	fmt.Println(unicode.IsDigit('3'))
	fmt.Println(unicode.IsDigit('&'))
	fmt.Println(unicode.IsDigit('x'))
}

Output:

true
true
true
false
false

Example 2:

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

package main

import (
	"fmt"
	"unicode"
)

func main() {
	for i := 48; i <= 57; i++ {
		if unicode.IsDigit(rune(i)) == true {
			fmt.Printf("%c is a digit\n", i)
		} else {
			fmt.Printf("%c is not a digit\n", i)
		}
	}
}

Output:

0 is a digit
1 is a digit
2 is a digit
3 is a digit
4 is a digit
5 is a digit
6 is a digit
7 is a digit
8 is a digit
9 is a digit

Golang unicode Package »




Comments and Discussions!

Load comments ↻





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