s.k.paul
s.k.paul

Reputation: 7301

Double Quotes in C# String.Format

I want to create a string as shown in the text below :

'bookNo':'" + bookNo + "'

My code is

String KeyValuePair =
      String.Format("'{0}':'\" + {1} + \"'", key,value);

But its not returning exact string as above. What can be done to achieve the same ?

Upvotes: 0

Views: 10286

Answers (2)

This is simply doing it

String key= "BookName";
String value = "BookValue";
String result = String.Format(@"'{0}:' ""+{1}+""", key, value);

Upvotes: 0

user2771704
user2771704

Reputation: 6212

I know two ways to do it: (I have already tested it)

1) using escape sequences \" and \'

string.Format("\'{0}\':\'\" + {1} + \"\'",key,value);

2) using verbatim string character @

string.Format(@"'{0}':'"" + {1} + ""'",key,value);

Hope it will help you.

Upvotes: 3

Related Questions