[c#] Display label text with line breaks in c#

Is it possible to display the label text with line breaks exactly as per the image

enter image description here

I need the text on the label to be printed exactly can some one help me, my desired text that has to be shown on the label will be stored in a string-builder

This question is related to c# asp.net

The answer is


Also you can use the following

@"Italian naval...<br><br>"+

Above code you can achieve double space. If you want single one means you simply use
.


I know this thread is old, but...

If you're using html encoding (like AntiXSS), the previous answers will not work. The break tags will be rendered as text, rather than applying a carriage return. You can wrap your asp label in a pre tag, and it will display with whatever line breaks are set from the code behind.

Example:

<pre style="width:600px;white-space:pre-wrap;"><asp:Label ID="lblMessage" Runat="server" visible ="true"/></pre>

I had to replace new lines with br

string newString = oldString.Replace("\n", "<br />");

or if you use xml

<asp:Label ID="Label1" runat="server" Text='<%# ShowLineBreaks(Eval("Comments")) %>'></asp:Label>

Then in code behind

public string ShowLineBreaks(object text)
{
    return (text.ToString().Replace("\n", "<br/>"));
}

Or simply add one line of:

Text='<%# Eval("Comments").ToString().Replace("\n","<br />") %>'

Following line worked for me:

lbTabRes.Text += num + " x " + i + " = " + (num * i).ToString() + "<br/> \n";

You can use <br /> for Line Breaks, and &nbsp; for white space.

string s = "First line <br /> Second line";

Output:

First line
Second line

For more info refer to this: Line break in Label


You can also use <br/> where you want to break the text.