What is CharUtils.isAsciiAlpha in Java?
The isAsciiAlpha method is a static method of the CharUtils class that checks whether the input character is alphabetic.
The method returns true if the character’s ASCII value is in the following list of ranges:
| Range | Description |
|---|---|
| 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 isAsciiAlpha(char ch)
Parameters
ch: character we check to determine whether it is an alphabetic character.
Return value
The function returns true if the character is an alphabet with ASCII values between 65-90 or 97-122. 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.isAsciiAlpha() for the character '%s' is %s", c1, CharUtils.isAsciiAlpha(c1));System.out.println();c1 = 'È';System.out.printf("The output of CharUtils.isAsciiAlpha() for the character '%s' is %s", c1, CharUtils.isAsciiAlpha(c1));System.out.println();c1 = '9';System.out.printf("The output of CharUtils.isAsciiAlpha() for the character '%s' is %s", c1, CharUtils.isAsciiAlpha(c1));System.out.println();c1 = 't';System.out.printf("The output of CharUtils.isAsciiAlpha() for the character '%s' is %s", c1, CharUtils.isAsciiAlpha(c1));System.out.println();}}
Expected output
The output of CharUtils.isAsciiAlpha() for the character 'F' is true
The output of CharUtils.isAsciiAlpha() for the character 'È' is false
The output of CharUtils.isAsciiAlpha() for the character '9' is false
The output of CharUtils.isAsciiAlpha() for the character 't' is true
Explanation
-
c1=FThe method returns
trueas the character is an alphabet. -
c1=ÈThe method returns
falseas the character is not an alphabet. -
c1=9The method returns
falseas the character is not an alphabet. -
c1=tThe method returns
trueas the character is an alphabet.