Dart String split() Method
To split a string in Dart, call the String.split() method with a delimiter or another Pattern. The method returns a List<String> containing the parts of the original string.
The delimiter itself is not included in the returned list. Dart can split a string using a single character, a multi-character substring, or a regular expression.
Syntax of String.split() in Dart
The syntax of String.split() method is
split(Pattern pattern)
where pattern is a delimiter/separator. Any substring that matches this pattern is considered delimiter, and the string is split at this location.
We can also provide a plain string for the pattern parameter.
The function returns a list of strings.
pattern: APatternused to find each split position. BothStringandRegExpimplementPattern.- Return value: A new
List<String>containing the substrings between matches. - The original string is not modified because Dart strings are immutable.
Dart Split String Examples
Split a Dart String by a Delimiter Character
In this example, we will take a string with words separated by delimiter -. And then split it into array of words using split() method.
main.dart
void main(){
String str = 'hello-world-tutorialkart';
//split string
var arr = str.split('-');
print(arr);
}
Output
[hello, world, tutorialkart]
Each hyphen marks a split position. The hyphens are removed, and the three remaining substrings become elements of the returned list.
Split a Dart String by a Delimiter Substring
In this example, we will take a string with words separated by delimiter abc. The delimiter is ripped off from the string and the parts are returned as list.
main.dart
void main(){
String str = 'helloabcworldabctutorialkart';
//split string
var arr = str.split('abc');
print(arr);
}
Output
[hello, world, tutorialkart]
A delimiter does not have to be one character long. Here, every complete occurrence of abc is used as a separator.
Split a Comma-Separated String in Dart
In this example, we will take a string of comma separated values. We shall split this CSV string into list of values.
main.dart
void main(){
String str = '25,85,96,741,63';
//split string
var arr = str.split(',');
print(arr);
}
Output
[25, 85, 96, 741, 63]
The returned elements are strings. Convert them separately when numeric values are required.
void main() {
String values = '25,85,96,741,63';
List<int> numbers = values
.split(',')
.map(int.parse)
.toList();
print(numbers);
}
Output
[25, 85, 96, 741, 63]
This simple approach is suitable for values separated only by commas. A complete CSV document can contain quoted fields, escaped quotation marks, and commas inside field values, so such documents should be processed with a CSV parser.
Split a Dart String by Whitespace
Splitting with a single space, as in split(' '), handles only literal space characters and can produce empty elements when several spaces occur together. Use a regular expression to split text containing spaces, tabs, or line breaks.
void main() {
String text = 'Dart string\tsplit\nexample';
List<String> words = text.trim().split(RegExp(r'\s+'));
print(words);
}
Output
[Dart, string, split, example]
The expression \s+ matches one or more whitespace characters. Calling trim() first prevents leading or trailing whitespace from creating unwanted empty elements.
Split a Dart String with Multiple Delimiters
Pass a RegExp to split() when more than one delimiter should be accepted. The following example splits a string at commas, semicolons, or vertical bars.
void main() {
String text = 'red,green;blue|yellow';
List<String> colors = text.split(RegExp(r'[,;|]'));
print(colors);
}
Output
[red, green, blue, yellow]
Remove Empty Values After Splitting a String
Adjacent delimiters, or a delimiter at the beginning or end of a string, can produce empty strings in the result. Use where() when those empty elements should be removed.
void main() {
String text = ',apple,,banana,';
List<String> fruits = text
.split(',')
.where((item) => item.isNotEmpty)
.toList();
print(fruits);
}
Output
[apple, banana]
Split a String into Characters in Dart
An empty delimiter splits a string at its UTF-16 code-unit boundaries.
void main() {
String text = 'Dart';
List<String> characters = text.split('');
print(characters);
}
Output
[D, a, r, t]
For user-perceived characters such as emoji or letters containing combining marks, UTF-16 units are not always the same as visible characters. Unicode-aware text processing may therefore require the characters package instead of split('').
What Happens When the Delimiter Is Missing?
If the delimiter does not occur in the string, split() returns a list containing the complete original string as its only element.
void main() {
String text = 'tutorialkart';
List<String> result = text.split('-');
print(result);
print(result.length);
}
Output
[tutorialkart]
1
Split Only at the First Delimiter in Dart
String.split() splits at every match and does not accept a limit argument. To separate a string only at its first delimiter, find the delimiter with indexOf() and extract the two sections with substring().
void main() {
String text = 'name=TutorialKart=Website';
String delimiter = '=';
int position = text.indexOf(delimiter);
if (position != -1) {
String key = text.substring(0, position);
String value = text.substring(position + delimiter.length);
print(key);
print(value);
}
}
Output
name
TutorialKart=Website
Dart String Splitting FAQs
What does split() return in Dart?
String.split() returns a List<String>. Each list element contains the text found between two delimiter matches.
Can a regular expression be used with Dart split()?
Yes. The parameter type is Pattern, so you can pass a RegExp to split on whitespace, multiple delimiter characters, or another pattern.
Why does split() return empty strings?
Empty strings can appear when delimiters are adjacent or occur at the boundaries of the input. Filter the result with where((value) => value.isNotEmpty) when those entries are not needed.
How do you convert split values to integers in Dart?
Call map(int.parse) on the split result and then call toList(). Use int.tryParse() when an element may not contain a valid integer.
Summary of Splitting Strings in Dart
In this Dart Tutorial, we learned how to split a string in Dart with String.split(). A string delimiter handles fixed separators, while a RegExp supports whitespace and multiple delimiter patterns. The returned values can also be filtered, trimmed, or converted to other data types as needed.
TutorialKart.com