Best implementation for an isNumber(string) method

前端 未结 19 2562
感动是毒
感动是毒 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:34

    Here is our way of doing this:

    public boolean isNumeric(String string) throws IllegalArgumentException
    {
       boolean isnumeric = false;
    
       if (string != null && !string.equals(""))
       {
          isnumeric = true;
          char chars[] = string.toCharArray();
    
          for(int d = 0; d < chars.length; d++)
          {
             isnumeric &= Character.isDigit(chars[d]);
    
             if(!isnumeric)
             break;
          }
       }
       return isnumeric;
    }
    

提交回复
热议问题