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 <br> tag.
(If the text is enclosed in a <pre> element then newline characters are interpreted as new line instructions, but <pre> does other things which might be inconsistent with what you want the page to look like.)
Daniel Demesmaecker wrote:If I remember correctly, you can't send html tags in a stringbuilder, they're not being rendered eather, if you use , that's exactly how it will show up, as a string, not as an htmltag
It's immaterial how the HTML that gets sent to the browser is created. If HTML tags are being sent, they will be interpreted. If they show up as string then something else is at work that prevents them from being interpreted.
I looked for it, but coudn't find the post anymore, but I seem to remeber I posted the same question once, I also was using a stringbuilder to build a message and at the end return it to the model.
Neather \n, \r, \n\r nor br worked
It's good to be able to use someting, it's better to understand how it works.
HTML only uses whitespace as a token delimiter. That means that any whitespace (besides spaces between words) that you want to have in the web page must be in the form of HTML tags. As has been said, line breaks are inserted into a text span with the <br /> tag. If this shows up in the web page as the literal text " <br />", then it's likely because you're trying to inject it into a template that is being processed by a template engine, which will escape anything that looks like HTML. Depending on the template language, there are different ways of injecting literal HTML.
As previously stated, newlines are just another whitespace character which HTML treats as a delimiter and will not render as anything in the output. Use HTML elements and CSS to control layout.
HTML can be built up in strings and still be interpreted as HTML.
There's lots of context missing from the original question. Why are you building HTML in a string in the first place? Is this a Java web application? If so, you should likely be building the view in a JSP. If not, how is the HTML string getting to the browser?
As a general rule, you should not attempt to create HTML in server-side logic. It's far better to use some sort of View Template (for example, a JSP or a JSF xhtml) that contains the HTML and insert the variable data into that template for display.
As everyone else has said, "space" characters such as space, tab, newline and the like are treated specially in HTML. First, because all of them render simply as a blank horizontal space - and that includes newline. Secondly, because any sequence of "space" characters renders simply as a single horizontal space, whether there's 1 or 101. To actually render multiple horizontal space characters, there's something called the non-break space entity: , but actually that's not a very good thing to use. Spacing control is better done with layout tags such as <div> and with CSS.
OK, so much for Yet Another HTML lesson. If you're actually trying to, say, capture and display a chat conversation verbatim, there's a way to honor how text was originally spaced and lined. Use the <pre> tag to wrap it.
When it comes to destroying a civilization, gas chambers cannot hold a candle to echo chambers.