java怎么把字符串中的数字串相加
2个回答
展开全部
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
boolean findUnm = false;
String s = "记得是2017.3.16那天,我借了100块,小明借了15.3块";
int len = s.length();
double sum = 0D;
for (int i = 0; i < len; i++) {
char c = s.charAt(i);
if (c == '.' || (c >= '0' && c <= '9')) {
sb.append(c);
findUnm = true;
} else if (findUnm) {
try {
sum += Double.parseDouble(sb.toString());
} catch (Exception e) {
}
sb = new StringBuffer();
findUnm = false;
}
}
System.out.println("一共借了" + sum + "块钱");
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询