Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escaping arguments for string.Format in a C# multiline verbatim string

string template = @"  
          {  
            argument1   = ""{0}"";
            argument2   = {1};  
          }";

When I format it as a usual string with string.Format, naturally i get an exception that the input string was not in correct format. I tried escaping the arguments as it is recommended in msdn documentation, like "{{0}}" and even "{{{0}}}", but i still get the same exception. Any ideas on how to format such a string?

Thanks!

P.S.[edit] my original string is for generating a WCAT scenario file:

 string scenarioHeaderTemplate = @"
    scenario
    {{
       name    = ""WCAT Scenario"";
       warmup      = {0};
       duration    = {1};
       cooldown    = {2};

       default
       {
           version     = HTTP11;
           setheader
           {
               name    = ""Connection"";
               value   = ""keep-alive"";
           }
           statuscode  = 200;
           close       = ka;
       }
     }}";

and it throws if i try string.Format(scenarioHeaderTemplate, 10, 10, 10);

like image 858
kateroh Avatar asked Oct 08 '10 19:10

kateroh


1 Answers

The problem is the open and close braces. You need to quote those, or Format will think you're begining a parameter specifier.

string template = @"   
          {{   
            argument1   = ""{0}""; 
            argument2   = {1};   
          }}"; 
like image 199
John Saunders Avatar answered Oct 24 '22 05:10

John Saunders