Using Double-Double Quotes in a C# string

Brian Bulla 96 Reputation points
2021-06-10T18:52:55.42+00:00

Hi,

In VB I use the following code to create a string with double-double quotes:

Dim jsonString As String = "{ ""id"": """ + drawingUID + """, ""fullPath"": """ + DestPathContractDB + """, ""link"": """ + LinkPath + """, ""scanFilename"": """ + txtCard.Text + ext + """ }"

This produces something like this in my application:

"{ ""id"": ""9999"", ""fullPath"": ""Z:\\AsBuiltDrawings\\OSHAWA\\OSH-00001.pdf"", ""link"": ""http://cctvweb/cctv/AsBuiltDrawings/OSHAWA/OSH-00001.pdf"", ""scanFilename"": ""OSH-00001.pdf"" }"

I need to convert this to C# code, but I am struggling to get the format of the double-double quotes to work. I have tried almost everything, but can't seem to get it correct. Using @, \, char.....this is driving me crazy. Here is my latest attempt:

string jsonString = "{ " + (char)34 + (char)34 + "id" + (char)34 + (char)34 + ": " + (char)34 + (char)34 + drawingUID + (char)34 + (char)34 + ", " + (char)34 + (char)34 + "fullPath" + (char)34 + (char)34 + ": " + (char)34 + (char)34 + destPath + (char)34 + (char)34 + ", " + (char)34 + (char)34 + "link" + (char)34 + (char)34 + ": " + (char)34 + (char)34 + linkPath + (char)34 + (char)34 + ", " + (char)34 + (char)34 + "scanFilename" + (char)34 + (char)34 + ": " + (char)34 + (char)34 + cardNum + ext + (char)34 + (char)34 + " }";

Which produces this:

"{ \"\"id\"\": \"\"9999\"\", \"\"fullPath\"\": \"\"Z:\\AsBuiltDrawings\\OSHAWA\\OSH-00001.pdf\"\", \"\"link\"\": \"\"http://cctvweb/cctv/AsBuiltDrawings/OSHAWA/OSH-00001.pdf\"\", \"\"scanFilename\"\": \"\"OSH-00001.pdf\"\" }"

Regardless of what I try I always end up getting the \"\" and not just ""

Any help is appreciated.

Thanks,

Developer technologies | C#
{count} votes

Accepted answer
  1. Brian Bulla 96 Reputation points
    2021-06-10T19:33:39.103+00:00

    For some reason I need the double-double quotes.

    Going back to my original post, but writing the string to a text file I seem to be getting what I need. So with all the (char)34 + (char)34 seems to be working.

    I was always using the Watch window and stopping my code when it didn't look right. Do you know why the Watch window shows it with \" instead of an actual ""??

    I'll roll with this for now. Thanks for help and assistance on how the VS IDE works!! :-)


6 additional answers

Sort by: Most helpful
  1. Viorel 122.6K Reputation points
    2021-06-10T19:05:20.247+00:00

    In C# "a\"b" and @"a""b" means a"b.

    To view the real text in Watch window during debugging, enter jsonString,nq.

    0 comments No comments

  2. Brian Bulla 96 Reputation points
    2021-06-10T19:08:29.023+00:00

    Thanks Viorel. I understand how to use double quotes, but the problem is in generating double-double quotes.

    Do you have an example on how to get double-double quotes (ie. ""id"" into the string).

    All of my samples above are taken from the "Watch" window.


  3. Brian Bulla 96 Reputation points
    2021-06-10T19:16:15.493+00:00

    Hi Viorel,

    I agree...it SHOULD work....but for some reason it doesn't.

    So using:

    string jsonString = @"{ ""id"": """ + drawingUID + @""", ""filePath"": """;

    Produces this:

    "{ \"id\": \"b91541c9-4cbc-4357-998c-7228cbd9604b\", \"filePath\": \""
    

    Any other ideas??


  4. Brian Bulla 96 Reputation points
    2021-06-10T19:27:24.177+00:00

    Hmmm....ok, so writing to the text file I get:

    { "id": "b91541c9-4cbc-4357-998c-7228cbd9604b", "filePath": "

    Using: string jsonString = @"{ ""id"": """ + drawingUID + @""", ""filePath"": """;

    So the double-double quotes are still missing.

    Any other ideas??


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.