Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding line break in C# Code behind page

Tags:

c#

I have written a code in C# which is exceeding page width, so i want it to be broken into next line according to my formatting. I tried to search a lot to get that character for line break but was not able to find out.

In VB.NET I use '_' for line break, same way what is used in C# ? I am trying to break a string.

Thanks in Advance Shantanu Gupta

like image 386
Shantanu Gupta Avatar asked Nov 23 '09 14:11

Shantanu Gupta


People also ask

How do you add a new line in C?

The newline character ( \n ) is called an escape sequence, and it forces the cursor to change its position to the beginning of the next line on the screen. This results in a new line.

What does \t in C do?

\t (Horizontal tab) – We use it to shift the cursor to a couple of spaces to the right in the same line. \a (Audible bell) – A beep is generated indicating the execution of the program to alert the user. \r (Carriage Return) – We use it to position the cursor to the beginning of the current line.

What does \n do C?

In C, all escape sequences consist of two or more characters, the first of which is the backslash, \ (called the "Escape character"); the remaining characters determine the interpretation of the escape sequence. For example, \n is an escape sequence that denotes a newline character.

How do I add a new line in printf?

The printf statement does not automatically append a newline to its output. It outputs only what the format string specifies. So if a newline is needed, you must include one in the format string. The output separator variables OFS and ORS have no effect on printf statements.


2 Answers

In C# there's no 'new line' character like there is in VB.NET. The end of a logical 'line' of code is denoted by a ';'. If you wish to break the line of code over multiple lines, just hit the carriage return (or if you want to programmatically add it (for programmatically generated code) insert 'Environment.NewLine' or '\r\n'.

Edit: In response to your comment: If you wish to break a string over multiple lines (i.e. programmatically), you should insert the Environment.NewLine character. This will take the environment into account in order to create the line ending. For instance, many environments, including Unix/Linux only use a NewLine character (\n), but Windows uses both carriage return and line feed (\r\n). So to break a string you would use:

string output = "Hello this is my string\r\nthat I want broken over multiple lines."

Of course, this would only be good for Windows, so before I get flamed for incorrect practice you should actually do this:

string output = string.Format("Hello this is my string{0}that I want broken over multiple lines.", Environment.NewLine);

Or if you want to break over multiple lines in your IDE, you would do:

string output = "My string"
              + "is split over"
              + "multiple lines";
like image 191
BenAlabaster Avatar answered Oct 02 '22 15:10

BenAlabaster


Option A: concatenate several string literal into one:

string myText = "Looking up into the night sky is looking into infinity" +
    " - distance is incomprehensible and therefore meaningless.";

Option B: use a single multiline string literal:

string myText = @"Looking up into the night sky is looking into infinity
- distance is incomprehensible and therefore meaningless.";

With option B, the newline character(s) will be part of the string saved into variable myText. This might, or might not, be what you want.

like image 40
Jørn Schou-Rode Avatar answered Oct 02 '22 15:10

Jørn Schou-Rode