Java Regex Number Validate isNumeric(String s)

Here you can find the source of isNumeric(String s)

Description

Returns true if String s is numeric (i.e., the Digit POSIX character class) characters and nothing else.

License

Apache License

Parameter

Parameter Description
s String

Return

boolean

Declaration

public static boolean isNumeric(String s) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.util.regex.Pattern.compile;

public class Main {
    private final static Pattern DIGIT_REGEX = compile("\\p{Digit}+");

    /**/*from  w  w  w. j ava2  s .  c  om*/
     * Returns {@code true} if {@link String s} is numeric (i.e., the
     * {@code Digit} POSIX character class) characters and nothing else.
     *
     * @param s {@link String}
     * @return boolean
     */
    public static boolean isNumeric(String s) {
        if (!hasLength(s)) {
            return false;
        }
        Matcher m = DIGIT_REGEX.matcher(s);
        return m.matches();
    }

    /**
     * Returns {@code true} if the string is non-null and non-empty,
     * {@code false} otherwise.
     *
     * @param s String, may be null
     * @return boolean
     */
    public static boolean hasLength(final String s) {
        return s != null && !s.isEmpty();
    }
}

Related

  1. isNumberLiteral(String s)
  2. isNumberOfShownValue(String inputString)
  3. isNumeric(String as_argument)
  4. isNumeric(String input)
  5. isNumeric(String number)
  6. isNumeric(String str)
  7. isNumeric(String str)
  8. isNumeric(String str)
  9. isNumeric(String str)