Imagine you’re writing a letter to your friend, and you want to include their name in it. Instead of writing:
“Hello, [friend’s name]!”
Wouldn’t it be great if you could just tell Dart to fill in the name for you? That’s exactly what string interpolation does!
String interpolation is a fancy way of saying “put variables inside a string.” Instead of using long and messy ways to combine text, Dart lets us insert values directly into a string in a simple and clean way.
In this article, we’ll explore how to use string interpolation to make working with text easier.
Basic String Interpolation
The easiest way to insert a variable into a string is by using the $
symbol.
Example: Adding a Name and Age to a String
void main() {
String name = "Edward";
int age = 20;
print("Hello, my name is $name and I am $age years old.");
}
What’s Happening Here?
- Dart sees the
$name
and replaces it with “Edward”. - It sees
$age
and replaces it with 20.
This is much cleaner than writing something like:
print("Hello, my name is " + name + " and I am " + age.toString() + " years old.");
Interpolation helps avoid using extra plus signs (+) and conversions, making the code easier to read.
Using Expressions in Interpolation
What if we need to do some math inside a string? That’s where ${}
comes in!
If you need to perform calculations or call functions inside a string, wrap the expression inside ${}
.
Example: Doing Math Inside a String
void main() {
int apples = 3;
int bananas = 5;
print("I have ${apples + bananas} fruits in total.");
}
Wha’s Happening Here?
- Dart sees
${apples + bananas}
and calculates 3 + 5 = 8. - It replaces the expression with 8, so the output is:
I have 8 fruits in total.
You can also call functions inside interpolation!
String upperCaseName(String name) {
return name.toUpperCase();
}
void main() {
String name = "Edward";
print("My name in uppercase is ${upperCaseName(name)}.");
}
Dart replaces ${upperCaseName(name)}
with EDWARD!
Handling Special Cases
Escaping Dollar Signs (\$
)
What if you actually want to show a dollar sign ($
) in your string, like for money? You can use a backslash (\
) before the dollar sign (\$
).
void main() {
int price = 50;
print("This book costs \$${price}.");
}
Without the backslash, Dart might think $price
is a variable!
Multiline String Interpolation
Dart also allows multiline strings using triple quotes ("""
or '''
). This is useful when formatting long text with interpolation.
void main() {
String name = "Edward";
int age = 20;
String introduction = """Hello!
My name is $name.
I am $age years old.""";
print(introduction);
}
This makes text more readable instead of using multiple print
statements.
Conclusion
You now understand how to use string interpolation in Dart! You’ve learned:
- How to insert variables into a string using
$variable
. - How to use
${expression}
for calculations and function calls. - How to handle special cases like escaping
$
and working with multiline strings.
String interpolation makes your code more readable and efficient. Try using it in your own programs to simplify text handling and improve clarity!