How can brackets be escaped in using string.Format
.
For example:
String val = \"1,2,3\"
String.Format(\" foo {{0}}\", val);
Yes to output {
in string.Format
you have to escape it like this {{
So this
String val = "1,2,3";
String.Format(" foo {{{0}}}", val);
will output "foo {1,2,3}"
.
BUT you have to know about a design bug in C# which is that by going on the above logic you would assume this below code will print {24.00}
int i = 24;
string str = String.Format("{{{0:N}}}", i); //gives '{N}' instead of {24.00}
But this prints {N}. This is because the way C# parses escape sequences and format characters. To get the desired value in the above case you have to use this instead.
String.Format("{0}{1:N}{2}", "{", i, "}") //evaluates to {24.00}
Reference Articles String.Format gottach and String Formatting FAQ