views:

591

answers:

3

If I write something like this:

string s = @"...."......";

it doesn't work.


If I try this:

string s = @"...\".....";

it doesn't work either.

How can I add a " character to a multi line string declaration in C#?

+19  A: 

Try this:

string s = @"..."".....";
mquander
Hm, compiler does not complain now. :) But how does it work? Double "" has some special meaning when inside a string declaration?
User
When dealing with literal strings (the ones prefixed with "@") then you use the double-quote to escape itself instead of a backslash.
mquander
One learn all the time. Thank you. :)
User
+2  A: 

The double character usage also works with '{' and '}' characters when using string.Format and you want to include a literal instance of either rather than indicate a parameter argument, for example:

string jsString = string.Format("var jsonUrls = {{firstUrl: '{0}', secondUrl: '{1}'}};", firstUrl, secondUrl);
Gordon Mackie JoanMiro
I was just thinking about other characters. You guessed it. Thank you. :)
User
+1  A: 

string s = "...\"....."; should work

the @ disables escapes so if you want to use \" then no @ symbol

Personely i think you should go with

string s = string.format("{0}\"{1},"something","something else"); it makes it easeir in the long run

Crash893
There is no need to format anything. Was just a multiline SQL query in code which I was trying to fix.
User