avaä¸å¤æå符串æ¯å¦ä¸ºæ°åçæ¹æ³:
1.ç¨JAVAèªå¸¦çå½æ°
public static boolean isNumeric(String str){
for (int i = 0; i < str.length(); i++){
System.out.println(str.charAt(i));
if (!Character.isDigit(str.charAt(i))){
return false;
}
}
return true;
}
2.ç¨æ£å表达å¼
é¦å
è¦import java.util.regex.Pattern å java.util.regex.Matcher
public boolean isNumeric(String str){
Pattern pattern = Pattern.compile("[0-9]*");
Matcher isNum = pattern.matcher(str);
if( !isNum.matches() ){
return false;
}
return true;
}
3.使ç¨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
ä¸é¢ä¸ç§æ¹å¼ä¸ï¼ç¬¬äºç§æ¹å¼æ¯è¾çµæ´»ã
第ä¸ãä¸ç§æ¹å¼åªè½æ ¡éªä¸å«è´å·â-âçæ°åï¼å³è¾å
¥ä¸ä¸ªè´æ°-199ï¼è¾åºç»æå°æ¯falseï¼
è第äºæ¹å¼åå¯ä»¥éè¿ä¿®æ¹æ£å表达å¼å®ç°æ ¡éªè´æ°ï¼å°æ£å表达å¼ä¿®æ¹ä¸ºâ^-?[0-9]+âå³å¯ï¼ä¿®æ¹ä¸ºâ-?[0-9]+.?[0-9]+âå³å¯å¹é
æææ°åã