What is CharUtils.isAsciiAlphanumeric in Java?
The isAsciiAlphanumeric method is a static method of the CharUtils class that checks whether the input character is a 7-bit alphabetic or a 7-bit numeric value. This means that the method checks if the ASCII value of the character is in one of the following ranges listed in the table:
| Range | Description |
|---|---|
| 48 to 57 | Numbers from 0 to 9 |
| 65 to 90 | Lowercase alphabets from a to z |
| 97 to 122 | Uppercase alphabets from A to Z |
How to import CharUtils
CharUtils is defined in the Apache Commons Lang package. Apache Commons Lang can be added to the Maven project by adding the following dependency to the pom.xml file:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
For other versions of the commons-lang package, refer to the Maven Repository.
We can import the CharUtils class as follows:
import org.apache.commons.lang3.CharUtils;
Syntax
public static boolean isAsciiAlphanumeric( char ch)
Parameters
ch: character we check to determine whether it is an alphanumeric character
Return value
The function returns true if the character is in any of the specified ranges. Otherwise, it returns false.
Code
import org.apache.commons.lang3.CharUtils;public class Main {public static void main(String[] args) {char c1 = 'F';System.out.printf("The output of CharUtils.isAsciiAlphanumeric() for the character '%s' is %s", c1, CharUtils.isAsciiAlphanumeric(c1));System.out.println();c1 = 'p';System.out.printf("The output of CharUtils.isAsciiAlphanumeric() for the character '%s' is %s", c1, CharUtils.isAsciiAlphanumeric(c1));System.out.println();c1 = '9';System.out.printf("The output of CharUtils.isAsciiAlphanumeric() for the character '%s' is %s", c1, CharUtils.isAsciiAlphanumeric(c1));System.out.println();c1 = '@';System.out.printf("The output of CharUtils.isAsciiAlphanumeric() for the character '%s' is %s", c1, CharUtils.isAsciiAlphanumeric(c1));System.out.println();c1 = 'È';System.out.printf("The output of CharUtils.isAsciiAlphanumeric() for the character '%s' is %s", c1, CharUtils.isAsciiAlphanumeric(c1));System.out.println();}}
Expected output
The output of CharUtils.isAsciiAlphanumeric() for the character 'F' is true
The output of CharUtils.isAsciiAlphanumeric() for the character 'p' is true
The output of CharUtils.isAsciiAlphanumeric() for the character '9' is true
The output of CharUtils.isAsciiAlphanumeric() for the character '@' is false
The output of CharUtils.isAsciiAlphanumeric() for the character 'È' is false
Explanation
-
c1='F'The method returns
trueas the numerical (ASCII in this case) value of the character ranges from 65 to 90. -
c1='p'The method returns
trueas the numerical (ASCII in this case) value of the character ranges from 97 to 122. -
c1='9'The method returns
trueas the numerical (ASCII in this case) value of the character ranges from 48 to 57. -
c1='@'The method returns
falseas the character is not a digit or an alphabet. -
c1='È'The method returns
falseas the character is not a digit or an alphabet.