java - Easy way of getting List<String> from String by splitting on EOL? -


how list<string> lines in string? want handle crlf (\r\n) , lf (\n) eols. empty lines, including trailing need preserved, can use string.join("\n", ...) original string (however, don't mind if crlfs become lfs).

this i've come 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 don't seem require preserve empty lines part.

you can try

string[] result = yourstring.split("\r?\n", -1); 

-1 parameter limit , negative value means trailing empty strings should not removed resulting string[] array (i assuming converting array list<string> not big problem you).


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -