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
  • 43,130
  • 20
  • 110
  • 148
Dante
  • 3,833
  • 4
  • 38
  • 55

3 Answers3

12

Yes. Use two {s, like this:

String.Format("a{{a}}a {0} bbb {1}", "val1", "val2");
Jacob
  • 77,566
  • 24
  • 149
  • 228
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
  • 236,483
  • 35
  • 423
  • 525
3

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

Matthew
  • 24,703
  • 9
  • 76
  • 110