/**
* 判断字符串是否为正整数或者浮点数
* 1.11 -> true
* 1 -> true
* -1 -> false
* 1a -> false
* @param str
* @return
*/
public static boolean isNumeric(String str){
// 判断正整数正则
Pattern patternInteger = Pattern.compile("^\\d+(\\.\\d+)?$");
// 判断浮点数正则
Pattern patternFloat = Pattern.compile("^(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))$");
return patternFloat.matcher(str).matches() || patternInteger.matcher(str).matches();
}
Java 判断字符串是否为正整数或者浮点数
最新推荐文章于 2024-08-12 03:30:17 发布