I have tried using string.split("\n\n") but it does not work. Anyone has any solution? thanks in advance
split() is documented with TextUtils. split(): String. split() returns [''] when the string to be split is empty.
To split a string on newlines, you can use the regular expression '\r?\ n|\r' which splits on all three '\r\n' , '\r' , and '\n' . A better solution is to use the linebreak matcher \R which matches with any Unicode linebreak sequence. You can also split a string on the system-dependent line separator string.
Answer: You just have to pass (“”) in the regEx section of the Java Split() method. This will split the entire String into individual characters.
split("-"); We can simply use a character/substring instead of an actual regular expression. Of course, there are certain special characters in regex which we need to keep in mind, and escape them in case we want their literal value. Once the string is split, the result is returned as an array of Strings.
First of all you should escape the \
with another \
like this:
string.split("\\n\\n");
Another way is using system default line separator:
string.split(System.getProperty("line.separator")+"{2}");
or you can try mix this:
string.split("(\\r\\n|"+System.getProperty("line.separator")+")+");
split
need RegExp, so you can try variants for your problem.
And don't forget that sometimes new line is not only \n
symbol, for Windows files it can be \r\n
char sequence.
You should escape the \
with another \
so try :-
string.split("\\n\\n");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With