Tell me more ×  How can I replace all line breaks from a string
in Java in such a way that will work on Windows and Linux (ie no
OS specific problems of carriage return/line feed/new line
etc.)?  I've tried (note readFileAsString is a function that
reads a text file into a String):  String text =
readFileAsString("textfile.txt"); text.replace("\n", "");  but
this doesn't seem to work.  How can this be done?  share|improve
this question Do you want to eliminate all line breaks? Or you
want to uniformize them to a standard solution? –  helios Jan 29
'10 at 15:48 Oh, if you want to delete all linefeeds, remove all
\n AND all \r (because Windows linebreak is \r\n). –  helios Jan
29 '10 at 15:49 add comment  10 Answers  up vote 84 down vote
accepted  You need to set text to the results of text.replace():
String text = readFileAsString("textfile.txt"); text =
text.replace("\n", "").replace("\r", "");  This is necessary
because Strings are immutable -- calling replace doesn't change
the original String, it returns