You can put the value of an expression inside a string by usingexpression
}
. If the expression is an identifier, you can skipthe {}. To get the string corresponding to an object, Dart calls theobject’s toString()
method.
var s = 'string interpolation';
assert('Dart has $s, which is very handy.' ==
'Dart has string interpolation, ' +
'which is very handy.');
assert('That deserves all caps. ' +
'${s.toUpperCase()} is very handy!' ==
'That deserves all caps. ' +
Note: The ==
operator tests whether two objects are equivalent. Two strings are equivalent if they contain the same sequence of code units.
Another way to create a multi-line string: use a triple quote witheither single or double quotation marks:
You can create
multi-line strings like this one.
''';
var s2 = """This is also a
multi-line string.""";
You can create a “raw” string by prefixing it with r
:
Literal strings are compile-time constants,as long as any interpolated expression is a compile-time constantthat evaluates to null or a numeric, string, or boolean value.
// These work in a const string.
const aConstBool = true;
// These do NOT work in a const string.
var aNum = 0;
var aBool = true;
var aString = 'a string';
const aConstList = [1, 2, 3];
const validConstString = '$aConstNum $aConstBool $aConstString';
// const invalidConstString = '$aNum $aBool $aString $aConstList';
For more information on using strings, see.