none
c# string format RRS feed

  • Question

  • Hi, I have string like this { ""{ template1 }"":""car"",""{ template2} "":""red""}, how can I turn it into the @"{ ""{ template1 }"":""car"",""{ template2} "":""red""}"
    Tuesday, November 28, 2017 9:22 PM

All replies

  • I don't understand.


    Sam Hobbs
    SimpleSamples.Info

    Wednesday, November 29, 2017 12:06 AM
  • Hello sdnd2000,

    You need to use escape character \" rather than use " directly. Because your string has a lot of " " " so that the complier doesn't know where the string starts where it ends.

      string str="{\"\"{ template1 } \"\":\"\"car\"\",\"\"{ template2} \"\":\"\"red\"\"}"; 

    For more information about escape character you could see here.

    Sincerely,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Edited by Fei HuModerator Wednesday, December 6, 2017 2:37 AM Format error
    • Proposed as answer by Cor Ligthert Wednesday, December 6, 2017 10:05 AM
    Wednesday, November 29, 2017 1:46 AM
    Moderator
  • sorry, I mean I have a string

    string = {""{ template1 }"":""car"",""{ template2} "":""red""}

    how can I convert into string literal 

    string string=@"{ ""{ template1 }"":""car"",""{ template2} "":""red""}";

    Wednesday, November 29, 2017 2:16 AM
  • Hello sdnd2000,

    string = {""{ template1 }"":""car"",""{ template2} "":""red""}

    The above string doesn't match c# syntax, it can't pass to compile in visual studio. Where the string come from? .txt file, database or etc.

    And when you read string from them , .Net has done some job to convert  string format using escape character.  Actually using "@" character will add escape character For some specific characters in the compile time. In other words, they are equal.

    And you could see the str final value use debug tool as above.

    Sincerely,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, November 29, 2017 2:51 AM
    Moderator
  • Hi, Neil,

    It is passed in from other application, how can I handle this string, and make it compatible with c# 

    Wednesday, November 29, 2017 3:07 AM
  • Hello sdnd2000,

    >>how can I handle this string

    I can't get more detailed information about you want, you could provide the relating code with the string. If you copy the text from other applications, you should add escaped characters manually. Please share your code example and tell me the desired output.

    Best regards,

    Neil Hu 


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, November 29, 2017 4:13 AM
    Moderator
  • It is not clear to me if you are getting the string from some other program as data during execution or if you need to enter the string as a constant in your source program. Or maybe something between those two.

    Is the string a JSON data? If it is then it will be much easier to create and manage if it is processed as JSON data.



    Sam Hobbs
    SimpleSamples.Info

    Wednesday, November 29, 2017 5:17 AM
  • As I have understood your query, you can use String.Format for this.

    string str = {""{ template1 }"":""car"",""{ template2} "":""red""};
    
    string str = string.Format("""{0}"":""{1},""{2}"":""{3}", "template1","car","template2","red");


    Software Developer

    Wednesday, December 6, 2017 8:38 AM
  • Hi Sdnd2000,

    Please use below

    string str="{ \"\"{ template1 }\"\":\"\"car\"\",\"\"{ template2} \"\":\"\"red\"\"}";
    Console.WriteLine(str);

    This will display as you want.

    Wednesday, December 6, 2017 8:45 AM