Dart split string
last modified October 18, 2026
In this article we show how to split strings in Dart language.
List<String> split(Pattern pattern)
The split function splits the string at matches of the supplied
pattern and returns a list of substrings. The pattern may be a string or a
regular expression; both implement the Pattern interface. The
matched separators are not part of the result.
Dart split string simple example
The following example splits a sentence into words.
void main() {
const text = 'There is an old hawk in the sky';
final words = text.split(' ');
for (final word in words) {
print('$word has ${word.length} characters');
}
}
In the program, we split a sentence into a list of words by a space character.
const text = 'There is an old hawk in the sky';
We have a text message.
final words = text.split(' ');
We split the message into words. The pattern is a space.
for (final word in words) {
print('$word has ${word.length} characters');
}
We go over the list of words and print them and their size to the console.
$ 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 split string with regex
In the next example, we split a string using a regular expression.
void main() {
const text = 'falcon,eagle,forest;sky,cloud,water;rock,wind';
final words = text.split(RegExp(r'[,;]'));
for (final word in words) {
print(word);
}
}
In the example, we have a text in which words are separated by two characters: comma and semicolon. We want to split the string by both characters.
final words = text.split(RegExp(r'[,;]'));
We use a simple regular expression to split by both characters.
$ dart main.dart falcon eagle forest sky cloud water rock wind
Dart split string by spaces
In the following example, we split a string by white space.
void main() {
const text = 'There are\t\t many clouds in the \n sky';
final pattern = RegExp(r'\s+');
final words = text.split(pattern);
print(words);
}
In the example, we have a string with multiple white spaces: tabs, single spaces and new lines.
final pattern = RegExp(r'\s+');
We use a regular expression which matches one or more white space characters.
$ dart main.dart [There, are, many, clouds, in, the, sky]
Dart split empty strings
When the text starts or ends with a separator, or contains two consecutive
separators, split produces empty strings. We can remove them with
where.
void main() {
const text = ',a,,b,';
print(text.split(','));
print(text.split(',').where((e) => e.isNotEmpty).toList());
print(''.split(',').length);
print('abc'.split(''));
}
The first line shows the empty strings at the start, the end, and in the middle. Splitting an empty string yields a list with one empty string; splitting by an empty pattern splits the string into single characters (UTF-16 code units).
$ dart main.dart [, a, , b, ] [a, b] 1 [a, b, c]
Dart split lines
To split text into lines, we use LineSplitter from
dart:convert. It handles \n, \r\n, and
\r line endings.
import 'dart:convert';
void main() {
const text = 'first line\nsecond line\r\nthird line';
final lines = LineSplitter.split(text).toList();
print(lines);
print(lines.length);
}
The static LineSplitter.split method returns an iterable of lines
without the line terminators.
$ dart main.dart [first line, second line, third line] 3
Dart split runes
The split function with an empty pattern does not work well for
characters outside the Basic Multilingual Plane, such as emojis. Such characters
are stored as two UTF-16 code units (a surrogate pair), and split
cuts them in half.
import 'dart:io';
void main() {
const 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)} ');
}
print('');
}
In order to enumerate Unicode code points, we need to use the runes
property. The first loop prints a replacement character for each half of the
emoji. Note that even runes do not always correspond to what users perceive as a
single character (for instance, an emoji with a skin tone modifier); for
grapheme clusters, use the characters package.
$ dart main.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
The splitMapJoin function splits the string, converts its parts,
and combines them into a new string.
void main() {
const 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);
}
In the program, we use the splitMapJoin function to mark all
occurrences of the fox word with star characters.
final res = text.splitMapJoin(
RegExp(r'[fF]ox(es)?'),
onMatch: (m) => '*${m.group(0)}*',
);
We supply a regular expression that matches all occurrences of the word fox,
including the plural form and the form with the capital F letter. The
onMatch callback is called on each match. It is also possible to
utilize the onNonMatch callback, which is called for the text
between the matches.
$ 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*.
Source
Dart String.split - API reference
In this article we have covered splitting strings in Dart.
Author
List all Dart tutorials.