Advertisements
Home > ASP.NET > Escaping curly brackets ({) in string format

Escaping curly brackets ({) in 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).

Advertisements
Categories: ASP.NET
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: