What is StringUtils.isAlpha in Java?

isAlpha is a static method of the StringUtils class that checks whether a given string contains only Unicode characters. The function returns false if the input string is null or empty.

How to import StringUtils

The definition for StringUtils can be found in the Apache Commons Lang package. The Apache Commons Lang can be added to the Maven project by adding the using dependency to the pom.xml file.

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

Note: For other versions of the commons-lang package, review the Maven Repository.

You can import the StringUtils class as follows:

import org.apache.commons.lang3.StringUtils;

Syntax


public static boolean isAlpha(final CharSequence cs)

Parameters

  • CharSequence cs: Character sequence to check.

Return value

The function returns true if the character sequence contains only Unicode characters. Otherwise, it returns false.

Code

import org.apache.commons.lang3.StringUtils;
public class Main{
public static void main(String[] args){
String characterSequence = "";
System.out.println(StringUtils.isAlpha(characterSequence));
characterSequence = "ñå";
System.out.println(StringUtils.isAlpha(characterSequence));
System.out.println(StringUtils.isAlpha(null));
}
}

Explanation

  1. string = ""

Since the string is empty, the function returns false.

  1. string = "ñå"

Since the string contains Unicode characters, the function returns true.

  1. string = null

Since the string is null, the function returns false.

Output

The output of the code is as follows:


false
true
false

Free Resources