Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How do I escape curly braces in a format string?

0 views
Skip to first unread message

Tony Chow

unread,
May 21, 2002, 4:09:12 PM5/21/02
to
...since curly braces already delimit format specifiers.

Say if I want to compose the following client-side javascript with a dynamic
value in it:

function IsThisAlright(value)
{
return value != dynamicnumber;
}

... in which "dynamicnumber" is generated on the server side at runtime.

If I use the String.Format() to construct this script and inserting the
dynamic value, how do I escape the curly braces?

This is driving me nuts. I can't local this elementary bit of information
in the documentation!

Please help. Thanks in advance.


-glenn-

unread,
May 21, 2002, 4:21:57 PM5/21/02
to
Tony, I couldn't find it in the docs either (read it here about a week ago).
I think what you want is to use two braces wherever you need one. For
example, if your format string is:

"{{ <{0}> }}"

And you pass "hi" as the first parameter, it will come out as:

{ <hi> }

-glenn-

"Tony Chow" <ever...@ucla.edu> wrote in message
news:e$YchNQACHA.1092@tkmsftngp02...

Greg Ewing

unread,
May 21, 2002, 4:25:40 PM5/21/02
to
Tony, you can escape the { and } with a \.

So, you would use Console.WriteLine("This is a curly bracket \{"); to print
a left curly.

In your VS.NET IDE help you can find more info at:

ms-help://MS.VSCC/MS.MSDNVS/cpgenref/html/xsdrefsinglecharacterescape.htm

--
Greg
http://www.claritycon.com/

"Tony Chow" <ever...@ucla.edu> wrote in message
news:e$YchNQACHA.1092@tkmsftngp02...

-glenn-

unread,
May 21, 2002, 6:00:52 PM5/21/02
to
Greg, that doesn't seem to work with String.Format or Console.WriteLine.

The help section you reference is for an XML Schema Regular Expression where
I think you can escape curly braces as you can with "regular" Regular
Expressions.

-glenn-

"Greg Ewing" <gewing@_NO_SPAM_claritycon.com> wrote in message
news:#pgp1WQACHA.2284@tkmsftngp02...

Greg Ewing

unread,
May 21, 2002, 6:09:28 PM5/21/02
to
glenn, my bad, I wrote the code off the top of my head assuming it would
work for Format and WriteLine. Sorry about that. You're {{ }} solution
definitely works though.

--
Greg
http://www.claritycon.com/

"-glenn-" <som...@example.com> wrote in message
news:Org$6LRACHA.1648@tkmsftngp02...

0 new messages