Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put a new line inside a string in C# source code?

Tags:

syntax

c#

Is there a way in C# sytax for defining a new line? (Like in VB _ )

Example:

Instead of:

string myStr = "very long string" +
"another long string";

This:

string myStr = "very long string \something
another long string"

Or; does compiler handles this and does something like; "string" + "string" -> "stringstring" in this example?

Different cases are welcome like when these strings are constants, etc.

like image 254
yusuf Avatar asked Jan 21 '09 12:01

yusuf


People also ask

How do I add a new line in string format?

In Windows, a new line is denoted using “\r\n”, sometimes called a Carriage Return and Line Feed, or CRLF. Adding a new line in Java is as simple as including “\n” , “\r”, or “\r\n” at the end of our string.

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.

How do you break a line through a string?

Line breaks differ between platforms in strings, although the most frequent ones are: Windows: Carry return \r\n followed by a character on the newline. Linux: \n a character on a newline. Older Macs: a character returned by a carriage.

Is newline a char in C?

In programming languages, such as C, Java, and Perl, the newline character is represented as a '\n' which is an escape sequence.


2 Answers

Compiler turns "ABC" + "DEF" into "ABCDEF" so there's no cost in using +

like image 56
Dead account Avatar answered Oct 11 '22 02:10

Dead account


You can use

string myStr = @"very long string
another long string";

@" is the beginning of a string literal and this kind of string does not end until the next " character is found.

There is not an exact syntax for c# which does the same thing as _ character in vb. So if you want to avoid including a new line, you must manually connect your strings on different lines.

like image 25
Serhat Ozgel Avatar answered Oct 11 '22 01:10

Serhat Ozgel