Search code examples
c#stringverbatim

How to define a string like: sss" + str1 + "ddd


string str1="xxx";
string str2=@"sss" + str1 + "ddd";
Console.WriteLine(str2);

The above code gives:

sssxxxddd

But what I want is:

sss" + str1 + "ddd

How to do that?


Solution

  • You can escape the quotes by preceding them with a backslash (\).

    string str1 = "xxx";
    string str2 = "sss\" + str1 + \"ddd";
    Console.WriteLine(str2);
    

    For strings prefixed with the @ character, quotes are escaped by placing two together (i.e., string str2 = "sss"" + str1 + ""ddd").