a shot of dev knowledge

RELATED TAGS

What is StringUtils.isAlphaSpace in Java?

isAlphaSpace() is a static method of the StringUtils class that is used to check if the given string only contains Unicode letters or space (" "). The method returns false if the string contains Unicode symbols other than Unicode letters or space (" ").

  • The method returns false if the input string is null.
  • The method returns true if the input string is empty.

How to import StringUtils

StringUtils 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.

You can import the StringUtils class as follows.

import org.apache.commons.lang3.StringUtils;

Syntax

public static boolean isAlphaSpace(final CharSequence cs)

Parameters

  • final CharSequence cs: the character sequence/string to check.

Return value

This method returns true if the string is not null and only contains Unicode letters or space (" "); otherwise, it returns false.

Code


import org.apache.commons.lang3.StringUtils;

public class Main {

    public static void main(String[] args) {
        String s = "543234 asfg";
        System.out.printf("The output of StringUtils.isAlphaSpace() for the string - '%s' is %s", s, StringUtils.isAlphaSpace(s));
        System.out.println();

        s = "";
        System.out.printf("The output of StringUtils.isAlphaSpace() for the string - '%s' is %s", s, StringUtils.isAlphaSpace(s));
        System.out.println();

        s = null;
        System.out.printf("The output of StringUtils.isAlphaSpace() for the string - '%s' is %s", s, StringUtils.isAlphaSpace(s));
        System.out.println();

        s = "   ";
        System.out.printf("The output of StringUtils.isAlphaSpace() for the string - '%s' is %s", s, StringUtils.isAlphaSpace(s));
        System.out.println();

        s = "hello-educative";
        System.out.printf("The output of StringUtils.isAlphaSpace() for the string - '%s' is %s", s, StringUtils.isAlphaSpace(s));
        System.out.println();
    }
}

Output

The output of the code will be as follows.

The output of StringUtils.isAlphaSpace() for the string - '543234 asfg' is false
The output of StringUtils.isAlphaSpace() for the string - '' is true
The output of StringUtils.isAlphaSpace() for the string - 'null' is false
The output of StringUtils.isAlphaSpace() for the string - '   ' is true
The output of StringUtils.isAlphaSpace() for the string - 'hello-educative' is false

Explanation

Example 1

  • string - "543234 asfg"

The method returns false, as the string contains Unicode digits that are not allowed in this method.

Example 2

  • string - ""

The method returns true, as the string is empty.

Example 3

  • string - null

The method returns false, as the string points to null reference.

Example 4

  • string - " "

The method returns true, as the string only contains spaces, which are allowed.

Example 5

  • string - "hello-educative"

The method returns false, as the string contains Unicode letters as well as other Unicode symbols, such as -.

RELATED TAGS

RELATED COURSES

View all Courses

Keep Exploring