为什么我不能在数千之前摆脱空白?
我已经编写了这样的方法来检查字符串是否可以解析为双精度:
编辑:好的,我已经更新了方法,因为每个人都写了相同的答案 - 这是不寻常的情况
public static boolean isNumber(String test) {
// remove whitespaces
System.out.print("Test is - " + test);
test = test.replaceAll("\\s", "");
// test = test.replaceAll("[ \\t]", "");
// test = test.replaceAll("\\s+", "");
// test = test.replaceAll(" ", "");
System.out.print(" - now test is - " + test);
// match pattern - numbers with decimal delimiter as dot or comma
String decimalPattern = "([0-9]*)(\\.|\\,)([0-9]*)";
boolean match = Pattern.matches(decimalPattern, test);
System.out.println(" - Is this number? ===> " + match);
return match;
}
现在我要疯了。这是我的方法的一些输出:
[stdout] Test is - aasdfg - now test is - aasdfg - Is this number? ===> false
[stdout] Test is - aa sd fg - now test is - aasdfg - Is this number? ===> false
[stdout] Test is - 123.50 - now test is - 123.50 - Is this number? ===> true
[stdout] Test is - 123,50 - now test is - 123,50 - Is this number? ===> true
[stdout] Test is - 1 123.50 - now test is - 1 123.50 - Is this number? ===> false
输出的最后一行是奇怪的!
建议 - 测试值来自HSSFCell#getStringCellValue()
- 也许这是问题。没有评论的String#replaceAll
作品。