Java String.replaceAllactually accepts a regular expression. You can delete all lines of a new line with:
s = s.replaceAll("\\n", "");
s = s.replaceAll("\\r", "");
But this will delete all lines of the newline.
Pay attention to double \: so that the string passed to the regular expression parser is \n.
You can also do it that is smarter:
s = s.replaceAll("\\s{2,}", " ");
This will remove all sequences from 2 or more spaces, replacing them with a single space. Since newlines are also spaces, this should do the trick for you.
source
share