isAlphanumericSpace()
is a static method of the StringUtils
class that is used to check if a given string contains only Unicode letters, digits, or spaces (" "
).
The method returns false
if the string contains Unicode symbols except for Unicode letters, digits, or spaces (" "
).
The method returns false
if the input string is null
.
The method returns false
if the input string is empty.
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;
public static boolean isAlphanumericSpace(final CharSequence cs)
final CharSequence cs
: the character sequence/string to be checked.This method returns true
if the string is not null and contains only Unicode digits, letters, or spaces (" "
). Otherwise, it returns false
.
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.isAlphanumericSpace() for the string - '%s' is %s", s, StringUtils.isAlphanumericSpace(s));System.out.println();s = "";System.out.printf("The output of StringUtils.isAlphanumericSpace() for the string - '%s' is %s", s, StringUtils.isAlphanumericSpace(s));System.out.println();s = null;System.out.printf("The output of StringUtils.isAlphanumericSpace() for the string - '%s' is %s", s, StringUtils.isAlphanumericSpace(s));System.out.println();s = " ";System.out.printf("The output of StringUtils.isAlphanumericSpace() for the string - '%s' is %s", s, StringUtils.isAlphanumericSpace(s));System.out.println();s = "hello-educative";System.out.printf("The output of StringUtils.isAlphanumericSpace() for the string - '%s' is %s", s, StringUtils.isAlphanumericSpace(s));System.out.println();}}
The output of StringUtils.isAlphanumericSpace() for the string - '543234 asfg' is true
The output of StringUtils.isAlphanumericSpace() for the string - '' is false
The output of StringUtils.isAlphanumericSpace() for the string - 'null' is false
The output of StringUtils.isAlphanumericSpace() for the string - ' ' is true
The output of StringUtils.isAlphanumericSpace() for the string - 'hello-educative' is false
string - "543234 asfg"
The method returns true
, as the string contains only Unicode digits, letters, and a space.
string - ""
The method returns false
, as the string is empty.
string - null
The method returns false
, as the string points to null
reference.
string - " "
The method returns true
, as the string contains only spaces that are allowed.
string - "hello-educative"
The method returns false
, as the string contains other Unicode symbols like -
.