Jonas Bush

Blog.blog.blog.blog
posts - 17 , comments - 188 , trackbacks - 17

Curly braces and string.Format()

Recently I was trying to use StringBuilder.AppendFormat to build some javascript, and was hit with an exception when trying to do this:

sb.AppendFormat("function {0}(args) { return false; }", someVariable);

The problem is that you can't have { or } inside an input string for string.Format(). The solution is actually fairly straightforward:

sb.AppendFormat("function {0}(args) {{ return false; }}", someVariable);

Instead of using "\" as an escape character, you would use { or } (depending on what you want to escape).

Print | posted on Monday, March 5, 2007 2:08 PM | Filed Under [ .Net ]

Feedback

Gravatar

# re: Curly braces and string.Format()

Excellent. I had this problem a few weeks ago but never followed it up, worked around it just concatenating (im bad i know). I'll be sure to revisit it.
3/5/2007 5:06 PM | Luke
Gravatar

# re: Curly braces and string.Format()

Excellent. I had this problem a few weeks ago but never followed it up, worked around it just concatenating (im bad i know). I'll be sure to revisit it.
3/5/2007 5:22 PM | Luke
Gravatar

# re: Curly braces and string.Format()

just repeat the bracket...

sb.AppendFormat("function {0}(args) {{ return false; }}", someVariable);
10/27/2007 8:28 AM | Waqas
Gravatar

# re: Curly braces and string.Format()

This comes up quick on google. Thanks for the tip, it saved me some time.
12/8/2007 7:20 AM | Jon
Gravatar

# re: Curly braces and string.Format()

Great post, surprising that I've never needed to do this before now. Great to have it so quickly and easily answered.
7/14/2008 2:14 PM | Andrew Baird
Gravatar

# re: Curly braces and string.Format()

This is is great! Really saved me some time!
3/26/2009 4:43 PM | David
Gravatar

# re: Curly braces and string.Format()

Very handy when building regex clauses...
Thanks!
10/13/2009 6:18 AM | Holf
Gravatar

# re: Curly braces and string.Format()

thank you very much I had the same problem also generating a javascript serverside
11/20/2009 1:48 AM | Ian
Gravatar

# re: Curly braces and string.Format()

thanks it save time
1/19/2010 1:42 PM | marty
Gravatar

# re: Curly braces and string.Format()

thx a lot... i had the same reason to come here as the previous posters...
3/15/2010 7:16 AM | Frank
Gravatar

# re: Curly braces and string.Format()

This is is great! Really saved me some time!
5/20/2010 3:59 AM | Uli
Gravatar

# re: Curly braces and string.Format()

wow what a pain. why can't it just ignore any curly braces that dont follw the pattern of{X} where X is a number? argh
8/12/2010 6:34 PM | theaters near you
Gravatar

# re: Curly braces and string.Format()

Because you do not always use one pattern: {X}, you can use {X:C} or any other formatting symbols inside the braces.
11/30/2010 3:40 PM | Moutaz Shams
Gravatar

# re: Curly braces and string.Format()

Thanks!, I was having the same problem
6/23/2011 10:59 AM | Frodoq
Gravatar

# re: Curly braces and string.Format()

Thanks - that was helpful
6/14/2012 10:46 AM | Max
Gravatar

# re: Curly braces and string.Format()

Good solution! The error message I received was about wrong date conversion trouble. Very misleading in fact. Because the {} characters have a special meaning in String.Format method, I went looking for something in that direction and immediately found your suggestion / solution. Many thanks!
Peter
10/10/2012 4:45 AM | Peter Klein
Gravatar

# re: Curly braces and string.Format()

Thanks! You just got me out of what could have been hours of pain.
11/7/2012 5:12 AM | David
Post A Comment
Title:
Name:
Email:
Comment:
Verification:
 
 

Powered by: