amateur
amateur

Reputation: 44605

string.Format throwing exception

I have an issue with String.Format that I need assistance with:

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

The following exception is thorwn at String.Format

'string.Format(Placeholder, "Value1", "Value2")' threw an exception of type 'System.FormatException' string {System.FormatException}

Any idea why?

Upvotes: 2

Views: 1522

Answers (2)

Ry-
Ry-

Reputation: 224857

It's because of the braces: { {1} } and || {}. Use doubles:

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

Upvotes: 9

Abe Miessler
Abe Miessler

Reputation: 85046

Probably the { brackets you have in there. Try doubling up the ones that don't surround a token to be replaced.

Like so:

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

Upvotes: 2

Related Questions