isdigit() in Java
The isDigit() function is an inbuilt method of Java that checks whether a given character is a digit.
The isDigit() method takes a character or codepoint value to be checked for a digit as an argument and returns a boolean value.
Syntax of isDigit() in Java
Let's understand the syntax of isDigit() function in java. The isDigit() in java uses the character or codepoint value that is to be checked for a digit.
Syntax:
Note:
codepoint value in java can be any value that is Unicode codespace
Exceptions of isDigit() in Java
The isDigit() method in Java does not throw any exceptions.
Parameters of isDigit() in Java
Let's understand the parameters of isDigit() in java. isDigit() function takes a character that has to be checked if it is a digit or not.
-
char ch :
It is the character value that is to be tested. -
int codepoint :
It is the integer value that is to be tested, some Unicode character ranges that contain digits are:- From ‘\u0030’ to ‘\u0039’ are ISO-LATIN-1 digits (‘0’ through ‘9’).
- From ‘\u0660’ to ‘\u0669’ are Arabic-Indic digits ('٠' through '٩').
- From ‘\u06F0’ to ‘\u06F9’ are Extended Arabic-Indic digits ('۱' through '۹').
- From ‘\u0966’ to ‘\u096F’ are Devanagari digits ('०' through '९')
- From ‘\uFF10’ to ‘\uFF19’ are Fullwidth digits (‘0’ through ‘9’).
There can be many other character ranges that can contain digits.
Return Values of isDigit() in Java
Return Type : boolean
isDigit() in Java returns true if the given character is a digit or not, else false.
Example of isDigit() in Java
Let us look at a short example of how the isDigit function works, In this example, we are simply passing character values in the isDigit() function and will fetch the result accordingly.
Output :
Both the characters are simply checked whether they are digits or not.
What is isDigit() Method in Java ?
Let's suppose you are asked to classify all the given characters and check for the digits. So, in this case, instead of checking the ASCII values of all the characters and comparing them, you can just use the inbuilt isDigit() java function to check for all the digits.
As discussed above, we can define the isDigit() function as an inbuilt method for checking whether the passed character is a digit. This method takes a character as a parameter and returns a boolean value stating whether the parameter passed is a digit.
A character is called a digit if Character's general category is DECIMAL_DIGIT_NUMBER returned by Character.getType(ch).
some Unicode character ranges that contain digits are :
- From ‘\u0030’ to ‘\u0039’ are ISO-LATIN-1 digits (‘0’ through ‘9’).
- From ‘\u0660’ to ‘\u0669’ are Arabic-Indic digits.
- From ‘\u06F0’ to ‘\u06F9’ are Extended Arabic-Indic digits.
- From ‘\u0966’ to ‘\u096F’ are Devanagari digits.
- From ‘\uFF10’ to ‘\uFF19’ are Fullwidth digits.
More Examples of isDigit() in Java
Example - 1 : isDigit() with Special Characters
Let us look at an example where we will pass and check special characters using the isDigit() function.
Output :
All three characters are simply checked whether they are digits or not.
Example - 2 : isDigit() Function to Check if a Digit is Present in a String or not
In this example, we will have an array of strings and will check whether that digit is present in each string.
Output :
For each string value, we are getting its output.
Example - 3 : isDigit() with Codepoint Value
Let's take a look at an example where we will be passing an integer value as the parameter for the isDigit() function; whenever the integer passed lies between the Unicode values for the digits, this method will return true for the following.
Output :
We can see that 57 true is returned because it lies between the charger range of digits. Similarly, for 95, this method returns false and for 9, it returns false.
Example - 4 : isDigit() with Codepoint Unicodes Passed as Parameters
Let's take a look at an example where we will be passing Unicode values as parameters for the isDigit() function; so whenever the Unicode passed lies between the Unicode values for the digits, this method will return true for the following.
Output :
We can see that for 0x59, false is returned because it does not lie between the character range of digits. Similarly, for 0x0668, this method returns true as this value lies between the range.
Example 5 : Use the Character.isDigit(char ch) Method to Decode a String Encoded with Run-length Encoding
We can use this function to create a code for decoding a string encoded with Run-length encoding.
Let us take an example. Supposing the string given is "a4b4c4", the decoded version of this string will be "aaaabbbbcccc".
While iterating over the string, we will check if a character is a digit or not, based on the returned value, we will do the task accordingly.
Output :
First we Iterated over the string. If the digit is found, we have to add myCh the digit times to result, else, Set myCh to the character found. Hence, We have successfully encoded the string according to a given value.
Conclusion
- The isDigit() function is an inbuilt method of Java.
- The isDigit() function is used to check whether the given character is a digit or not.
- The isDigit() function takes a character or codepoint value which is to be checked for a digit.
- The isDigit() function returns a boolean value.
- The isDigit() function can be used for :
- checking if a character is a digit or not.
- checking if a digit is present in a string or not.
- checking if a codepoint value is a digit or not.
- to decode a string encoded with Run-length encoding.