string.Format выдает исключение - PullRequest
2 голосов
/ 30 июня 2011

У меня есть проблема с 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 'string {System.FormatException}

Есть идеи почему?

Ответы [ 2 ]

9 голосов
/ 30 июня 2011

Это из-за скобок: { {1} } и || {}.Используйте двойные значения:

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

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

2 голосов
/ 30 июня 2011

Вероятно, { скобки у вас там. Попробуйте удвоить те, которые не окружают токен для замены.

Вот так:

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