Multimobile Development: Building Applications for any Smartphone
How do I escape curly braces in a format string?
Messages   Related Types
This message was discovered on microsoft.public.dotnet.languages.csharp.


Tony Chow
....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.

Reply to this message...
Vote that this is a GOOD answer...
 
Really good experience at the Apple Store
MonoDroid – looking *awesome*
 
    
Greg Ewing
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" <Click here to reveal e-mail address> wrote in message
news:e$YchNQACHA.1092@tkmsftngp02...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer...
 
First volume of Multimobile Development nearly ready to go to press
A mention on Developing for the iPhone and Android: The pros and cons
 
    
-glenn-
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" <Click here to reveal e-mail address> wrote in message
news:#pgp1WQACHA.2284@tkmsftngp02...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer...
 
 
    
Greg Ewing
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-" <Click here to reveal e-mail address> wrote in message
news:Org$6LRACHA.1648@tkmsftngp02...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer...
 
First chapters of Multimobile Development book now available on Apress Alpha program
iPad
 
    
-glenn-
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" <Click here to reveal e-mail address> wrote in message
news:e$YchNQACHA.1092@tkmsftngp02...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer...
 
New book project – Multimobile Development: Building Applications for any Smartphone
Dive into HTML5
 
 
System.Console
System.String




Ad
BootFX
Reliable and powerful .NET application framework.
iOS, Android and Windows Phone Development Training and Consultancy
Hosted by RackSRV Communications
 
Multimobile Development: Building Applications for any Smartphone
Copyright © AMX Software Ltd 2008-2010. Portions copyright © Matthew Baxter-Reynolds 2001-2010. All rights reserved.
Contact Us - Terms of Use - Privacy Policy - 4.0.30129.1734