Best implementation for an isNumber(string) method

前端 未结 19 2555
感动是毒
感动是毒 2020-12-15 20:04

In my limited experience, I\'ve been on several projects that have had some sort of string utility class with methods to determine if a given string is a number. The idea h

19条回答
  •  隐瞒了意图╮
    2020-12-15 20:31

    public static boolean CheckString(String myString) {
    
    char[] digits;
    
        digits = myString.toCharArray();
        for (char div : digits) {// for each element div of type char in the digits collection (digits is a collection containing div elements).
            try {
                Double.parseDouble(myString);
                System.out.println("All are numbers");
                return true;
            } catch (NumberFormatException e) {
    
                if (Character.isDigit(div)) {
                    System.out.println("Not all are chars");
    
                    return false;
                }
            }
        }
    
        System.out.println("All are chars");
        return true;
    }
    

提交回复
热议问题