How to Use the Ternary Operator Inside an Interpolated String

How to use the ternary operator inside an interpolated string?

According to the documentation:

The structure of an interpolated string is as follows:

{ <interpolationExpression>[,<alignment>][:<formatString>] }

The problem is that the colon is used to denote formatting, like:

Console.WriteLine($"The current hour is {hours:hh}")

The solution is to wrap the conditional in parenthesis:

var result = $"Descending {(isDescending ? "yes" : "no")}";

How to use conditional operator with string interpolation?

You need to put parenthesis around the expression like so:

string body = $"Dear {(user.Gender ? "Mr." : "Mrs.")} {user.First_name}...";

The reason for that is the colon which signals a format string for something.

Ternary conditional operator in interpolated strings in F#

You can use any valid F# expression when using string interpolation, including the if expression. Just use the standard F# way of writing it if <boo> then <e1> else <e2>:

let boolFlag = true
let innerString = "Yo"

printfn $"""This is a string{
if boolFlag then $", and here's the inner string: {innerString}!"
else "!"}\n"""

C# interpolated string with conditional-operator

You need to put the string in parentheses within {}, so: {(1 == 1 ? "yes" : "no")}.

How can I use a operator ternary in a string?

When you enclose a string with grave marks (`), it allows for interpolated expression over string concatenation, called a template literal which is new in ES2015. Your ternary operator is an expression, so you can use ${expr} notation in string interpolation to interpolate an expression. Since you're already using template literals, there's no reason to do concatenation, thus:

var res = `<li id="thumbnail-${key}">
<div class="thumbnail ${product.photo == photoList[key].id ? 'thumbnail-main' : ''}>
...
</div>
</li>`;

Anywhere you want to use string concatenation of an expression, you can just use interpolation. So instead of:

'1 + 1 = ' + (1 + 1)

You can do:

`1 + 1 = ${1 + 1}`

Because 1 + 1 is an expression.

Why can't I use the conditional operator in an interpolated string without brackets?

From MSDN (emphasis mine):

$"{person.Name, 20} is {person.Age:D3} year {(p.Age == 1 ? "" : "s")} old."

You do not need to quote the quotation characters within the contained interpolation expressions because interpolated string expressions start with $, and the compiler scans the contained interpolation expressions as balanced text until it finds a comma, colon, or close curly brace. For the same reasons, the last example uses parentheses to allow the conditional expression (p.Age == 1 ? "" : "s") to be inside the interpolation expression without the colon starting a format specification. Outside of the contained interpolation expression (but still within the interpolated string expression) you escape quotation characters as you normally would.

Without the parentheses, the parser is treating the portion after the colon as a format specifier instead (compare the {person.Age:D3} portion of the example above).



Related Topics



Leave a reply



Submit