StringUtils.isNumeric 大于等于0 的正整数为true 如果小数负数正整数都行, 就用 NumberUtils.isCreatable 都是org.apache.commons.lang3的
判断是否为大于等于0的数字
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
public static boolean isNumberContainsZero(String str) {
if (StringUtils.isBlank(str)) {
return false;
}
if (!NumberUtils.isCreatable(str)) {
return false;
}
Double doubleValue = Double.valueOf(str);
if (doubleValue >= 0) {
return true;
}
return false;
}
判断是否为正整数
import org.apache.commons.lang3.StringUtils;
import java.util.regex.Pattern;
public class StringUtil {
public static boolean isPositiveInteger(String str) {
if (StringUtils.isBlank(str)) {
return false;
}
Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
if(pattern.matcher(str).matches()){
return Integer.parseInt(str) >0;
}
return pattern.matcher(str).matches();
}
}