Share this page 

Replace \r\n with the <br> tagTag(s): String/Number


This can be done easily a regular expression.

[JDK1.4]

import java.util.regex.Pattern;
import java.util.regex.Matcher;

...
// 4 different combinaisons
Pattern CRLF = Pattern.compile("(\r\n|\r|\n|\n\r)");
Matcher m = CRLF.matcher(myString);
 
if (m.find()) {
  newString = m.replaceAll("<br>");
}
or use the String.replaceAll(regex,replacement) method which is doing basically the same thing.
newString = myString.replaceAll("(\r\n|\r|\n|\n\r)", "<br>");