java判断一个字符串是否是整数/正整数
public boolean isInteger(String str,boolean isPositive) {
Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
if (!isPositive) 不继续检查是否是正整数
return pattern.matcher(str).matches();
继续检查是否是正整数
if(pattern.matcher(str).matches()){
return Integer.parseInt(str) >0;
}
return pattern.matcher(str).matches();
}
两个参数。 str待检查字符串。isPositive为布尔值,由方法调用方决定,是否检查str为正整数。
该博客介绍了如何使用Java编写一个方法,根据`isPositive`参数来判断输入的字符串是否为整数或者正整数。通过正则表达式`^[-+]?[d]*$`匹配字符串,如果`isPositive`为真,则进一步检查字符串转换为整数后是否大于0。

2054

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



