Escaping curly brackets ({) in string format

In case you wondered, you can escape curly brackets in a string.Format call by just repeating the brackets.  For example, the following:

Console.WriteLine( string.Format( "{{0}}-{0}", 42 ) );

will output:

{0}-42

I needed to do this to use Format to create a string I intended to pass to another Format method.   More examples are in a blog post here.

4 comments: (+add yours?)

Anonymous said...

Aaaarrrggghhh! I wasted an hour on this. Alas. Thanks.

Bugs! said...

Great!!!! Thanks a LOT

Anonymous said...

So... how did you solve it? Reading that blog didn't help much.

Ismail said...

Thank you very much