How do you escape a { in a format string for string.Format?

I want to have a format string that builds code, something like "static void
{0}() { {1} }". I know there are other solutions like building the {1} with
the brackets.

Tom

Re: { in a string.Format by Jon

Jon
Wed Dec 17 03:32:21 CST 2003

Heinz Kiosk <removethis.tom.mcclelland@ntlworld.com> wrote:
> How do you escape a { in a format string for string.Format?
>
> I want to have a format string that builds code, something like "static void
> {0}() { {1} }". I know there are other solutions like building the {1} with
> the brackets.

You need to just double the brace. For instance:

Console.WriteLine ("static void {0} {{ {1} }}",
"Foo", "Bar();");

produces:

static void Foo { Bar(); }

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Re: { in a string.Format by Heinz

Heinz
Wed Dec 17 09:13:49 CST 2003

TYVM, I thought it would be something like this but I didn't want to try all
the possibilities and I couldn't find it in the online documentation.

Tom

"Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
news:MPG.1a4a3303a522b456989c55@msnews.microsoft.com...
> Heinz Kiosk <removethis.tom.mcclelland@ntlworld.com> wrote:
> > How do you escape a { in a format string for string.Format?
> >
> > I want to have a format string that builds code, something like "static
void
> > {0}() { {1} }". I know there are other solutions like building the {1}
with
> > the brackets.
>
> You need to just double the brace. For instance:
>
> Console.WriteLine ("static void {0} {{ {1} }}",
> "Foo", "Bar();");
>
> produces:
>
> static void Foo { Bar(); }
>
> --
> Jon Skeet - <skeet@pobox.com>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too



Re: { in a string.Format by Jon

Jon
Wed Dec 17 10:04:08 CST 2003

Heinz Kiosk <removethis.tom.mcclelland@ntlworld.com> wrote:
> TYVM, I thought it would be something like this but I didn't want to try all
> the possibilities and I couldn't find it in the online documentation.

For reference, the bit of documentation I found it in was "Composite
Formatting" in the paragraph titled "Format Item Syntax".

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too