Solved

line break in C#

Posted on 2004-10-05
12
2,076 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

772 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