我正在使用以下代码计算Java中的空行,但此代码返回的空行数量大于此数量。Java:计算文本文件/字符串中的空行
int countEmptyLines(String s) {
int result=0;
Pattern regex = Pattern.compile("(?m)^\\s*$");
Matcher testMatcher = regex.matcher(s);
while (testMatcher.find())
{
result++;
}
return result;}
我在做什么错或有没有更好的方法来做到这一点?