Posted by: R Manimaran | April 9, 2011

C#: Using Curly Braces { } in String.Format

While I am working in SharePoint I want a Guid that needs to enclosed with curly braces { }. In C# we use curly braces in the string.Format to pass the arguments value which get appended with the string

Guid myId = Guid.NewGuid();

Console.WriteLine(string.Format("My Guid is {0}",myId));

My output is

My Guid is 748d0b24-6a23-419a-90a2-89543258e4d8

But I like to have the guid enclosed with { }. When I tried something like this

Console.WriteLine(string.Format("My Guid is {{0}}",myId));

My output is

My Guid is {0}

How to escape curly braces{ }. I tried to add \{. But it is not accepting. So how to escape L Add one more curly braces as below

Console.WriteLine(string.Format("My Guid is {{{0}}}",myId));

And I got the required output.

My Guid is {748d0b24-6a23-419a-90a2-89543258e4d8}

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

Follow

Get every new post delivered to your Inbox.

Join 25 other followers

%d bloggers like this: