{ in a string.Format

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
 
J

Jon Skeet [C# MVP]

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(); }
 
H

Heinz Kiosk

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
 
J

Jon Skeet [C# MVP]

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".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top