Dart 字符串
最后修改日期:2024 年 1 月 28 日
Dart 字符串教程演示了如何在 Dart 语言中操作字符串。
Dart 字符串
字符串是 UTF-16 码单元的序列。它用于在程序中表示文本。一个字符可能由多个码点表示。每个码点可以有一个或两个码单元。
Dart 中的字符串是不可变的。存在像 toLowerCase 或 split 这样的方法,它们会返回一个修改后的字符串,但原始字符串保持不变。
代码点 (rune) 是表示 Unicode 码点的整数。字符串的 runes 属性返回其代码点。
Dart 中的字符串是一个对象。我们可以对字符串对象调用几种方法。
Dart 简单字符串
下面是一个带有字符串的简单 Dart 程序。
void main() {
var text = "There are six falcons";
print(text);
var len = text.length;
print('The string has ' + len.toString() + ' characters');
var word = 'falcon ';
print(word * 3);
}
字符串字面量是用单引号或双引号分隔的。
var len = text.length;
length 属性返回字符串的长度,即字符串中 UTF-16 码单元的数量。
print('The string has ' + len.toString() + ' characters');
字符串可以用 + 运算符连接。
var word = 'falcon '; print(word * 3);
* 运算符用于字符串的乘法(重复)。
$ dart main.dart There are six falcons The string has 21 characters falcon falcon falcon
Dart 字符串分隔符
字符串是用单引号或双引号分隔的。引号可以嵌套,以便在需要时创建引号或直接引语。
void main() {
var text1 = "old falcon";
var text2 = 'new book';
var text3 = "This was a 'great movie'";
var text4 = 'He said: "I saw an old man in the park.';
print(text1);
print(text2);
print(text3);
print(text4);
}
示例展示了 Dart 中的字符串分隔符。
$ dart main.dart old falcon new book This was a 'great movie' He said: "I saw an old man in the park.
Dart 字符串长度
length 属性返回字符串的码单元长度。
void main() {
var word = 'čerešňa';
print(word.length);
print(word.codeUnits);
print(word.runes);
var word2 = "合気道";
print(word2.length);
print(word2.codeUnits);
print(word2.runes);
}
在示例中,我们有两个包含宽字符的字符串。length 属性返回可见字符的数量。codeUnits 属性返回此字符串的 UTF-16 码单元的不可修改列表。runes 属性返回字符串的 Unicode 码点迭代器。
$ dart main.dart 7 [269, 101, 114, 101, 353, 328, 97] (269, 101, 114, 101, 353, 328, 97) 3 [21512, 27671, 36947] (21512, 27671, 36947)
Dart 多行字符串
多行字符串是用三重单引号或双引号创建的。
void main() {
var sonnet55 = '''
Not marble nor the gilded monuments
Of princes shall outlive this powerful rhyme,
But you shall shine more bright in these contents
Than unswept stone besmeared with sluttish time.
When wasteful war shall statues overturn,
And broils root out the work of masonry,
Nor Mars his sword nor war's quick fire shall burn
The living record of your memory.
'Gainst death and all-oblivious enmity
Shall you pace forth; your praise shall still find room
Even in the eyes of all posterity
That wear this world out to the ending doom.
So, till the Judgement that yourself arise,
You live in this, and dwell in lovers' eyes.''';
print(sonnet55);
}
示例打印了一个用三重单引号创建的诗句。
$ dart main.dart Not marble nor the gilded monuments Of princes shall outlive this powerful rhyme, But you shall shine more bright in these contents Than unswept stone besmeared with sluttish time. When wasteful war shall statues overturn, And broils root out the work of masonry, Nor Mars his sword nor war's quick fire shall burn The living record of your memory. 'Gainst death and all-oblivious enmity Shall you pace forth; your praise shall still find room Even in the eyes of all posterity That wear this world out to the ending doom. So, till the Judgement that yourself arise, You live in this, and dwell in lovers' eyes.
Dart 字符串是不可变的
字符串无法被修改。我们可以调用返回修改后字符串的方法。
void main() {
var text = "a red fox";
var parts = text.split(' ');
print(parts);
print(text);
}
使用 split 方法,我们根据模式匹配分割字符串,并返回子字符串列表。原始字符串保持不变。
$ dart main.dart [a, red, fox] a red fox
Dart 字符串转义序列
转义序列是特殊字符,在字符串字面量中使用时具有特定含义。例如,换行符 \n 转义序列会导致下一个字符出现在新行上。
void main() {
var text1 = 'a red \n fox';
var text2 = 'a red \t fox';
var text3 = 'a red \b\b\b\b\b fox';
var text4 = 'a red fox\ra brown fox';
print(text1);
print(text2);
print(text3);
print(text4);
}
在示例中,我们使用了一些转义序列。
var text1 = 'a red \n fox';
\n 添加换行符。
var text2 = 'a red \t fox';
\t 插入一个制表符。
var text3 = 'a red \b\b\b\b\b fox';
\b 删除前一个字符。
var text4 = 'a red fox\ra brown fox';
\r 返回到字符串的开头。
$ dart main.dart a red fox a red fox a fox a brown fox
Dart 原生字符串
在原生字符串中,转义字符不会被解释。通过在字符串字面量前加上 r 字符来创建原生字符串。
void main() {
print(r"falcon\tfalcon\tfalcon");
print("becomes");
print("falcon\tfalcon\tfalcon");
}
示例使用了原生字符串。
$ dart main.dart falcon\tfalcon\tfalcon becomes falcon falcon falcon
Dart 字符串插值
字符串插值是评估包含变量和表达式的字符串的过程。当评估插值字符串时,变量和表达式将被替换为它们相应的值。
在 Dart 中,$ 用于插值变量,${} 用于插值表达式。
void main() {
var name = "John Doe";
var occupation = "gardener";
print("$name is a $occupation");
var x = 12;
var y = 14;
print("${x} * ${y} = ${x * y}");
}
在示例中,我们插值了两个变量和一个表达式。
print("$name is a $occupation");
通过在变量名前加上美元符号来插值变量。
print("${x} * ${y} = ${x * y}");
如果我们想评估更复杂的表达式,我们使用 ${}。
$ dart main.dart John Doe is a gardener 12 * 14 = 168
如果我们想输出美元符号,我们对其进行转义:\$。
void main() {
var item = "beer";
var price = 4.5;
print("The price of a $item is \$$price");
}
示例打印了啤酒的美元价格。
$ dart main.dart The price of a beer is $4.5
Dart 字符串 isEmpty/isNotEmpty
使用 isEmpty 和 isNotEmpty,我们可以分别检查 Dart 中的字符串是否为空或非空。
void main() {
var word1 = '';
var word2 = "falcon";
if (word1.isEmpty) {
print('word1 is empty');
} else {
print('word1 is not empty');
}
if (word2.isNotEmpty) {
print('word2 is not empty');
} else {
print('word2 is empty');
}
}
示例在两个单词上调用了这些方法。
$ dart main.dart word1 is empty word2 is not empty
Dart 截断字符串
我们可以使用 trim、trimLeft 和 trimRight 方法去除字符串中的空白字符。
void main() {
var word = " falcon\t";
print("|" + word + "|");
print("|" + word.trim() + "|");
print("|" + word.trimLeft() + "|");
print("|" + word.trimRight() + "|");
print("|" + word + "|");
}
示例在一个具有前导和尾部空白字符的字符串上使用了这些方法。
$ dart main.dart | falcon | |falcon| |falcon | | falcon| | falcon |
Dart 字符串遍历
我们可以借助 runes 属性和 String.fromCharCode 方法来遍历字符串的元素。
void main() {
var word = 'falcon';
for (var c in word.runes) {
print(String.fromCharCode(c));
}
}
示例将 'falcon' 这个词的字符打印到终端;每个字符占一行。
$ dart main.dart f a l c o n
来源
在本文中,我们介绍了 Dart 中的字符串。
作者
列出 所有 Dart 教程。