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

C#StringEscaping

C# Problem Overview


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

C# Solutions


Solution 1 - C#

Try this:

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

Solution 2 - C#

The double character usage also works with the characters { and } when you're 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
    );

Solution 3 - C#

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

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

Personally i think you should go with

string s = string.format("{0}\"{1},"something","something else"); 

it makes it easier in the long run

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionUserView Question on Stackoverflow
Solution 1 - C#mqpView Answer on Stackoverflow
Solution 2 - C#Gordon Mackie JoanMiroView Answer on Stackoverflow
Solution 3 - C#Crash893View Answer on Stackoverflow