Click here to Skip to main content
15,034,432 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a text field, lets assume user writes a string:
Example text.

It's saved to string as:
Example text. \r\n\r\nEtc.

When you try to display it, it will look like this:
Example text.  Etc.

Simply it ignores all the details that ensure the string is displayed properly. How to display it the way it was in input? I want to display it inside

What I have tried:

Tried to replace \n and \r with
it doesn't work. I'm out of ideas. Looks like ASP.NET does not support it but that seems unlikely.
Updated 29-Mar-21 3:21am
Richard MacCutchan 28-Mar-21 5:24am
Where is the code that does the replacement?
Member 15047625 28-Mar-21 5:33am
.Replace() inside controller and view, both versions change nothing.
Richard MacCutchan 28-Mar-21 5:39am
Well I am sorry but there is no way I can guess from a single method call what is happening in your code. Please provide proper details.
Member 15047625 28-Mar-21 5:51am
string.Replace(Environment.NewLine, "< br>"). "\n" and "\r\n" also does not change the output.


There is no space in br but here br gets removed for reasons somewhat known.
Richard MacCutchan 28-Mar-21 5:53am
Yes, because the Replace method returns the new modified string: String.Replace Method (System) | Microsoft Docs[^].
Member 15047625 28-Mar-21 15:35pm
It just replaces Environment.NewLine with raw < br > which displays on the website. It would be wiser to replace it with actual new line, and not let user compile html on their own in their head.
Dave Kreskowiak 29-Mar-21 12:10pm
You're blaming ASP.NET for this when it's not a problem with ASP.NET. It's HTML that doesn't natively support \r\n the way you want.

To fix-up the Windows new line sequence of \r\n to one more friendly to HTML, you simply have to replace the \r characters with nothing, or an empty string.
string newContent = oldContent.Replace("\r", string.Empty);

Try it this way:

<%=myText.Replace("\r\n", "<br/>")%>
Member 15047625 28-Mar-21 6:43am
<%=@item.Body.Replace("\r\n", "br/")%> (br with <>)

Now the output looks like this:

<%=Example text. br/br/Etc.%>
Set style="white-space:pre-line;" on the containing element:
<div style="white-space:pre-line;">
white-space - CSS: Cascading Style Sheets | MDN[^]

Or use a custom helper method, such as the one described in this StackOverflow answer[^]:
public static IHtmlContent RenderNewlines<TModel>(this IHtmlHelper<TModel> html, string content)
    if (string.IsNullOrEmpty(content) || html is null)
        return null;

    TagBuilder brTag = new TagBuilder("br");
    IHtmlContent br = brTag.RenderSelfClosingTag();
    HtmlContentBuilder htmlContent = new HtmlContentBuilder();

    // JAS: On the off chance a browser is using LF instead of CRLF we strip out CR before splitting on LF.
    string lfContent = content.Replace("\r", string.Empty, StringComparison.InvariantCulture);
    string[] lines = lfContent.Split('\n', StringSplitOptions.None);
    foreach(string line in lines)
        _ = htmlContent.Append(line);
        _ = htmlContent.AppendHtml(br);

    return htmlContent;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900