【问题标题】:string.Format throwing exceptionstring.Format 抛出异常
【发布时间】:2011-06-30 07:47:46
【问题描述】:

我有一个关于 String.Format 的问题,需要帮助:

string Placeholder = @"(function({0}, $, undefined) { {1} }( window.{0} = window.{0} || {}, jQuery));";
string output = string.Format(Placeholder, "Value1", "Value2");

String.Format 处出现以下异常

'string.Format(Placeholder, "Value1", "Value2")' 抛出类型为 'System.FormatException' 的异常字符串 {System.FormatException}

知道为什么吗?

【问题讨论】:

    标签: c# .net string


    【解决方案1】:

    这是因为大括号:{ {1} }|| {}。使用双打:

    string Placeholder = @"(function({0}, $, undefined) {{ {1} }} (window.{0} = window.{0} || {{}}, jQuery));";
    字符串输出 = string.Format(Placeholder, "Value1", "Value2");

    http://geekswithblogs.net/jonasb/archive/2007/03/05/108023.aspx

    【讨论】:

      【解决方案2】:

      可能是您在其中的{ 括号。尝试将不围绕要替换的令牌的那些加倍。

      像这样:

      string Placeholder = @"(function({0}, $, undefined) {{ {1} }}( window.{0} = window.{0} || {{}}, jQuery));";
      string output = string.Format(Placeholder, "Value1", "Value2");
      

      【讨论】:

        猜你喜欢
        • 2013-05-24
        • 2011-05-30
        • 1970-01-01
        • 1970-01-01
        • 2011-02-25
        • 2012-01-24
        • 1970-01-01
        相关资源
        最近更新 更多