Skip to main content

Concatenation & interpolation

Sometimes we have two strings that we want to join together. For this, we can use concatenation.

Or maybe we want to insert a value in the middle of the string. To do it, we can use interpolation.

Concatenation

To concatenate two or more strings, we have to use the + operator like this:

var variable1 = 'Hello';
var variable2 = 'world';

print( variable1 + variable2 );

The previous code will print Helloworld because there is no space between the two variables. We can improve the previous code to print Hello world like this:

print( variable1 + ' ' + variable2 );

Interpolation

String interpolation helps us to insert variables inside the string. To interpolate variables, we have to use the $ operator like this:

var variable1 = 'Hello';
var variable2 = 'world';

print( '$variable1 $variable2' );

The previous code will print Hello world because we already have a space between the two variables.

If we interpolate a string with another data type, this will be automatically converted to a string, for example:

var variable3 = 'The number is:';
var variable4 = 100;

print( '$variable3 $variable4' );

You can run all the previous examples in DartPad: