search

Home  >  Q&A  >  body text

java - Long.valueOf(workOrder.getCreator())类型验证

天蓬老师天蓬老师2781 days ago923

reply all(1)I'll reply

  • 黄舟

    黄舟2017-04-18 09:23:37

    public static boolean isNumeric(String str) {
            if (null == str) {
                return false;
            }
    
            int length = str.length();
    
            for (int i = 0; i < length; i++) {
                if (!Character.isDigit(str.charAt(i))) {
                    return false;
                }
            }
    
            return true;
        }
    

    public boolean isNumeric(String str){ 
       Pattern pattern = Pattern.compile("[0-9]*"); 
       Matcher isNum = pattern.matcher(str);
       if( !isNum.matches() ){
           return false; 
       } 
       return true; 
    }
    

    使用org.apache.commons.lang
    org.apache.commons.lang.StringUtils;
    boolean isNunicodeDigits=StringUtils.isNumeric("aaa123456789");
    http://jakarta.apache.org/commons/lang/api-release/index.html下面的解释:
    isNumeric
    public static boolean isNumeric(String str)Checks if the String contains only unicode digits. A decimal point is not a unicode digit and returns false.
    null will return false. An empty String ("") will return true.
     StringUtils.isNumeric(null)   = false
     StringUtils.isNumeric("")     = true
     StringUtils.isNumeric("  ")   = false
     StringUtils.isNumeric("123")  = true
     StringUtils.isNumeric("12 3") = false
     StringUtils.isNumeric("ab2c") = false
     StringUtils.isNumeric("12-3") = false
     StringUtils.isNumeric("12.3") = false
     
    Parameters:
    str - the String to check, may be null 
    Returns:
    true if only contains digits, and is non-null
    

    Among the above three methods, the second method is more flexible.

    The first and third methods can only verify numbers without the negative sign "-", that is, if you enter a negative number -199, the output result will be false;

    The second method can be used to verify negative numbers by modifying the regular expression. Just change the regular expression to "^-?[0-9]+" and change it to "-?[0-9]+.? [0-9]+" will match all numbers.

    reply
    0
  • Cancelreply