【问题标题】:Escape curly brackets around a template entry when using String.Format [duplicate]使用 String.Format 时,在模板条目周围转义大括号 [重复]
【发布时间】:2012-11-26 02:55:28
【问题描述】:

可能重复:
String.Format exception when format string contains “{”

是否可以使用C# String.Format 进行以下操作?

需要的输出"products/item/{itemId}"

我试过转义大括号,但这不起作用:

const string itemIdPattern = "itemId";
string result = String.Format("products/item/{{0}}", itemIdPattern);

最好是比

更好的东西
string result = String.Format("products/item/{0}{1}{2}", 
                              "{", 
                              itemIdPattern, 
                              "}");   

【问题讨论】:

    标签: c# .net string.format


    【解决方案1】:

    为此,每边需要 3 个大括号 - 2 个用于大括号,1 个用于替换。

    string result = String.Format("products/item/{{{0}}}", itemIdPattern);
    

    【讨论】:

      猜你喜欢
      • 2011-04-15
      • 2014-05-31
      • 1970-01-01
      • 1970-01-01
      • 2022-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多