How do I get a List<String>
from lines in String
? I want to handle CRLF
(\r\n
) and LF
(\n
) as EOLs. Empty lines, including those trailing need to be preserved, so that I can use String.join("\n", ...)
to get back the original String
(however, I don't mind if CRLF
s become LF
s).
This is what I've come up with:
String x = "\r\n\r\na\nb\r\nc\n\r\n\n";
List<String> lines = Arrays.asList(org.apache.commons.lang3.StringUtils.splitPreserveAllTokens(x.replace("\r", ""), "\n"));
I've seen various other questions but they don't seem to require the preserve empty lines part.