Search code examples
javahtmlstringline-breaks

How can I add a newline character to a String in Java?


In a Java application, I am creating a String like below (by concatenation):

String notaCorrente = dataOdierna + " - " + testoNotaCorrente;

My problem is that I want to add also something like an HTML newline character at the end of this String (that will be shown into an HTML page).

How can I implement it?


Solution

  • The newline character in Java is "\n" which will look like this:

    String notaCorrente = dataOdierna + " - " + testoNotaCorrente + "\n";
    

    However, this will not display as you expect on your HTML page. You can try adding an html break tag, or add the 
 (Line Feed) and 
 (Carriage Return) HTML entities:

    String notaCorrente = dataOdierna + " - " + testoNotaCorrente + "<br>";
    

    or

    String notaCorrente = dataOdierna + " - " + testoNotaCorrente + "&#13;&#10";