/**
* 判断字符串是否为非负整数
* @param str
* @return
*/
public boolean isNonnegativeInteger(String str){
return str == null ? false : Pattern.compile("^\\d+$").matcher(str).matches();
}
/**
* 判断字符串是否为数字
* @param str
* @return
*/
public boolean isNumeric(String str){
return str == null ? false : Pattern.compile("-?[0-9]+(.[0-9]+)?").matcher(str).matches();
}
/**
* 判断多字符串是否为正整数
* @param str
* @return
*/
public static boolean isPositiveInteger(String... strs) {
if (strs.length > 0) {
for (String str : strs) {
if (str == null || !Pattern.compile("^[0-9]*[1-9][0-9]*$").matcher(str).matches()) return false;
}
return true;
}
return false;
}
本文介绍如何使用Java编程语言判断一个字符串是否能转换为非负整数。内容涉及正则表达式、Integer.parseInt()和NumberFormatException的使用,以及相关错误处理策略。
313

被折叠的 条评论
为什么被折叠?



