C # format the character string with the escape braces "{}"

Source: Internet
Author: User

Today, we use C # To write a program to Operate Excel, read the cell content and generate a String according to the required Format, and use the string. format (String Format, object arg0) method. Previously, only "{0}" was known as the index placeholder (that is, the format item), which corresponds to the first object in the parameter list, during the format setting process, each format item is replaced with the text representation of the corresponding object value. However, this time the parameter object must be formatted as a pair of braces, that is, the return string "{object arg0 text representation }".

 

// Directly increase the brackets

String. Format ("{0}", 1); // {0}

 

If the preceding statement is used, the required result "{1}" cannot be obtained because the left braces and the right braces are interpreted as the start and end of the format item, if you want to specify the braces in the format parameter, you must use the escape sequence to display the Left or Right braces of the text. In the formatted string, the escape character is the braces themselves. Therefore, specify two left braces ("{") in the fixed text to display a left braces ("{") or two right braces ("}"). to display a right braces ("}").

 

// A correct method in this example

String. Format ("{0 }}", 1); // {1}

They are interpreted in sequence according to the braces in the format. nested braces cannot be interpreted. Although the above results are correct, the method of interpreting the escape braces may lead to unexpected results. For example, you need to display a left braces, a numeric value formatted as a decimal number, and a format item "{0: D }}}" in the right braces. However, the format item is interpreted as follows:

1. The first two left braces ("{") are escaped to generate a left braces;

2. The subsequent three characters ("{0:") are interpreted as the beginning of the format item.

3. The next character ("D") will be interpreted as a Decimal standard value format specifier, but the two escape braces ("}") below generate a single braces. Because the obtained string ("D}") is not a standard value format specifier, the resulting string is interpreted as a custom format string used to display the string "D.

4. The last braces ("}") are interpreted as the end of the format item.

5. The final result is the string "{D }". The value to be formatted is not displayed.

 

When writing code, one way to avoid false interpretations of escape braces and format items is to format the braces and format items separately. That is to say, the left braces of the text are displayed in the first formatting operation, the results of the format items are displayed in the next operation, and the right braces of the text are displayed in the last operation.

 

// The correct format

String. Format ("{0} {1} {2}", "{", 1, "}"); // {1}

Author tht2009

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.