我将避免重新发明此方法,而应使用Apache Commons。如果您使用Spring,Struts或许多其他常用的Java库,则它们通常包含Apache Commons。您将需要commons- lang.jar文件。这是您想要的NumberUtils中的方法:
isNumber[1]
public static boolean isNumber(java.lang.String str)
Checks whether the String a valid Java number.
Valid numbers include hexadecimal marked with the 0x qualifier, scientific notation and numbers marked with a type qualifier (e.g. 123L).
Null and empty String will return false.
Parameters:
str - the String to check
Returns:
true if the string is a correctly formatted number