How to check if a String is numeric in Java

前端 未结 30 2573
盖世英雄少女心
盖世英雄少女心 2020-11-21 05:26

How would you check if a String was a number before parsing it?

相关标签:
30条回答
  • 2020-11-21 05:43

    As @CraigTP had mentioned in his excellent answer, I also have similar performance concerns on using Exceptions to test whether the string is numerical or not. So I end up splitting the string and use java.lang.Character.isDigit().

    public static boolean isNumeric(String str)
    {
        for (char c : str.toCharArray())
        {
            if (!Character.isDigit(c)) return false;
        }
        return true;
    }
    

    According to the Javadoc, Character.isDigit(char) will correctly recognizes non-Latin digits. Performance-wise, I think a simple N number of comparisons where N is the number of characters in the string would be more computationally efficient than doing a regex matching.

    UPDATE: As pointed by Jean-François Corbett in the comment, the above code would only validate positive integers, which covers the majority of my use case. Below is the updated code that correctly validates decimal numbers according to the default locale used in your system, with the assumption that decimal separator only occur once in the string.

    public static boolean isStringNumeric( String str )
    {
        DecimalFormatSymbols currentLocaleSymbols = DecimalFormatSymbols.getInstance();
        char localeMinusSign = currentLocaleSymbols.getMinusSign();
    
        if ( !Character.isDigit( str.charAt( 0 ) ) && str.charAt( 0 ) != localeMinusSign ) return false;
    
        boolean isDecimalSeparatorFound = false;
        char localeDecimalSeparator = currentLocaleSymbols.getDecimalSeparator();
    
        for ( char c : str.substring( 1 ).toCharArray() )
        {
            if ( !Character.isDigit( c ) )
            {
                if ( c == localeDecimalSeparator && !isDecimalSeparatorFound )
                {
                    isDecimalSeparatorFound = true;
                    continue;
                }
                return false;
            }
        }
        return true;
    }
    
    0 讨论(0)
  • 2020-11-21 05:43

    This a simple example for this check:

    public static boolean isNumericString(String input) {
        boolean result = false;
    
        if(input != null && input.length() > 0) {
            char[] charArray = input.toCharArray();
    
            for(char c : charArray) {
                if(c >= '0' && c <= '9') {
                    // it is a digit
                    result = true;
                } else {
                    result = false;
                    break;
                }
            }
        }
    
        return result;
    }
    
    0 讨论(0)
  • 2020-11-21 05:44

    Java 8 lambda expressions.

    String someString = "123123";
    boolean isNumeric = someString.chars().allMatch( Character::isDigit );
    
    0 讨论(0)
  • 2020-11-21 05:45

    If you using java to develop Android app, you could using TextUtils.isDigitsOnly function.

    0 讨论(0)
  • 2020-11-21 05:45

    Java 8 Stream, lambda expression, functional interface

    All cases handled (string null, string empty etc)

    String someString = null; // something="", something="123abc", something="123123"
    
    boolean isNumeric = Stream.of(someString)
                .filter(s -> s != null && !s.isEmpty())
                .filter(Pattern.compile("\\D").asPredicate().negate())
                .mapToLong(Long::valueOf)
                .boxed()
                .findAny()
                .isPresent();
    
    0 讨论(0)
  • 2020-11-21 05:46

    I modified CraigTP's solution to accept scientific notation and both dot and comma as decimal separators as well

    ^-?\d+([,\.]\d+)?([eE]-?\d+)?$
    

    example

    var re = new RegExp("^-?\d+([,\.]\d+)?([eE]-?\d+)?$");
    re.test("-6546"); // true
    re.test("-6546355e-4456"); // true
    re.test("-6546.355e-4456"); // true, though debatable
    re.test("-6546.35.5e-4456"); // false
    re.test("-6546.35.5e-4456.6"); // false
    
    0 讨论(0)
提交回复
热议问题