Reputation: 1093
Why does the below code displays new line in console, but not in the browser/database? How can I resolve this?
StringBuilder s2 = new StringBuilder("John");
s2.append("Hey" + "\n")
Please assume that this code snippet is a part of the bigger code that is saving the conversation in a database.
Upvotes: 1
Views: 3577
Reputation: 914
It's because newline characters are normally interpreted by HTML as spaces. The usual way to have the text start on the next line in HTML is via a tag.
(If the text is enclosed in a element then newline characters are interpreted as new line instructions, but does other things which might be inconsistent with what you want the page to look like.)
Upvotes: 1
Reputation: 2727
You need to replace "\n"
with "<br/>"
before sending to browser.
Also, you can probably add css style="white-space: pre-line"
to the html component if you don't want the server side conversion. I haven't tested this solution myself.
Upvotes: 3