Jonas Bush

Blog.blog.blog.blog
posts - 16, comments - 161, trackbacks - 17

My Links

News

Archives

Post Categories

Image Galleries

General

Monday, March 05, 2007

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

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Posted On Monday, March 05, 2007 2:08 PM | Feedback (14) | Filed Under [ .Net ]

Powered by: