Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String.Format in Double Quotes in C#

Tags:

c#

.net

Is anybody can help me with string.Format for below line. In '2056' I need to pass as {0}.

string body = @"{""idTimeSerie"":""2056"",""idTso"":-1}";

Due to double quotes I can't get it to execute.

I've tried in this way but no success.

string body = string.Format
                    (@"{""idTimeSerie"": "" \"{0}\" "",""idTso"":-1}", countryID);
like image 555
user1990395 Avatar asked Sep 01 '25 10:09

user1990395


1 Answers

you have to escape the curly braces

replace { to {{

string body = @"{{""idTimeSerie"":""2056"",""idTso"":-1}}";

Edit : From MSDN - Another way of Escaping

Opening and closing braces are interpreted as starting and ending a format item. Consequently, you must use an escape sequence to display a literal opening brace or closing brace. Specify two opening braces ("{{") in the fixed text to display one opening brace ("{"), or two closing braces ("}}") to display one closing brace ("}"). Braces in a format item are interpreted sequentially in the order they are encountered. Interpreting nested braces is not supported.

int value = 6324;
string output = string.Format("{0}{1:D}{2}", 
                             "{", value, "}");
Console.WriteLine(output);
// The example displays the following output: 
//       {6324}
like image 190
aked Avatar answered Sep 04 '25 11:09

aked