In this article, we continue to explore string manipulations by focusing on interpolation.
Instead of concatenating many pieces together, interpolation allows us to insert special tokens inside a string.
A value then replaces those tokens.
Interpolation and concatenation play the same role, but often one ends up being more elegant than the other and makes the code easier to maintain.
This article is part of a learn programming series where you need no prior knowledge of programming.
If you want to learn how to program and want to learn it using .NET/C#, this is the right place.
I suggest reading the whole series in order, starting with Creating your first .NET/C# program, but that's not mandatory.
This article is part of a sub-series, starting with Introduction to string concatenation.
It is not mandatory to read all articles in order, but I strongly recommend it, especially if you are a beginner.
If you are already reading the whole series in order, please discard this word of advice.
Interpolation
The definition of interpolation, from Oxford Languages (Google search), is:
The insertion of something of a different nature into something else.
In C#, we must prefix the string with the $
character if we want to use interpolation.
Then, we can insert the value of an expression in that string by wrapping it with {
and }
, like this:
var name = "Joe";
var result = $"Hello {name}!";
Console.WriteLine(result);
Executing the preceding code should write Hello Joe!
in the terminal.
I find $"Hello {name}!"
to be more elegant than "Hello " + name + "!"
and way easier to read, especially for a neophyte.
Next, we explore how to use interpolation in a multiline string.
Multiline string interpolation
If you wondered why I talked about multiline strings in the previous article, I planned this section.
We can use both $
and @
to mix interpolation and multiline string, like this:
var name = "Joe";
var result = $@"Hello {name}!
What's up?";
Console.WriteLine(result);
In the preceding code, we used interpolation in a multiline string by chaining both $
and @
(in this order).
As easy as that!
Note: the order is important. If you inverse the symbols order (
@$"..."
instead of$@"..."
), the code will not compile.
Next, it is your turn to try it out!
Exercise
To practice interpolation, we will replace concatenation with interpolation in the code from the previous article's exercise.
Here is the previous solution as a reference:
using System;
Console.Title = "IntroToDotNet";
Console.Write("What is your first name? ");
var firstName = Console.ReadLine();
Console.Clear();
Console.Write("What is your last name? ");
var lastName = Console.ReadLine();
Console.Clear();
var greetings = "Greetings " + firstName + " " + lastName + "!";
Console.WriteLine(greetings);
Unfortunately, I was not able to recreate the exercise on this platform, so please look at the exercise on the original post on my blog. I'm sorry for the inconvenience.
Conclusion
In this article, we explored interpolation as a way to replace concatenation in certain scenarios.
At this point, using one or the other is only a matter of taste.
To use interpolation, we need to prefix a string with $
.
Inside that string, we can then wrap an expression, like a variable, with {
and }
.
The program will replace that token at runtime with the expression's value.
We also saw that we could use interpolation with multiline strings by prefixing the string with both special characters, like this: $@"..."
.
Next step
It is now time to move to the next article: Escaping characters in C# strings which is coming soon. Stay tuned by following me on dev.to, Twitter, or other places you can find me.
You can look at my blog contact page for more info.
Top comments (0)