Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C#, what's the best way to spread a single-line string literal across multiple source lines?

Suppose that you have a lengthy string (> 80 characters) that you want to spread across multiple source lines, but don't want to include any newline characters.

One option is to concatenate substrings:

string longString = "Lorem ipsum dolor sit amet, consectetur adipisicing" +     " elit, sed do eiusmod tempor incididunt ut labore et dolore magna" +     " aliqua. Ut enim ad minim veniam"; 

Is there a better way, or is this the best option?

Edit: By "best", I mean easiest for the coder to read, write, and edit. For example, if you did want newlines, it's very easy to look at:

string longString = @"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam"; 

I am wondering if there is something just as clean when you don't want newlines.

like image 308
Matthew Avatar asked Jan 02 '10 02:01

Matthew


People also ask

What does |= mean in C?

The ' |= ' symbol is the bitwise OR assignment operator.

What is '~' in C programming?

In mathematics, the tilde often represents approximation, especially when used in duplicate, and is sometimes called the "equivalency sign." In regular expressions, the tilde is used as an operator in pattern matching, and in C programming, it is used as a bitwise operator representing a unary negation (i.e., "bitwise ...

What are operators in C?

C operators are one of the features in C which has symbols that can be used to perform mathematical, relational, bitwise, conditional, or logical manipulations. The C programming language has a lot of built-in operators to perform various tasks as per the need of the program.

What is the use of in C?

In C/C++, the # sign marks preprocessor directives. If you're not familiar with the preprocessor, it works as part of the compilation process, handling includes, macros, and more.


1 Answers

I would use a variation of your method:

string longString =     "Lorem ipsum dolor sit amet, consectetur adipisicing " +      "elit, sed do eiusmod tempor incididunt ut labore et dolore magna " +      "aliqua. Ut enim ad minim veniam."; 

Here I start the string on the line after the equals sign so that they all line up, and I also make sure the space occurs at the end of the line (again, for alignment purposes).

like image 94
bobbymcr Avatar answered Sep 19 '22 15:09

bobbymcr