-3

In my code i got a very long and complicated string that I want to save as a literal string At the moment my line is 1200 character long. And I want to separate the lines in the way which every line it wouldn't be longer than 200 characters

string comlexLine = @"{""A"": {""B"": [{""C"": {""D"": {""E"":""+"" ""/F;

I would like to separate it into shorter lines so the code would be more readable.

At the moment when I enter a new line, because it is a literal string a \r is entering to the string

for example:

string comlexLine = @"{""A"": {""B"": "
                     + "[{""C"": {""D"": {""E"":""+"" ""/F;
 Console.WriteLine(comlexLine); 

would print:

 @"{""A"": {""B"": //r "[{""C"": {""D"": {""E"":""+"" ""/F

I prefer not to split it to different constant and also to use a literal string. Is there any solution?

Rafa_G
  • 45
  • 1
  • 10
  • How about to put your line in resource file? – Aleks Andreev Jul 27 '17 at 17:17
  • If you split the string over two lines you have two options: Split the string into two strings and concatenate with `+`, this will not add a linefeed to the string. Use the literal string syntax, `@"..."` and split it inside, this will add a linefeed inside. Those are your options. – Lasse V. Karlsen Jul 27 '17 at 17:21
  • Your example with the `+` in it won't add a newline, nor does it compile. Can you clarify what your problem with that statement is? It seems to me that you already know how to solve this. – Lasse V. Karlsen Jul 27 '17 at 18:19
  • Yes. I see that the people that comment here not fumiler with literal strings. Its a kind of a string that what ever you write - for example jump a line it inserts it to the string. – Rafa_G Jul 29 '17 at 19:30

2 Answers2

1

Use Environment.NewLine instead for including a new line in your string. i would rather make it like below using a back slash \ to escape the extra double quotes

string comlexLine = "{\"A\": {\"B\": " + Environment.NewLine + "[{\"C\": {\"D\": {\"E\":\"+\" \"/F";
Console.WriteLine(comlexLine);
Rahul
  • 76,197
  • 13
  • 71
  • 125
1

Try not using the literal and escaping the double quotes with a slash.

string comlexLine = "{\"A\": {\"B\": [{\"C\": "
+ "{\"D\": {\"E\":\"+\" \"/F";

If I use that, it doesn't introduce the //r.

Will W.
  • 46
  • 1
  • 4
  • you cant add "+" to a literal string without changing it. literal string is "literal" and will add everything that you write to the string – Rafa_G Jul 30 '17 at 10:51