如何使用java查找字符串中三个独立数字的和?
我有以下字符串:如何使用java查找字符串中三个独立数字的和?,java,string,Java,String,我有以下字符串: String Str = "passed=20 failed=15 skipped=10"; 从中我需要找到 通过 失败 已跳过 使用正则表达式\\d+查找字符串中的所有数字,然后对它们进行分析和求和: String example = "passed=20 failed=15 skipped=10"; Pattern pattern = Pattern.compile("\\d+"); Matcher matcher = pattern.matcher(example);
String Str = "passed=20 failed=15 skipped=10";
从中我需要找到
通过
失败
已跳过
\\d+
查找字符串中的所有数字,然后对它们进行分析和求和:
String example = "passed=20 failed=15 skipped=10";
Pattern pattern = Pattern.compile("\\d+");
Matcher matcher = pattern.matcher(example);
int sum = 0;
while (matcher.find()) {
sum += Integer.parseInt(matcher.group(0));
}
System.out.println(sum);
1) 空间分割。2) 平分。3) 取第二次拆分的第二项。4) 将它们解析为一个数字。5) regexp上的sum.split要使用除数字以外的任何字符作为分隔符,请先将字符串转换为整数。
Pattern.compile(“\\s”).splitAsStream(input).map(kv->kv.split(“=”[1]).mapToInt(Integer::parseInt).sum()
。Regex适用于此