{ in a string.Format

  • Thread starter Thread starter Heinz Kiosk
  • Start date Start date
H

Heinz Kiosk

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
 
Heinz Kiosk said:
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(); }
 
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
 
Heinz Kiosk said:
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".
 
Back
Top