• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 10658
  • Last Modified:

C# break string literal across multiple lines

I'd like to break a very long string literal across multiple lines in the source code.  In VB, this is done with the '_' (underscore) character dangling at the end.  How can I do this in C#?  I can't seem to find a line continuation character for C#.
0
rschaaf
Asked:
rschaaf
  • 2
  • 2
  • 2
  • +1
1 Solution
 
WinterMuteUKCommented:
You could do it like this:

string str =
     "thi" +
     "s is a string";

admittedly it's not exactly the same.
0
 
tomvergoteCommented:
the semicolon serves as a line ending, so you can just type breaks, they don't mean anything in c#, the command finishes with the ;
0
 
AvonWyssCommented:
C# sees line breaks as white space, that is, having the same meaning like a space or a tab. Therefore, everywhere where you can add spaces, you can also add line breaks without the need for a line contuinuation character.

Note that you can also span a string across multiple lines when using the literal string prefix:
string str=@"this is a string.
it contains two lines (not just in the source code)!";

Hope this helps.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
rschaafAuthor Commented:
The suggestion from AvonWyss helps, but it's still not the answer I'm lookng for.  In your example, "This is a string" and "it contains two lines" will be displayed on two lines when that string is displayed in a MessageBox, for example.  There is no real continuation happening.
0
 
AvonWyssCommented:
Well. If you write a literal string, it will also contain the line breaks. This is intentional, as you may have guessed by the contents of that two-line string. If you want to span it without line breaks, use the knowledge that "a"+"b" is the same as "ab" and that you may add any number and any type of whitespace between the string parts and operators (here the + operator):
string str="a"+
"b";
0
 
rschaafAuthor Commented:
Unless someone else chimes in, I'll assume that there is no line continuation character (analogous to the '_' char in VB).
0
 
WinterMuteUKCommented:
There isn't a line continuation character for the reason that tomvergote gave, and both my and AvonWyss' suggestions of the concatenation character '+' is the only way to do what you are looking for.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now