What is StringUtils.isNumeric in Java?

isNumeric() is a static method of the StringUtils that is used to check if the given string contains only Unicode digits. Unicode symbols other than Unicode digits are not allowed in the string.

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

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

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

  • The method does not allow for a leading sign, positive or negative. Hence, it returns false for inputs with a leading sign.

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 isNumeric(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. Otherwise, it returns false.

Code

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.isNumeric() for the string - '%s' is %s", s, StringUtils.isNumeric(s));
System.out.println();
s = "54 3 234 ";
System.out.printf("The output of StringUtils.isNumeric() for the string - '%s' is %s", s, StringUtils.isNumeric(s));
System.out.println();
s = "\u0967\u0968";
System.out.printf("The output of StringUtils.isNumeric() for the string - '%s' is %s", s, StringUtils.isNumeric(s));
System.out.println();
s = "ingf-2edf";
System.out.printf("The output of StringUtils.isNumeric() for the string - '%s' is %s", s, StringUtils.isNumeric(s));
System.out.println();
s = "-1334";
System.out.printf("The output of StringUtils.isNumeric() for the string - '%s' is %s", s, StringUtils.isNumeric(s));
System.out.println();
}
}

Output

The output of the code will be as follows:

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

Explanation

Example 1

  • string - '543234'

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

Example 2

  • string - '54 3 234'

The method returns false because the string contains space that is not allowed.

Example 3

  • string - '१२'

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

Example 4

  • string - 'ingf-2edf'

The method returns false because the string contains Unicode letters and other symbols besides Unicode digits.

Example 5

  • string - '-1334'

The method returns false because the string contains a leading sign.

Free Resources