判断字符串是否为数字
@Test
public void test2(){
String str1 = "-1";
String str2 = "0";
String str3= "1";
String str4= "春";
String str5= "1.1";
String str6= "--";
String str7= ".9";
String str8= " ";
String str9= "0.";
String str10= ".";
System.out.println("str1 = " + NumberUtil.isNumber(str1));
System.out.println("str2 = " + NumberUtil.isNumber(str2));
System.out.println("str3 = " + NumberUtil.isNumber(str3));
System.out.println("str4 = " + NumberUtil.isNumber(str4));
System.out.println("str5 = " + NumberUtil.isNumber(str5));
System.out.println("str6 = " + NumberUtil.isNumber(str6));
System.out.println("str7 = " + NumberUtil.isNumber(str7));
System.out.println("str8 = " + NumberUtil.isNumber(str8));
System.out.println("str9 = " + NumberUtil.isNumber(str9));
System.out.println("str10 = " + NumberUtil.isNumber(str10));
}
结果
str1 = true
str2 = true
str3 = true
str4 = false
str5 = true
str6 = false
str7 = true
str8 = false
str9 = true
str10 = false
依赖
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.15</version>
</dependency>