Dart 字符串分割
最后修改日期:2024 年 1 月 28 日
本文将介绍如何在 Dart 语言中分割字符串。
List<String> split(Pattern pattern)
split
函数根据提供的模式匹配项分割字符串,并返回一个子字符串列表。模式可以是字符串或正则表达式。
Dart 字符串分割简单示例
以下示例将句子分割成单词。
main.dart
void main() { final text = "There is an old hawk in the sky"; final words = text.split(' '); for (final word in words) { print("${word} has ${word.length} characters"); } }
在程序中,我们通过空格字符将句子分割成单词列表。
final text = "There is an old hawk in the sky";
我们有一条文本消息。
final words = text.split(' ');
我们将消息分割成单词。分隔符是空格。
for (final word in words) { print("${word} has ${word.length} characters"); }
我们遍历单词列表,并将它们及其大小打印到控制台。
$ dart main.dart There has 5 characters is has 2 characters an has 2 characters old has 3 characters hawk has 4 characters in has 2 characters the has 3 characters sky has 3 characters
Dart 使用正则表达式分割字符串
在下一个示例中,我们将使用正则表达式来分割字符串。
main.dart
void main() { final text = "falcon,eagle,forest;sky,cloud,water;rock,wind"; final words = text.split(RegExp(r"[,;]")); for (final word in words) { print(word); } }
在示例中,我们有一段文本,其中的单词由两个字符分隔:逗号和分号。我们想同时按这两个字符分割字符串。
final words = text.split(RegExp(r"[,;]"));
我们使用简单的正则表达式来按这两个字符分割。
$ dart main.dart falcon eagle forest sky cloud water rock wind
Dart 按空格分割字符串
在以下示例中,我们将字符串按空格分割。
main.dart
void main() { final text = "There are\t\t many clouds in the \n sky"; final pattern = RegExp(r"\s+"); final words = text.split(pattern); print(words); for (final word in words) { print(word); } }
在示例中,我们有一个包含多个空白字符的字符串:制表符、单个空格和换行符。
final pattern = RegExp(r"\s+");
我们使用一个正则表达式,该表达式可以匹配一个或多个空白字符。
$ dart main.dart [There, are, many, clouds, in, the, sky] There are many clouds in the sky
Dart 分割 runes
split
函数不适用于 runes。
main.dart
import "dart:io"; void main() { final msg = "one 🐘 and three 🐋"; final els = msg.split(""); for (final e in els) { stdout.write("${e} "); } print('\n----------------------'); for (final rune in msg.runes) { stdout.write("${String.fromCharCode(rune)} "); } }
为了枚举 runes,我们需要使用 runes
属性。
$ dart runes.dart o n e � � a n d t h r e e � � ---------------------- o n e 🐘 a n d t h r e e 🐋
Dart splitMapJoin
splitMapJoin
函数会分割字符串,转换其部分,然后将它们合并成一个新字符串。
main.dart
void main() { final text = '''Foxes are omnivorous mammals belonging to several genera of the family Canidae. Foxes have a flattened skull, upright triangular ears, a pointed, slightly upturned snout, and a long bushy tail. Foxes live on every continent except Antarctica. By far the most common and widespread species of fox is the red fox.'''; final res = text.splitMapJoin(RegExp(r"[fF]ox(es)?"), onMatch: (m) => '*${m.group(0)}*'); print(res); }
在程序中,我们使用 splitMapJoin
函数来标记所有出现的 "fox" 单词,并用星号字符替换。
final res = text.splitMapJoin(RegExp(r"[fF]ox(es)?"), onMatch: (m) => '*${m.group(0)}*');
我们提供一个正则表达式,该正则表达式匹配 "fox" 单词的所有出现,包括复数形式和首字母大写的形式。onMatch
回调函数在每次匹配时被调用。也可以使用 onNonMatch
回调函数。
$ dart main.dart *Foxes* are omnivorous mammals belonging to several genera of the family Canidae. *Foxes* have a flattened skull, upright triangular ears, a pointed, slightly upturned snout, and a long bushy tail. *Foxes* live on every continent except Antarctica. By far the most common and widespread species of *fox* is the red *fox*.
来源
在本文中,我们介绍了 Dart 中字符串的分割方法。
作者
列出 所有 Dart 教程。