Go Premium for a chance to win a PS4. Enter to Win


line break in C#

Posted on 2004-10-05
Medium Priority
Last Modified: 2012-06-27
Hi guys:

How do we put line breaks in the code editor when writing C# statements like the following

string s = "This is a very looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
ooooooooooooooooooooooog string";
Question by:rxraza
  • 3
  • 2
  • 2
  • +4

Expert Comment

ID: 12231157
add \n inside the " " for line breaks.


string s = "This is a very loooooooooooooooooooooooooo\noooooooooooooooooooooooooooooooooooooo\nooooooooooooooooo
ooooooooooooooooooooooog\n string";

will give you:

This is a very loooooooooooooooooooooooooo
LVL 15

Expert Comment

ID: 12231171
The cross-platform way to do it is with Environment.NewLine.

string s = "line 1" + Environment.NewLine + "line 2";

Expert Comment

ID: 12231281
In the editor itself you use:

string s = "This is a very loooooooooooooooooooooooooooooooo" +
"ooooooooooooooooooooooooooooooooooooooooooooooooooo" +
"oooooooooooooooooooooooooooooooooooooooooooong string";
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.


Author Comment

ID: 12231508
Actually I meant to put line breaks in the code editor itself.


your approach is what I have been doing but I am fed up with it. Is there a way I would do that without appending smaller strings. I do not want to use '+' operator.  

Accepted Solution

Razzie_ earned 120 total points
ID: 12231715
to the best of my knowledge no, that is not possible.

But I don't understand your comment: 'do that without appending smaller strings'. One big string will automatically be cut up in smaller strings. How did you had in mind?
LVL 22

Expert Comment

ID: 12232082

Well.... There is always the string builder class as well as the += operator.

StringBuilder sb =  new StringBuilder();

sb.Add("This is");
sb.Add(" A Very Loooo");
sb.Add("oong string");

string str = string.empty;

str += "This is";
str += " A Very Loooo";
str += "oooooooooooo";
str += "oong string";

Expert Comment

ID: 12232297

I think that is a bad way just to make the code editor a bit more readable. Concatenating the string at run-time which means an unavoidable performance hit just so you don't have to use the horizontal scrollbar in your editor would not be my choice. That would be the same as writing int i = 10; i = i*2; i = i-5; instead of writing int i = 15; :)
LVL 15

Expert Comment

ID: 12233132
The StringBuilder would probably yield better performance that concatenating with + or +=, though. I would have to agree with Razzie though that in general, you should not sacrifice performance just so the code looks prettier.
LVL 22

Assisted Solution

_TAD_ earned 80 total points
ID: 12233218

I guess it depends on what you are doing.

If it is just 1 string, or even 100 strings that are going to be a constant, I'd make my code look pretty and forget about the performance hit.  You are talking about an additional 4 to 7 cycle overhead for each concatenation.

Let's see... I have a 2.1 Ghz computer and I make 500 concatenations (100 strings, 5 joins each).  That would be, at worst, 3,500 additional cycles which is roughly 1.7 seconds additional load time for the entire duration of your program.

In that scenario, I'd go with the concatenations.  

I would only be concerned if the concatenations were in a loop somewhere and run 1,000's of times.

I'm not suggesting that this is the optimal solution.  I am merely giving you an altenative that may be feasible for your design,  As I've said... If this is a one time loading, be friendly to your programmer, with today's computers efficiency is not always a high priority.  


Author Comment

ID: 12233247
>>I think that is a bad way just to make the code editor a bit more readable

I totally agree with Razzie, the thing is that I have been thinking about this performace issue that comes with contcatenation or using StringBuilder or String.Format() and cumulatively it adds up no matter what and who knows the code of today can be used in other systems that are not necessarily computers like wireless devices.

I thought may be there was a way out of it, but apparently there is not any. I am going to do a split.

Thanks guys for sharing your thoughts...

Expert Comment

ID: 12347274
Summarizing FYI:

The StringBuilder is the prefered ways to concat strings, it has a better performance than concatenating Strings directly.
For each myString += "myString" a new String object is initalized, wich is less memory efficient and reduces your performance.

You should either put your string on one line, or use the StringBuilder...

LVL 11

Expert Comment

ID: 12352337
i think you mean something like the "\" in cpp or the _ in vb ?
in c# you could use the @, but your string will be filled with all the tabs and spaces.
the + is an operator and not a preprocessor statement, so it will concatenate two or more strings at compile time.
do you ask cause you wanna format your strings in your ide to a block structure?
the @ is a possibility but not equivalent to the backslash in cpp or the underline in vb.

string s = @"This is a very                     looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
ooooooooooooooooooooooong string";

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Integration Management Part 2

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question