简体   繁体   English

在C#多行逐字字符串中转义string.Format的参数

[英]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. 当我将它格式化为string.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. 我尝试转义msdn文档中推荐的参数,比如“{{0}}”甚至“{{{0}}}”,但我仍然得到相同的异常。 Any ideas on how to format such a string? 关于如何格式化这样一个字符串的任何想法?

Thanks! 谢谢!

PS[edit] my original string is for generating a WCAT scenario file: PS [edit]我的原始字符串用于生成WCAT场景文件:

 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); 如果我尝试string.Format(scenarioHeaderTemplate,10,10,10),它会抛出;

The problem is the open and close braces. 问题是开放和紧密的支撑。 You need to quote those, or Format will think you're begining a parameter specifier. 你需要引用它们,否则Format会认为你是一个参数说明符。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM