繁体   English   中英

string.Format 抛出异常

[英]string.Format throwing exception

我对 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}

知道为什么吗?

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

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

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

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

像这样:

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

暂无
暂无

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

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