This is not a duplication of Java String split removed empty values, which deals with split()
method returning a new array. In this case I would like to avoid the array.
I solved this problem with a workaround, which I am posting below as a possible solution to my question.
My goal is to process all lines including empty strings such as the following example:
String input = "foo\nbar\n\n\nzul\n\n\n";
Pattern NEWLINE = Pattern.compile("\\R");
int [] count = {1};
NEWLINE
.splitAsStream(input)
.forEach(line -> System.out.println(count[0]++ + ": " + line));
which produces:
1: foo
2: baz
3:
4:
5: zul
Yet, it is missing:
6:
7:
How to include last empty lines?