user7728939
user7728939

Reputation:

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?

Upvotes: 0

Views: 357

Answers (5)

Jonathan Wood
Jonathan Wood

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

OreaSedap
OreaSedap

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

Gordon
Gordon

Reputation: 576

Here you go:

 Console.WriteLine("sss\" + str1 + \"ddd");

Upvotes: 3

Xss
Xss

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

Harsh Mehta
Harsh Mehta

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

Related Questions