Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用java查找字符串中三个独立数字的和?_Java_String - Fatal编程技术网

如何使用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适用于此