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

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -