Solved

line break in C#

Posted on 2004-10-05
12
2,066 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
network + 7 73
Unit testing, moq, best practices 7 29
COnsume rest client 6 15
C# TextBox 11 15
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 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

16 Experts available now in Live!

Get 1:1 Help Now