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

1

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

Shushan answered 8/10, 2010 at 19:15 Comment(0)
C
31

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};   
          }}"; 
Carom answered 8/10, 2010 at 19:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.