What is StringUtils.isNumericSpace in Java?
isNumericSpace() is a StringUtils class that checks if the given string contains only Unicode digits or space.
-
If the given string is a
decimalpoint, then the method returnsfalseas the decimal point is not considered to be aUnicodedigit. -
The method returns
falseif the input string isnull. -
The method returns
trueif 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