Three methods to determine whether a string is a number in java

 The first way to use the functions that come with java

public static boolean isNumeric(String str){
          for(i<str.length();--i>=0;){
                if(!Character.isDigit(str.CharAt(i))){
                  return false;
                }
          }
          return true;
}

 The second uses regular expressions

    

public static boolean isNumeric(String str){
          Pattern pattern = Pattern.compile("[0-9]*");
          return pattern.matcher(str).matches();
}

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326946561&siteId=291194637