Java Check Character is ASCII Alphabetic using Apache Common Lang

Tags: Apache Commons Apache Commons Lang CharUtils char Character ASCII

In this Java tutorial we learn how to check whether a character is an ASCII alphabetic character using the CharUtils class of Apache Commons Lang library.

How to add Apache Commons Lang 3 library to your Java project

To use the Apache Commons Lang 3 library in the Gradle build project, add the following dependency into the build.gradle file.

implementation 'org.apache.commons:commons-lang3:3.12.0'

To use the Apache Commons Lang 3 library in the Maven build project, add the following dependency into the pom.xml file.

<dependency>
  <groupId>org.apache.commons</groupId>
  <artifactId>commons-lang3</artifactId>
  <version>3.12.0</version>
</dependency>

To have more information about the Apache Commons Lang 3 library you can visit the library home page at commons.apache.org/proper/commons-lang/

How to check a character is an ASCII alphabetic in Java

The Apache Commons Lang library provides the method CharUtils.isAsciiAlpha() to check whether a given character is an ASCII alphabetic character or not. You can learn how to use the CharUtils.isAsciiAlpha() method in the following example code.

CheckIsAsciiAlphaChar.java

import org.apache.commons.lang3.CharUtils;

public class CheckIsAsciiAlphaChar {
    public static void main(String... args) {
        boolean result1 = CharUtils.isAsciiAlpha('a');
        boolean result2 = CharUtils.isAsciiAlpha('A');
        boolean result3 = CharUtils.isAsciiAlpha('7');
        boolean result4 = CharUtils.isAsciiAlpha('-');
        boolean result5 = CharUtils.isAsciiAlpha('\n');

        System.out.println("Character 'a' is ASCII alphabetic result: " + result1);
        System.out.println("Character 'A' is ASCII alphabetic result: " + result2);
        System.out.println("Character '7' is ASCII alphabetic result: " + result3);
        System.out.println("Character '-' is ASCII alphabetic result: " + result4);
        System.out.println("Character '\\n' is ASCII alphabetic result: " + result5);
    }
}
The output is:
Character 'a' is ASCII alphabetic result: true
Character 'A' is ASCII alphabetic result: true
Character '7' is ASCII alphabetic result: false
Character '-' is ASCII alphabetic result: false
Character '\n' is ASCII alphabetic result: false

Happy Coding 😊