Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

newline character in c# string

Tags:

string

c#

newline

I have some html code in a C# string. If I look with the Text Visualizer of Visual Studio I can see it has numerous newlines in it. However, after i apply this code

string modifiedString = originalString.Replace(Environment.NewLine, "<br />");

and then I look with the Text Visualizer at modifiedString I can see it doesn't have anymore newlines except for 3 places. Are there any other character types than resemble newline and I am missing?

like image 423
peter Avatar asked Jan 08 '13 12:01

peter


People also ask

Does \n work in C?

In most C compilers, including ours, the newline escape sequence '\n' yields an ASCII line feed character. The C escape sequence for a carriage return is '\r'.

Which character used for newline?

LF (character : \n, Unicode : U+000A, ASCII : 10, hex : 0x0a): This is simply the '\n' character which we all know from our early programming days. This character is commonly known as the 'Line Feed' or 'Newline Character'.

What is the meaning of \n and \r in C?

in Unix and all Unix-like systems, \n is the code for end-of-line, \r means nothing special.


1 Answers

They might be just a \r or a \n. I just checked and the text visualizer in VS 2010 displays both as newlines as well as \r\n.

This string

string test = "blah\r\nblah\rblah\nblah";

Shows up as

blah
blah
blah
blah

in the text visualizer.

So you could try

string modifiedString = originalString
    .Replace(Environment.NewLine, "<br />")
    .Replace("\r", "<br />")
    .Replace("\n", "<br />");
like image 122
juharr Avatar answered Oct 01 '22 07:10

juharr