Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

line break in C#

Posted on 2004-10-05
12
Medium Priority
?
2,111 Views
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";
0
Comment
Question by:rxraza
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +4
12 Comments
 

Expert Comment

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

Example:

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

will give you:

This is a very loooooooooooooooooooooooooo
oooooooooooooooooooooooooooooooooooooo
nooooooooooooooooo
ooooooooooooooooooooooog
string
0
 
LVL 15

Expert Comment

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

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

Expert Comment

by:Razzie_
ID: 12231281
In the editor itself you use:

string s = "This is a very loooooooooooooooooooooooooooooooo" +
"ooooooooooooooooooooooooooooooooooooooooooooooooooo" +
"oooooooooooooooooooooooooooooooooooooooooooong string";
0
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.

 

Author Comment

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

Razzie:

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.  
0
 
LVL 8

Accepted Solution

by:
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?
0
 
LVL 22

Expert Comment

by:_TAD_
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("oooooooooooo");
sb.Add("oong string");




string str = string.empty;

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

Expert Comment

by:Razzie_
ID: 12232297
_TAD_,

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; :)
0
 
LVL 15

Expert Comment

by:Timbo87
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.
0
 
LVL 22

Assisted Solution

by:_TAD_
_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.  

0
 

Author Comment

by:rxraza
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...
0
 
LVL 3

Expert Comment

by:Uritsukidoji
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...



0
 
LVL 11

Expert Comment

by:vo1d
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";
0

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

670 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