What is StringUtils.isNumericSpace in Java?

isNumericSpace() is a staticthe methods in Java that can be called without creating an object of a class. method of the StringUtils class that checks if the given string contains only Unicode digits or space.

  • If the given string is a decimal point, then the method returns false as the decimal point is not considered to be a Unicode digit.

  • 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. To add the Apache Commons Lang package to the Maven project, add 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 isNumericSpace(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 contains only Unicode digits or space. Otherwise, it returns false.

Code

Example 1

  • string - "543234"

The method returns true as the string contains only Unicode digits.

Example 2

  • string - "54 3 234 "

The method returns true as the string contains only Unicode digits and space.

Example 3

  • string - "१ २"

The method returns true as the string contains only Unicode digits and space.

Example 4

  • string - "ingf-2edf"

The method returns false as the string contains Unicode letters.

import org.apache.commons.lang3.StringUtils;
public class Main {
public static void main(String[] args) {
String s = "543234";
System.out.printf("The output of StringUtils.isNumericSpace() for the string - '%s' is %s",
s, StringUtils.isNumericSpace(s));
System.out.println();
s = "54 3 234 ";
System.out.printf("The output of StringUtils.isNumericSpace() for the string - '%s' is %s",
s, StringUtils.isNumericSpace(s));
System.out.println();
s = "\u0967 \u0968";
System.out.printf("The output of StringUtils.isNumericSpace() for the string - '%s' is %s",
s, StringUtils.isNumericSpace(s));
System.out.println();
s = "ingf-2edf";
System.out.printf("The output of StringUtils.isNumericSpace() for the string - '%s' is %s",
s, StringUtils.isNumericSpace(s));
System.out.println();
}
}

Output

The output of the code will be as follows.


The output of StringUtils.isNumericSpace() for the string - '543234' is true
The output of StringUtils.isNumericSpace() for the string - '54 3 234 ' is true
The output of StringUtils.isNumericSpace() for the string - '१ २' is true
The output of StringUtils.isNumericSpace() for the string - 'ingf-2edf' is false

Free Resources