Check If the Rune is a Decimal Digit or not in Golang
Last Updated :
27 Sep, 2019
Rune is a superset of ASCII or it is an alias of int32. It holds all the characters available in the world’s writing system, including accents and other diacritical marks, control codes like tab and carriage return, and assigns each one a standard number. This standard number is known as a Unicode code point or rune in the Go language.
You are allowed to check the given rune is a decimal digit or not with the help of IsDigit() function. This function returns true if the given rune is a decimal digit, or return false if the given rune is a non-decimal digit. This function is defined under Unicode package, so for accessing this method you need to import the Unicode package in your program.
Syntax:
func IsDigit(r rune) bool
The return type of this function is boolean. Let us discuss this concept with the help of given examples:
Example 1:
package main
import (
"fmt"
"unicode"
)
func main() {
rune_1 := 'g'
rune_2 := 'e'
rune_3 := '1'
rune_4 := '4'
rune_5 := 'S'
res_1 := unicode.IsDigit(rune_1)
res_2 := unicode.IsDigit(rune_2)
res_3 := unicode.IsDigit(rune_3)
res_4 := unicode.IsDigit(rune_4)
res_5 := unicode.IsDigit(rune_5)
fmt.Println(res_1)
fmt.Println(res_2)
fmt.Println(res_3)
fmt.Println(res_4)
fmt.Println(res_5)
}
|
Output:
false
false
true
true
false
Example 2:
package main
import (
"fmt"
"unicode"
)
func main() {
val := []rune{ 'g' , 'E' , '3' , 'K' , '1' }
for i := 0; i < len(val); i++ {
if unicode.IsDigit(val[i]) == true {
fmt.Println( "It is a decimal digit" )
} else {
fmt.Println( "It is not a decimal digit" )
}
}
}
|
Output:
It is not a decimal digit
It is not a decimal digit
It is a decimal digit
It is not a decimal digit
It is a decimal digit
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...