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 |
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;
public static boolean isAsciiAlpha(char ch)
ch
: character we check to determine whether it is an alphabetic character.The function returns true
if the character is an alphabet with ASCII values between 65-90 or 97-122. Otherwise, it returns false
.
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();}}
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
c1
= F
The method returns true
as the character is an alphabet.
c1
= È
The method returns false
as the character is not an alphabet.
c1
= 9
The method returns false
as the character is not an alphabet.
c1
= t
The method returns true
as the character is an alphabet.