方法一:
private static Double getMoney(String content){
Matcher matcher = null;
matcher = Pattern.compile("[0-9.]+").matcher(content.replaceAll(",",""));
while(matcher.find()){
return Double.parseDouble(matcher.group());
}
return null;
}
存在隐患:
public static void main(String[] args) {
Double money = getMoney(null);
System.out.println(money);
}
报错:
Exception in thread "main" java.lang.NullPointerException
at TestReplaceAll.getMoney(TestReplaceAll.java:23)
at TestReplaceAll.main(TestReplaceAll.java:15)
方法二:
private Double getMoney(String content) {
Matcher matcher = null;
if (org.apache.commons.lang3.StringUtils.isBlank(content)) {
return 0.0;
}
matcher = Pattern.compile("[0-9.]+").matcher(content.replaceAll(",", ""));
while (matcher.find()) {
return Double.parseDouble(matcher.group());
}
return 0.0;
}
执行同样的main方法,结果是 0.0