Java (android): How to split a string every two line breaks?

I tried using string.split ("\ n \ n") but it doesn't work. Anyone have a solution? thank you in advance

+3


source to share


3 answers


First of all, you must exit \

with another \

like this:

string.split("\\n\\n");

      

Another way is to use the default line separator:

string.split(System.getProperty("line.separator")+"{2}");

      



or you can try mixing this:

string.split("(\\r\\n|"+System.getProperty("line.separator")+")+");

      

split

needs RegExp, so you can try options for your problem.

And don't forget that sometimes a newline is not just a character \n

, for Windows files it can be a \r\n

char sequence .

+2


source


You should avoid \

with others \

, so try: -



string.split("\\n\\n");

      

+2


source


Try using the escape sequence \

:

string.split("\\n\\n");

      

+1


source







All Articles