- The java.lang.Character.isDigit(char ch) is an inbuilt method in java which determines whether a specified character is a digit or not. There are few conditions that a character must accomplish to be accepted as a digit. That is if the general category type of a character, provided by Character.getType(ch), is DECIMAL_DIGIT_NUMBER, then the character is a digit.
Some Unicode character ranges that contain digits:
From ‘\u0030’ to ‘\u0039’ : ISO-LATIN-1 digits (‘0’ through ‘9’)
From ‘\u0660’ to ‘\u0669’ : Arabic-Indic digits
From ‘\u06F0’ to ‘\u06F9’ : Extended Arabic-Indic digits
From ‘\u0966’ to ‘\u096F’ : Devanagari digits
From ‘\uFF10’ to ‘\uFF19’ : Fullwidth digitsApart from the above mentioned ranges, many other character ranges contain digits as well.
Syntax:
public static boolean isDigit(char ch)
Parameter: This method accepts character parameter ch as an argument, which is to be tested.
Return value:This method returns a boolean value. It returns True if ch is digit, else False.
Note: This method cannot handle supplementary characters. To support all Unicode characters, including supplementary characters, use the isDigit(int) method.
Below programs illustrate the above method:
Program 1:// Java program to illustrate the
// Character.isDigit() method
Â
Âimport
java.util.*;
import
java.lang.*;
Â
Âpublic
class
GFG {
Â
ÂÂ Â Â Â
public
static
void
main(String[] args)
   Â
{
Â
ÂÂ Â Â Â Â Â Â Â
// two characters
       Â
char
c1 =
'A'
, c2 =
'4'
;
Â
ÂÂ Â Â Â Â Â Â Â
// Function to check if the character
       Â
// is digit or not
       Â
System.out.println(
           Â
c1 +
" is a digit -> "
           Â
+ Character.isDigit(c1));
       Â
System.out.println(
           Â
c2 +
" is a digit -> "
           Â
+ Character.isDigit(c2));
   Â
}
}
Output:A is a digit -> false 4 is a digit -> true
Program 2:
// Java program to illustrate the
// Character.isDigit() method
Â
Âimport
java.util.*;
import
java.lang.*;
Â
Âpublic
class
GFG {
Â
ÂÂ Â Â Â
public
static
int
search_digit(String s)
   Â
{
Â
ÂÂ Â Â Â Â Â Â Â
// Function to check if is digit
       Â
// is found or not
       Â
for
(
int
i =
0
; i < s.length(); i++) {
           Â
if
(Character.isDigit(
                   Â
s.charAt(i))
               Â
==
true
) {
               Â
// return position of digit
               Â
return
i +
1
;
           Â
}
       Â
}
       Â
// return 0 if digit not present
       Â
return
0
;
   Â
}
Â
ÂÂ Â Â Â
public
static
void
main(String[] args)
   Â
{
Â
ÂÂ Â Â Â Â Â Â Â
// Array of strings
       Â
String[] arr = {
"ABC4DEF"
,
"QWERTY"
};
Â
ÂÂ Â Â Â Â Â Â Â
// To store the position of digit
       Â
int
index =
0
;
Â
ÂÂ Â Â Â Â Â Â Â
// Traverse the array arr[] to find digit
       Â
// within it's elements
       Â
for
(String x : arr) {
           Â
index = search_digit(x);
           Â
if
(index !=
0
) {
               Â
System.out.println(
                   Â
"Digit found at : "
                   Â
+ (index)
                   Â
+
"th position."
);
           Â
}
           Â
else
{
               Â
System.out.println(
                   Â
"Digit not present."
);
           Â
}
       Â
}
   Â
}
}
Output:Digit found at : 4th position. Digit not present.
- The java.lang.Character.isDigit(int codePoint) is an inbuilt method in java which determines whether the specified Unicode code point character of integer type is a digit or not.
There are few conditions that a character must accomplish to be accepted as a digit. That is if the general category type of a character, provided by getType(codepoint), is a DECIMAL_DIGIT_NUMBER, then the character is a digit. Some Unicode character ranges that contain digits:
From ‘\u0030’ to ‘\u0039’ : ISO-LATIN-1 digits (‘0’ through ‘9’)
From ‘\u0660’ to ‘\u0669’ : Arabic-Indic digits
From ‘\u06F0’ to ‘\u06F9’ : Extended Arabic-Indic digits
From ‘\u0966’ to ‘\u096F’ : Devanagari digits
From ‘\uFF10’ to ‘\uFF19’ : Fullwidth digitsApart from the above mentioned ranges, many other character ranges contain digits as well.
Syntax:
public static boolean isDigit(int codePoint)
Parameter: This method accepts unicode character parameter codePoint of integer type as an argument, which is to be tested.
Return value: This method returns a boolean value. It returns True if the specified character is digit, else it returns False.
Below programs illustrate the above method:
Program 1:// This program demonstrates the use of
// isDigit(int codePoint) method of Character class.
import
java.util.*;
Â
Âpublic
class
GFG {
   Â
public
static
void
main(String[] args)
   Â
{
       Â
// create codePoints
       Â
int
cp1 =
57
;
       Â
int
cp2 =
84
;
Â
ÂÂ Â Â Â Â Â Â Â
// Check whether the codePoints
       Â
// are digit or not.
       Â
System.out.println(
           Â
"The codePoint cp1 is a digit -> "
           Â
+ Character.isDigit(cp1));
       Â
System.out.println(
           Â
"The codePoint cp2 is a digit -> "
           Â
+ Character.isDigit(cp2));
   Â
}
}
Output:The codePoint cp1 is a digit -> true The codePoint cp2 is a digit -> false
Program 2:
// This program demonstrates the use of
// isDigit(int codePoint) method of
// Character class.
import
java.util.*;
Â
Âpublic
class
Main {
   Â
public
static
void
main(String[] args)
   Â
{
       Â
// create codePoints
       Â
int
cp1 =
0x50
;
       Â
int
cp2 =
0x06f8
;
Â
ÂÂ Â Â Â Â Â Â Â
// Check whether the codePoints
       Â
// are digit or not.
       Â
System.out.println(
           Â
"The codePoint cp1 is a digit -> "
           Â
+ Character.isDigit(cp1));
       Â
System.out.println(
           Â
"The codePoint cp2 is a digit -> "
           Â
+ Character.isDigit(cp2));
   Â
}
}
Output:The codePoint cp1 is a digit -> false The codePoint cp2 is a digit -> true
Reference: https://docs.oracle.com/javase/8/docs/api/java/lang/Character.html#isDigit-char-