What is "\n"? Any difference from "\r"?

I know "\r" indicates a sentence break. Then what about "\n"

HtmlText = HtmlText.Replace("\r\n","\r");

HtmlText = HtmlText.Replace("\n","\r");

HtmlText = HtmlText.Replace("\r\r","<p>");

HtmlText = HtmlText.Replace("\r","<br>");

In the above line, since "\r" is to changed into "<br>", why should not directly change "\n" and "\r\n"directly into "<br>"

http://west-wind.com/weblog/posts/1499.aspx




Answer this question

What is "\n"? Any difference from "\r"?

  • Philippe Cand

    if you are asking why it does not automatically convert to <br>, its because its not an HTML language :-)

    it shows the raw representation of the command

    i believe it may change it as \r is a carriage return, and a carriage return in html must be <br>

    \n is a newline, used for .NET/C++



  • Svitlana

    \n is newline

    \r\n I believe is carriage return (this also applies for Environment.NewLine)

    I also believe Environment.NewLine is probably the better approach, to make sure it is a newline



  • Tammt

    The 1st statement replaces a DOS end-of-line to a \r, the 2nd replaces a Unix end-of-line to a \r. Now he can detect an empty line and replace it with a paragraph. Directly replacing would allow that to work...


  • John Sobernheim

    Thank you all very much.

    Very grateful for your kindness!



  • lex23456

    \r is carriage return ( in DOS mode, used to bring cursor position to the beginning of a line )
    \n is new line

    in Windows, new line is written as "\r\n" in a text file
    in Linux, new line is written as "\n" in a text file

    The above code is to ensure proper replacement whether system uses "\r" only, "\n" only or both "\r\n" to represent new line.

    I think just use HtmlText.Replace ( "\n", "<BR>" ) will work ( leaving "\r" as it is ), since in all system new line will always have "\n" character CMIIMW


  • Rodrigo B. de Oliveira

    why should not change "\n" and "\r\n"directly into "<br>"

  • What is "\n"? Any difference from "\r"?