我做了一个可以检测字符串中数字的方法。但是,它不接受带有ØØÅ字母的字符串。有什么建议可以使其接受并轻松集成到我的方法中吗?
public static boolean checkNumbers(String testString) {
String numOnly = testString.replaceAll("\\p{Alpha}", "");
try {
double numVal = Double.valueOf(numOnly);
System.out.println(testString + " contains numbers");
return true;
} catch (NumberFormatException e) {
System.out.println(testString + " Contains no numbers");
return false;
}
}
如评论中所述:
您将需要更改您的.replaceAll("\\p{Alpha}", "");
至.replaceAll("[^\\d.]", "");