Dart/Flutter - Split a String Into a List Examples

This tutorial gives you examples of how to split a String into a List based on the given delimiter.

Using String split()

Dart's String class has an instance method split() that can be used to split a String into a List<String>.

  List<String> split(Pattern pattern);

Below are the usage examples of the method.

Split by Comma

To split a string by comma, use the split method with comma (,) as the argument.

  String text = 'Dart,Flutter,Programming';
  List<String> result = text.split(',');

Output:

  ['Dart', 'Flutter', 'Programming']

 

Split by Space

Below is another example with space as the delimiter.

  String text = 'Android iOS Fuschia Linux Windows MacOS';
  List result = text.split(' ');

Output:

  ['Android', 'iOS', 'Fuschia', 'Linux', 'Windows', 'MacOS']

 

Split using Regex

The split method accepts a Pattern to be passed as argument. In simple cases, passing a string as the argument is enough. Sometimes using regular expression is necessary. For example, there is a string axbxxcd and we want to split it by x. If we pass x as the argument, there will be an empty string as the result of two sequential x characters.

  String text = 'axbxxcd';
  List<String> result = text.split('x');

Output:

  ['a', 'b', '', 'cd']

 

For excluding the empty string from the result, pass a RegExp instead and find the occurence of x+ which means one or more x characters.

  String text = 'axbxxcd';
  List<String> result = text.split(new RegExp(r'x+'));

Output:

  ['a', 'b', 'cd']

 

Split with An Empty String Delimiter

If the delimiter is an empty string, each character will become an element in the resulting array. Keep in mind that split() works on code unit boundaries, not at rune boundaries, which may affect characters with multi code units.

  String text = 'dart';
  List<String> result = text.split('');

Output:

  ['d', 'a', 'r', 't']

 

Split with An Unmatched Delimiter

If the given delimiter doesn't present in the string, it will return a List with the string as the only element.

  String text = 'dart';
  List<String> result = text.split('x');

Output:

  ['dart']

 

Split An Empty String

If the passed string is empty, the result will be an empty List if the passed argument matches.

  String text = '';
  List<String> result = text.split('');

Output:

  []

 

If the pattern doesn't match, it will return a List with an empty string as the only element.

  String text = '';
  List<String> result = text.split('x');

Output:

  ['']

 

Handle Characters with Multi Code Units

Some characters can have multiple code units despite only having one rune. split() works at code unit boundaries, not at rune boundaries. Look at the example below. There is a string that contains a Byzantine music symbol. The symbol is represented by two code units. If you split the string with an empty string as the delimiter, the expected result is each character in the original string become an element with the same value in the result. However, because the symbol is formed from multiple code units, it's splited into two elements in the result.

  String text = '𝀾woolha';
  List result = text.split('');

Output:

  ['�', '�', 'w', 'o', 'o', 'l', 'h', 'a']

 

For this case, the solution is iterating at rune boundaries. The list of runes can be obtained by accessing runes property.

  String text = '𝀾woolha';
  List result =text.runes
      .map((rune) => new String.fromCharCode(rune))
      .toList());

Output:

  ['𝀾', 'w', 'o', 'o', 'l', 'h', 'a']

 

That's how to convert a String into a List<String>. It can be done using split() method which supports any delimiter or Pattern. For doing the reverse thing, you can read about how to join a List into a String with any separator.