void main() { var s1 = 'Single quotes work well for string literals.'; var s2 = "Double quotes work just as well."; var s3 = 'It\'s easy to escape the string delimiter.'; var s4 = "It's even easier to use the other delimiter."; }
void main() { var s = 'string interpolation'; print('Dart has $s, which is very handy.'); //打印结果:Dart has string interpolation, which is very handy. print('That deserves all caps. ' + '${s.toUpperCase()} is very handy!');//打印结果:That deserves all caps. STRING INTERPOLATION is very handy! }
字符串连接
可以使用相邻的字符串文字或+运算符来连接字符串
1 2 3 4 5 6 7 8 9 10 11
void main() { var s1 = 'String ''concatenation'" works even over line breaks."; print(s1); //打印结果:String concatenation works even over line breaks.
var s2 = 'The + operator ' + 'works, as well.'; print(s2); //打印结果:The + operator works, as well. var s3 = 'hello' ' world' '!'; print(s3); //打印结果:hello world! }
多行字符串
使用带有单引号或双引号的三引号创建多行字符串:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
void main() { var s1 = ''' You can create multi-line strings like this one. '''; print(s1); /*打印结果 You can create multi-line strings like this one. */ var s2 = """This is also a multi-line string."""; print(s2); /*打印结果 This is also a multi-line string. */ }
原始字符串(r前缀)
可以通过在前面加上r来创建原始字符串:
1 2 3 4
void main() { var s = r'In a raw string, not even \n gets special treatment.'; print(s);//打印结果:In a raw string, not even \n gets special treatment. }