String Interpolation
Build strings by putting values straight into the text with
Novice · C# · Updated
After this lesson you can
- Build a string with interpolation, including format specifiers
Key takeaways
- Put
$before the opening quote, and C# works out anything in curly braces and puts it into the text, as in.quot;({rank}) {teamName}" - The braces can hold any expression, not only a variable; an expression with
?:needs parentheses around it. - A format specifier after a colon sets how a number looks:
F2for 2 decimal places,N0for thousands separators,Cfor currency andP0for a percentage. - A comma and a width line up columns: a positive width right-aligns, a negative width left-aligns.
- To print a curly brace itself, double it:
{{prints{and}}prints}.
So far you've built strings by joining pieces with +. It works, but with several values the quotes and plus signs pile up. String interpolation lets you write the text once, with the values in place.
$ and curly braces
Put $ before the opening quote. Then, inside the string, anything in curly braces is worked out and put into the text:
string teamName = "Georgia";
int rank = 1;
int finalScore = 39;
Console.WriteLine("(" + rank + ") " + teamName + ": " + finalScore);
Console.WriteLine( quot;({rank}) {teamName}: {finalScore}");
(1) Georgia: 39
(1) Georgia: 39
Both lines print the same thing, but the second reads like the output it makes. From now on, this course uses interpolation.
The braces can hold any expression, not only a variable:
int wins = 7;
int losses = 3;
Console.WriteLine( quot;Games played: {wins + losses}");
Console.WriteLine( quot;Winning percentage: {wins * 100 / (wins + losses)}%");
Console.WriteLine( quot;Result: {(wins > losses ? "winning record" : "rebuilding")}");
Games played: 10
Winning percentage: 70%
Result: winning record
An expression with ?: inside the braces needs parentheses around it, because : has a special meaning there.
Formatting numbers
After the value, a colon and a format specifier say how it should look:
| Specifier | Means | Example | Result |
|---|---|---|---|
F2 |
2 decimal places | {3.14159:F2} |
3.14 |
N0 |
thousands separators, no decimals | {1234567:N0} |
1,234,567 |
C |
currency | {12.5m:C} |
$12.50 |
P0 |
a percentage, no decimals | {0.75:P0} |
75% |
decimal price = 12.5m;
double average = 24.5678;
int attendance = 92746;
Console.WriteLine( quot;Price: {price:C}");
Console.WriteLine( quot;Average: {average:F2}");
Console.WriteLine( quot;Attendance: {attendance:N0}");
Price: $12.50
Average: 24.57
Attendance: 92,746
C, N0 and P0 follow your computer's region settings: a computer set up for the United States shows dollars and commas, while one set up for Germany shows 12,50 €.
Lining up columns
A comma and a number set a minimum width: positive pads on the left (right-aligns), negative pads on the right (left-aligns). It's handy for tables:
Console.WriteLine( quot;{"Team",-12}{"Final",6}");
Console.WriteLine( quot;{"Georgia",-12}{39,6}");
Console.WriteLine( quot;{"Ohio State",-12}{52,6}");
Team Final
Georgia 39
Ohio State 52
Braces in the text
To print a curly brace itself, double it: {{ prints {, and }} prints }.
string tag = "p";
Console.WriteLine( quot;CSS: {tag} {{ color: navy; }}");
CSS: p { color: navy; }
Check yourself
2 questions to try on the lesson page.
So what
Interpolation keeps the shape of the output visible in your code, and format specifiers handle money, decimals and columns without any extra arithmetic.