Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a carriage return in my C# string

Tags:

c#

I have C# code that creates HTML. One particular piece of code creates a long string which is no problem for the browser. However when I look at the code with view > source it's difficult to see what's happening.

Is there some way that I can insert a carriage return or new line into my string so that it will make the string continue on the next line.

Thanks,

like image 242
Amy Avatar asked Jul 24 '11 12:07

Amy


People also ask

How do you type a carriage return?

CR = Carriage Return ( \r , 0x0D in hexadecimal, 13 in decimal) — moves the cursor to the beginning of the line without advancing to the next line.

What does \r do in C programming?

When writing to an interactive terminal on stdout or stderr , '\r' can be used to move the cursor back to the beginning of the line, to overwrite it with new contents.

What is the symbol for carriage return?

CR (character : \r, Unicode : U+000D, ASCII : 13, hex : 0x0d) : This is simply the 'r' character. This character is commonly known as 'Carriage Return'.

How do I add a new line in C?

The escape sequence \n means newline. When a newline appears in the string output by a printf, the newline causes the cursor to position to the beginning of the next line on the screen.


2 Answers

You can put \r\n in your string.

like image 193
SLaks Avatar answered Sep 20 '22 13:09

SLaks


Along with Environment.NewLine and the literal \r\n or just \n you may also use a verbatim string in C#. These begin with @ and can have embedded newlines. The only thing to keep in mind is that " needs to be escaped as "". An example:

string s = @"This is a string that contains embedded new lines, that will appear when this string is used." 
like image 34
dbb Avatar answered Sep 19 '22 13:09

dbb