Dante
Dante

Reputation: 3891

C# String.Format with '{' character

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

Upvotes: 4

Views: 4511

Answers (3)

Jacob
Jacob

Reputation: 78840

Yes. Use two {s, like this:

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

Upvotes: 12

Matthew
Matthew

Reputation: 25763

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

Upvotes: 3

jason
jason

Reputation: 241641

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 "}}".

Upvotes: 12

Related Questions