bevacqua
bevacqua

Reputation: 48486

why does this throw a System.FormatException?

Is this a bug on string.Format or what?

// Act
string l = "This is an UnitTest embedded resource.";
string r = "Please do not remove or change. Sincerely yours, {0}".FormatWith(model.Username);
string expected = "[\r\n\t{0}\r\n\r\n\r\n]\r\n{\r\n\t\r\n\t{1}\r\n\r\n}".FormatWith(l, r);

The FormatWith method is just an extension for syntactic sugar.

public static string FormatWith(this string text, params object[] args)
{
    return string.Format(text, args);
}

Upvotes: 0

Views: 124

Answers (1)

Kendall Frey
Kendall Frey

Reputation: 44326

Your problem is with trying to use { and } in the format string. They need to be escaped or they will be treated as a format variable.

Change the last line to this:

string expected = "[\r\n\t{0}\r\n\r\n\r\n]\r\n{{\r\n\t\r\n\t{1}\r\n\r\n}}".FormatWith(l, r);

Upvotes: 6

Related Questions