Dart:
Interpolating a string
How to:
In Dart, string interpolation is straightforward, utilizing the $
symbol to interpolate expressions directly within string literals:
void main() {
String name = 'Dart';
int year = 2023;
// Simple variable interpolation
print('Learning $name in $year!');
// Output: Learning Dart in 2023!
// Interpolating expressions
print('In two years, it will be ${year + 2}.');
// Output: In two years, it will be 2025.
}
In the case where you have more complex expressions or want to perform operations within the string itself, enclose the expression in ${}
. Dart doesn’t have any popular third-party libraries specifically for string interpolation as it’s well equipped natively to handle varied and complex scenarios.