Friday, 11 February 2011

Escaping a curly bracket in string.format

This doesn't work because the curly brackets (or braces) aren't escaped:
newStr = string.Format("{Hello {0}, in curly braces}", yourName);
Do we need to escape the brackets with a backslash like this? No, it doesn't work:
newStr = string.Format("\{Hello {0}, in curly braces\}", yourName);
Seems strange, but you need to escape your curly brackets with another one like, this:
newStr = string.Format("{{Hello {0}, in curly braces}}", yourName);

No comments:

Post a Comment

Comments are moderated, so you'll have to wait a little bit before they appear!