user9058537
user9058537

Reputation:

How can I make my \n a new Line in HTML

Basically I have a string with \n and I want to display on a editable textbox in html. My problem is that in HTML i get everything in a new line and it's a big mess.

How can I replace the \n with <br> or something like that to make new lines in html??

JAVA GitLab.getProjectFile() Return this string (it's not this but it's the same format):

blablabla\nblablabla\nbla bla bla\n

In HTML I'm calling like this:

<div style="margin-top:20px;">
   <%
        String data = GitLab.getProjectFile();
    %>
    <%=data%>
</div>

Upvotes: 2

Views: 2745

Answers (2)

Jules Dupont
Jules Dupont

Reputation: 7577

You can also solve this problem without having to replace any newlines by using CSS. Just add white-space: pre-line; to the style attribute as follows:

<div style="margin-top:20px; white-space: pre-line;">
    <%
        String data = GitLab.getProjectFile();
    %>
    <%=data%>
</div>

One of the advantages of this approach is that you won't have to strip <br/> tags from the text you retrieve in the textbox.

There are other CSS whitespace options if pre-line isn't quite what you're looking for. You can see a description of them here.

Upvotes: 0

Mureinik
Mureinik

Reputation: 311468

You could just call replace:

String data = GitLab.getProjectFile().replace("\n", "<br/>");

Upvotes: 2

Related Questions