3

Can we do a String.Format in a string that contains the '{' character?

Example: String.Format("a{a}a {0} bbb {1}", "val1", "val2");

The a{a}a should be interpreted as part of the string, not as a formatter...

Thanks in advance

Kendall Frey
  • 41,292
  • 18
  • 105
  • 145
Dante
  • 3,707
  • 4
  • 35
  • 54

3 Answers3

12

Yes. Use two {s, like this:

String.Format("a{{a}}a {0} bbb {1}", "val1", "val2");
Jacob
  • 75,331
  • 23
  • 142
  • 223
11

Use: {{. By the way, this is answerable from the documentation:

To specify a single literal brace character in format, specify two leading or trailing brace characters; that is, "{{" or "}}".

jason
  • 228,647
  • 33
  • 413
  • 517
3

You should escape { and } with {{ and }}

Matthew
  • 23,095
  • 7
  • 70
  • 102