I have stumbled upon one issue with interpolated strings for a several times now.
Consider the following case:
double number = 123.4567;
var str = $"{{{number:F2}}}"; //I want to get "{123.45}"
Console.WriteLine(str); // Will print "{F2}"
A little surprising at first but once you realize how the curly brackets are paired it makes sense. Two following curly brackets are an escape sequence for a single curly in the interpolated string. So the opening bracket of the interpolated expression is paired with the last curly in the string.
___pair____
| |
$"{{{number:F2}}}";
Now you could do the following to break the escape sequence:
var str = $"{{{number:F2} }}"; // This will be "{123.45 }"
Notice the space character this approach adds to the output. (Not ideal)
My question:
Lets say I want to use a single interpolated string to get exactly the output "{123.45}"
Is this at all possible without doing something hackish like the following?
var s = $"{{{number:F2}{'}'}";
Format strings contain “replacement fields” surrounded by curly braces {} . Anything that is not contained in braces is considered literal text, which is copied unchanged to the output. If you need to include a brace character in the literal text, it can be escaped by doubling: {{ and }} .
format string parser that the given curly braces must be escaped and considered as a part of the given text literal. This can be easily done by doubling the curly braces encompassing the string, that is using the following syntax: {{Serial No.}} What is this?
Use "{{ '{' }}") to escape it.)
String n = s. replaceAll("/{", " "); String n = s. replaceAll("'{'", " ");
This is an expected behavior of string interpolation. It is mentioned at this Microsoft document. The below content is from Microsoft link only.
Opening and closing braces are interpreted as starting and ending a format item. Consequently, you must use an escape sequence to display a literal opening brace or closing brace. Specify two opening braces ("{{")
in the fixed text to display one opening brace ("{")
, or two closing braces ("}}")
to display one closing brace ("}")
. Braces in a format item are interpreted sequentially in the order they are encountered. Interpreting nested braces is not supported.
The way escaped braces are interpreted can lead to unexpected results. For example, consider the format item "{{{0:D}}}", which is intended to display an opening brace, a numeric value formatted as a decimal number, and a closing brace. However, the format item is actually interpreted in the following manner:
("{{")
are escaped and yield one
opening brace.("{0:")
are interpreted as the start of a
format item.("D")
would be interpreted as the Decimal standard
numeric format specifier, but the next two escaped braces ("}}")
yield a single brace. Because the resulting string ("D}")
is not a
standard numeric format specifier, the resulting string is
interpreted as a custom format string that means display the literal
string "D}"
.("}")
is interpreted as the end of the format item."{D}"
. The
numeric value that was to be formatted is not displayed.One way to write your code to avoid misinterpreting escaped braces and format items is to format the braces and format item separately. That is, in the first format operation display a literal opening brace, in the next operation display the result of the format item, then in the final operation display a literal closing brace. The following example illustrates this approach.
int value = 6324;
string output = string.Format("{0}{1:D}{2}",
"{", value, "}");
Console.WriteLine(output);
// The example displays the following output:
// {6324}
Assuming that it is not required to use a named format string, you can use:
var s = $"{{{number:#.#0}}}";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With