方法:
public static boolean isNumber(String str){
String reg = "^[0-9]+(.[0-9]+)?$";
return str.matches(reg);
}
示例:
public static void main(String[] args) {
String str1 = "123.5646";
String str2 = "123213";
String str3 = "asdas";
String str4 = "asdas1234";
String reg = "^[0-9]+(.[0-9]+)?$";
boolean result1 = str1.matches(reg);
boolean result2 = str2.matches(reg);
boolean result3 = str3.matches(reg);
boolean result4 = str4.matches(reg);
System.out.println(result1);
System.out.println(result2);
System.out.println(result3);
System.out.println(result4);
}
结果:
true
true
false
false