Solved

line break in C#

Posted on 2004-10-05
12
2,069 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
  • 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
 

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 30 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 20 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Events in static methods 3 50
scanning dentists xray (the small ones) 3 45
Error in script 11 47
C#  Radio button search for Date not DateTime 4 35
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…
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…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

895 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now