4

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

2
  • 1
    Warning: "val2" is never used. Commented Apr 16, 2012 at 20:34
  • It was a typo, I meant to write {1}. Commented Apr 16, 2012 at 20:37

3 Answers 3

12

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

Sign up to request clarification or add additional context in comments.

2 Comments

{{{? What do you mean? The escape is {{.
@Kendall Frey: It was a typo.
12

Yes. Use two {s, like this:

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

Comments

3

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.