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

340 Views Asked by At
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?

5

There are 5 best solutions below

1
On BEST ANSWER

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").

3
On

Here you go:

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

        string str3 = "xxx";
        string str4 = "sss\" + str1 + \"ddd";
        Console.WriteLine(str4);
        Console.ReadKey();
3
On
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";

5
On

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();