简体   繁体   中英

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

Yes. Use two { s, like this:

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

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

你应该用{{和}}来逃避{和}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM