I've got a regex "[\r\n\f]+" to find the number of lines contained in a String. My code is like this:
pattern = Pattern.compile("[\\r\\n\\f]+")
String[] lines = pattern.split(texts);
In my unit test I've got sample strings like these:
"\t\t\t \r\n \n"
"\r\n"
The result of parsing the first string is 2, however it becomes 0 when it's parsing the second string.
I thought the second string includes 1 line although the line is "blank" (suppose I'm editing a file which begins with "\r\n" in a text editor, should the caret be placed at the second line?). Is my regex incorrect for parsing lines? or am I missing something here?
Edit:
I think I'll make the question more obvious:
Why
// notice the trailing space in the string
"\r\n ".split("\r\n").length == 2 // results in 2 strings {"", " "}. So this block of text has two lines.
but
// notice there's no trailing space in the string
"\r\n".split("\r\n").length == 0 // results in an empty array. Why "" (empty string) is not in the result and this block of text contains 0 lines?