I am actually working on a software that requires to read text files with some features that won't be explained here. While testing my code, I've found an anomaly which seems to come from the implementation of str.split("\r\n")
, where str
is a substring of the file's content.
When my substring ends with a succession of "\r\n"
(several line breaks), the method completely neglects this part. For example, if I work with the following string:
"\r\nLine 1\r\n\r\nLine 2\r\n\r\n"
, I would like to get the following array;
["", "Line 1", "", "Line 2", "", ""]
, but it returns:
["", "Line 1", "", "Line 2"]
The String.split()
Javadoc only notifies this without explaining:
... Trailing empty strings are therefore not included in the resulting array.
I cannot understand this asymmetry; why did they neglect empty string at the end, but not at the beginning?