StringUtils isNumeric() Java

StringUtils isNumeric() Java | The isNumeric() method is the static method in the StringUtils method that checks whether the string contains Unicode digits or not. This method returns false if the string has decimal values as decimal values are not considered Unicode digits and also it returns false if it contains null or the string is empty. Also see:- Unicode character set in Java

Java StringUtils isNumeric() Method

The syntax for the isNumeric method is as follows:- public static boolean isNumeric(final CharSequence cs)
Parameters: cs – the string which needed to be checked whether it contains Unicode digits or not.
Return type: boolean
Returns: true if the string contains Unicode or else returns false.

StringUtils isNumeric() method checks if the CharSequence contains only Unicode digits. A decimal point is not a Unicode digit and returns false. The null will return false. An empty CharSequence (length()=0) will return false.

Note that the method does not allow for a leading sign, either positive or negative. Also, if a String passes the numeric test, it may still generate a NumberFormatException when parsed by Integer.parseInt or Long.parseLong, e.g. if the value is outside the range for int or long respectively.

Java isNumeric() StringUtils Example

Let us see some examples of StringUtils.isNumeric() method.

import org.apache.commons.lang3.StringUtils;

public class Main {

    public static void main(String[] args) {
        String string = "4789652";
        System.out.println(StringUtils.isNumeric(string));
    }
}

Output:-

true

import org.apache.commons.lang3.StringUtils;

public class Main {

    public static void main(String[] args) {
        String string = "Hi ";
        System.out.println(StringUtils.isNumeric(string));
    }
}

Output:-

false

import org.apache.commons.lang3.StringUtils;

public class Main {

    public static void main(String[] args) {
        String string = "\u0967\u0968";
        System.out.println(StringUtils.isNumeric(string));
        System.out.println("String: " + string);
    }
}

Output:-

true
String: १२

import org.apache.commons.lang3.StringUtils;

public class Main {

    public static void main(String[] args) {
        String string = "   ";
        System.out.println(StringUtils.isNumeric(string));
    }
}

Output:-

false

import org.apache.commons.lang3.StringUtils;

public class Main {

    public static void main(String[] args) {
        String string = "-1334";
        System.out.println(StringUtils.isNumeric(string));
    }
}

Output:-

false

Since StringUtils.isNumeric() method doesn’t allow a leading sign therefore StringUtils.isNumeric() method return false for the number = -1334.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *