「Java入門」正規表現で指定された文字列が整数かどうかを判定する

方法1
public static boolean isInteger(String str) {
Pattern pattern = Pattern.compile(“^[-\\+]?[\\d]*$");
return pattern.matcher(str).matches();
}

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

方法3
public final static boolean isNumeric(String s) {
if (s != null && !"".equals(s.trim()))
return s.matches(“^[0-9]*$");
else
return false;
}

方法4
public static boolean isNumeric(String str){
for(int i=str.length();–i>=0;){
int chr=str.charAt(i);
if(chr<48 || chr>57)
return false;
}
return true;
}

Java

Posted by arkgame