JSRUN 用代码说话

String(字符串)

编辑教程

string(字符串)

是Dart 中的一组 UTF-16 单元序列。 通过单引号或者双引号创建字符串。

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.";

通过 ${expression} 的方式string字符串可以内嵌表达式。 如果表达式是一个标识符,那么可以省略 {} 。 调用就对象的 toString() 方法可以在 Dart 中得到对象相应的字符串。


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. ' +
        'STRING INTERPOLATION is very handy!');

提示: 两个对象是否相等是用 == 运算符来测试的。如果 在字符串中,两个字符串包含了相同的编码序列,那么这两个字符串相等。 units.

多个字符串连接为一个,可以用 + 运算符来进行,也可以把多个字面量字符串写在一起来实现:

var s1 = 'String '
    'concatenation'
    " works even over line breaks.";
assert(s1 ==
    'String concatenation works even over '
    'line breaks.');
var s2 = 'The + operator ' + 'works, as well.';
assert(s2 == 'The + operator works, as well.');

多行字符串对象的创建是由连续使用三个单引号或者三个双引号来实现:


var s1 = '''
You can create
multi-line strings like this one.
''';
var s2 = """This is also a
multi-line string.""";

“原始 raw” 字符串由 r 前缀创建:


var s = r"In a raw string, even \n isn't special.";

参考 Runes 来了解如何在字符串中表达 Unicode 字符。

如果编译时常量的字面量字符串中有插值表达式,而表达式内容也是编译时常量, 那么这个字符串依旧是编译时常量。 插入的常量值类型可以是 null,数值,字符串或布尔值。


// const 类型数据
const aConstNum = 0;
const aConstBool = true;
const aConstString = 'a constant string';
// 非 const 类型数据
var aNum = 0;
var aBool = true;
var aString = 'a string';
const aConstList = [1, 2, 3];
const validConstString = '$aConstNum $aConstBool $aConstString'; //const 类型数据
// const invalidConstString = '$aNum $aBool $aString $aConstList'; //非 const 类型数据
JSRUN闪电教程系统是国内最先开创的教程维护系统, 所有工程师都可以参与共同维护的闪电教程,让知识的积累变得统一完整、自成体系。 大家可以一起参与进共编,让零散的知识点帮助更多的人。
X
支付宝
9.99
无法付款,请点击这里
金额: 0
备注:
转账时请填写正确的金额和备注信息,到账由人工处理,可能需要较长时间
如有疑问请联系QQ:565830900
正在生成二维码, 此过程可能需要15秒钟