Click here to Skip to main content
15,891,763 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I have a long line of code that is for a string format and would like to know how I can spread it over multiple lines so it is easier to read in the editor screen?

C#
const string format = "{0,3} | {1,6} |A {2,6} |B {3,4} |C {4,4} |D {5,4} |E {6,4} |F {7,4} |G {8,3} |H {9,4} |I {10,4} |J {11,3} |K {12,2} |L {13,2} |M {14,6} |N {15,6} |O {16,6} |P {17,6} |Q {18,1} |R {19,4} |S {20,2} |T {21,4} |U {22,4} |V {23,3} |W {24,8}";


Appreciate the help,
suprsnipes
Posted
Updated 7-Mar-13 17:05pm
v2
Comments
AspDotNetDev 8-Mar-13 13:58pm    
For this particular line of code, I'd store the string in a configuration file and load it. You wouldn't be able to make it a const, but you'd still be able to make it readonly.

If you don't want any embedded end-of-line characters, then the @"..." syntax won't help.
But, you can use the concatenation operator (+):
C#
const string format = "{0,3} | {1,6} |A {2,6} |B {3,4} |C {4,4} |D {5,4} |E {6,4}" + 
                      " |F {7,4} |G {8,3} |H {9,4} |I {10,4} |J {11,3} |K {12,2}" + 
                      " |L {13,2} |M {14,6} |N {15,6} |O {16,6} |P {17,6} |Q {18,1}" + 
                      " |R {19,4} |S {20,2} |T {21,4} |U {22,4} |V {23,3} |W {24,8}";
 
Share this answer
 
It the code is bad, formatting of the code won't help. But some reason, you demonstrate two of the trends haunting beginners very badly these days: trying to work with strings representing data instead of data itself, and hard-coding. This is very hard to cure.

As to your question, multi-line string literal can be written in verbose form, when you prefix it with '@':
C#
const string format = @"begin here
continue
continue...
...
and so on...
end";


In your case, it makes very little sense.

Read the language manual before writing who knows what. What you are righting right now is not programming at all.

Wish you the best of luck,
—SA
 
Share this answer
 
v2
Comments
Matt T Heffron 8-Mar-13 13:34pm    
But this solution does not define the constant with the same value. See my solution #3.
Sergey Alexandrovich Kryukov 8-Mar-13 14:37pm    
Only because I don't see any sense in doing it... OP just needed the syntax. But you are right about end-of line characters. The can be ignored during processing.
The whole thing does not deserve arguing I think; this is an ugly way of defining data...
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900