StringUtils isAlpha() example in Java

In this post, we will see org.apache.commons.lang3.StringUtils isAlpha() example in Java. The isAlpha() method is used to check given character is an alphabet or not. It returns true if the given character is alphabet else returns false.

We need to add the below dependency in maven to use org.apache.commons.lang3.StringUtils isEmpty() method. We can download apache-commons maven dependency from here.

pom.xml changes

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

StringUtils isAlpha() Example in Java

import org.apache.commons.lang3.StringUtils;

public class SpringMain {
    public static void main(String[] args) {
        String s1 = "this";

        String s2 = "2121";
        System.out.println(StringUtils.isAlpha(s1));//true
        System.out.println(StringUtils.isAlpha(s2));//false
    }
}

Output –

true
false

Syntax of isAlpha() – public static boolean isAlpha(final CharSequence cs)

Note – Internally isAlpha() uses the Character.isLetter() method. The isAlpha() method checks for empty also. If we provide an empty string or a character it will return false. It accepts CharSequence as argument.

import org.apache.commons.lang3.StringUtils;
public class SpringMain {
    public static void main(String[] args) {
        System.out.println(StringUtils.isAlpha(null));//false
        System.out.println(StringUtils.isAlpha("  "));//false
        System.out.println(StringUtils.isAlpha("ab2c"));//false
        System.out.println(StringUtils.isAlpha("ab-c"));//false
    }
}

Output –

false
false
false
false

Let’s see a few questions related to StringUtils isAplha() method.

How do I make sure a string only has letters?

We can use StringUtils isAlpha() method. if it returns true then the given string has only letters.

Note – We can also use Character.isLetter() to check given String is alpha or not.

Is StringUtils isAlpha() method null safe?

Yes. It will return false for null or empty. See above code snippet.

What is the difference between StringUtils isAlpha() and Character isLetter()?

Both are used to check character is alphabet or not. But isLetter() accepts single char whereas using isAlpha() we can pass String. It is easier to use isAlpha() method than isLetter().

See

That’s all about StringUtils isAlpha() example in Java.

See docs.

Related post.