Geeks With Blogs
Jonas Bush Blog.blog.blog.blog

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).

Posted on Monday, March 5, 2007 2:08 PM .Net | Back to top


Comments on this post: Curly braces and string.Format()

# re: Curly braces and string.Format()
Requesting Gravatar...
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.
Left by Luke on Mar 05, 2007 5:06 PM

# re: Curly braces and string.Format()
Requesting Gravatar...
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.
Left by Luke on Mar 05, 2007 5:22 PM

# re: Curly braces and string.Format()
Requesting Gravatar...
just repeat the bracket...

sb.AppendFormat("function {0}(args) {{ return false; }}", someVariable);
Left by Waqas on Oct 27, 2007 8:28 AM

# re: Curly braces and string.Format()
Requesting Gravatar...
This comes up quick on google. Thanks for the tip, it saved me some time.
Left by Jon on Dec 08, 2007 7:20 AM

# re: Curly braces and string.Format()
Requesting Gravatar...
Great post, surprising that I've never needed to do this before now. Great to have it so quickly and easily answered.
Left by Andrew Baird on Jul 14, 2008 2:14 PM

# re: Curly braces and string.Format()
Requesting Gravatar...
This is is great! Really saved me some time!
Left by David on Mar 26, 2009 4:43 PM

# re: Curly braces and string.Format()
Requesting Gravatar...
Very handy when building regex clauses...
Thanks!
Left by Holf on Oct 13, 2009 6:18 AM

# re: Curly braces and string.Format()
Requesting Gravatar...
thank you very much I had the same problem also generating a javascript serverside
Left by Ian on Nov 20, 2009 1:48 AM

# re: Curly braces and string.Format()
Requesting Gravatar...
thanks it save time
Left by marty on Jan 19, 2010 1:42 PM

# re: Curly braces and string.Format()
Requesting Gravatar...
thx a lot... i had the same reason to come here as the previous posters...
Left by Frank on Mar 15, 2010 7:16 AM

# re: Curly braces and string.Format()
Requesting Gravatar...
This is is great! Really saved me some time!
Left by Uli on May 20, 2010 3:59 AM

# re: Curly braces and string.Format()
Requesting Gravatar...
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
Left by theaters near you on Aug 12, 2010 6:34 PM

# re: Curly braces and string.Format()
Requesting Gravatar...
Because you do not always use one pattern: {X}, you can use {X:C} or any other formatting symbols inside the braces.
Left by Moutaz Shams on Nov 30, 2010 3:40 PM

# re: Curly braces and string.Format()
Requesting Gravatar...
Thanks!, I was having the same problem
Left by Frodoq on Jun 23, 2011 10:59 AM

# re: Curly braces and string.Format()
Requesting Gravatar...
Thanks - that was helpful
Left by Max on Jun 14, 2012 10:46 AM

# re: Curly braces and string.Format()
Requesting Gravatar...
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
Left by Peter Klein on Oct 10, 2012 4:45 AM

# re: Curly braces and string.Format()
Requesting Gravatar...
Thanks! You just got me out of what could have been hours of pain.
Left by David on Nov 07, 2012 5:12 AM

Your comment:
 (will show your gravatar)


Copyright © Jonas Bush | Powered by: GeeksWithBlogs.net | Join free