How do I interpolate strings?

This has been added as of C# 6.0 (Visual Studio 2015+). Example: var planetName = “Bob”; var myName = “Ford”; var formattedStr = $”Hello planet {planetName}, my name is {myName}!”; // formattedStr should be “Hello planet Bob, my name is Ford!” This is syntactic sugar for: var formattedStr = String.Format(“Hello planet {0}, my name is … Read more

String concatenation vs. interpolation in Ruby

Whenever TIMTOWTDI (there is more than one way to do it), you should look for the pros and cons. Using “string interpolation” (the second) instead of “string concatenation” (the first): Pros: Is less typing Automatically calls to_s for you More idiomatic within the Ruby community Faster to accomplish during runtime Cons: Automatically calls to_s for … Read more

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”)}”;