Reputation:
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?
Upvotes: 0
Views: 357
Reputation: 67345
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"
).
Upvotes: 4
Reputation: 21
You may try this
string str1="xxx";
string str2=@"sss\"" + str1 + "\"ddd";
Console.WriteLine(str2);
EDITED
string str1 = "\"xxx\"";
string str2 = "sss" + str1 + "ddd";
Console.WriteLine(str2);
Console.ReadLine();
Upvotes: -3
Reputation: 221
string str1 = "xxx";
string str2 = @"sss"" + str1 + ""ddd";
Console.WriteLine(str2);
string str3 = "xxx";
string str4 = "sss\" + str1 + \"ddd";
Console.WriteLine(str4);
Console.ReadKey();
Upvotes: 3
Reputation: 20
string str1="xxx";
string str2=@"sss""" + str1 + @"""ddd";
Console.WriteLine(str2);
or
string str1="xxx";
string str2="sss\"" + str1 + "\"ddd";
Console.WriteLine(str2);
This will give you an answer like: sss"xxx"ddd. If you want an answer like sss" + str1 + "ddd then you replace the second line with this: string str2=@"sss"" + str1 + ""ddd";
Upvotes: -2