by @CodingExample

How to escape curly braces in String.format C#

Home » c sharp » How to escape curly braces in String.format C#

To escape curly braces and interpolate a string inside the String.format() method use triple curly braces {{{ }}}.

Here is an example:

using System;

class EscapeDemo {

  static void Main() {
      string val = "1, 2, 3";
      string output =String.Format(" My values are {{{0}}}", val);      Console.WriteLine(output);
  }
}

Output:

 My values are {1, 2, 3}

Similarly, you can also use the c# string interpolation feature instead of String.format().

using System;

class EscapeDemo {

  static void Main() {
      string val = "1, 2, 3";
      string output = $" My values are {{{val}}}";      Console.WriteLine(output);
  }
}