C # escape curly bracket not working with format modifier?

I know that with {{ and }} we can avoid curly braces in C #. But they do not work well if they are right after the format modifier (for example, {0:F6} ).

 string str; // Prints "{3.14}" as expected str = string.Format("{{{0}}}", 3.14); Console.WriteLine(str); // Expected "{3.140000}", found "{F6}" str = string.Format("{{{0:F6}}}", 3.14); Console.WriteLine(str); 
+5
source share
2 answers

This is how C # handles curly braces, this is well known. Look here

You can avoid this (for example, there are different ways):

  var str = string.Format("{0}{1:F6}{2}", "{", 3.14, "}"); Console.WriteLine(str); 
+6
source

Try the following:

  String.Format("{0}{1:F6}{2}", "{",3.14, "}") 
+1
source

Source: https://habr.com/ru/post/1237866/


All Articles