java #code
方法1: Using Integer.parseInt or Double.parseDouble
public class NumberUtils {
public static boolean isNumeric(String str) {
if (str == null || str.isEmpty()) {
return false;
}
try {
Double.parseDouble(str);
return true;
} catch (NumberFormatException e) {
return false;
}
}
}
方法2: Using Regular Expressions
public class NumberUtils {
public static boolean isNumeric(String str) {
if (str == null || str.isEmpty()) {
return false;
}
return str.matches("-?\\d+(\\.\\d+)?");
}
}
方法3: Using Character Class
public class NumberUtils {
public static boolean isNumeric(String str) {
if (str == null || str.isEmpty()) {
return false;
}
for (char c : str.toCharArray()) {
if (!Character.isDigit(c)) {
return false;
}
}
return true;
}
}