How can brackets be escaped in a C# format string so, something like :
String val = "1,2,3"
String.Format(" foo {{0}}", val);
doesn't throw a parse exception but actually outputs the string " foo {1,2,3}"
Is there a way to escape the brackets or should a workaround be used.
Source: Tips4all
For you to output foo {1, 2, 3} you have to do something like:
ReplyDeletestring t = "1, 2, 3";
string v = String.Format(" foo {{{0}}}", t); ;
To put a { you use {{ and to put a } you use }}.
Almost there! The escape sequence for a brace is {{ or }} so for your example you would use:
ReplyDeletestring t = "1, 2, 3";
string v = String.Format(" foo {{{0}}}", t);
You can use double open brackets and double closing brackets which will only show one bracket on your page.
ReplyDelete