Sam
Sam

Reputation: 943

String parameter placeholder

        string str = "({{0}})";
        int i = 0;
        string str2 = string.Format(str, i++);
        string str3 = string.Format(str, i++);

why is str3 ({0}) instead of ({1})?

Upvotes: 3

Views: 951

Answers (1)

Mark Byers
Mark Byers

Reputation: 838716

You escaped the curly braces so they have no special meaning. From the documentation:

To specify a single literal brace character in format, specify two leading or trailing brace characters; that is, "{{" or "}}".

You can simplify your program and still demonstrate the problem:

Console.WriteLine("{{0}}", 1);

Output:

{0}

See it working online: ideone


To get the desired output you need to use {{ followed by {0} and then finally }}:

string str = "({{{0}}})";

Upvotes: 5

Related Questions