Dart Split String
To split a string with a substring as delimiter in Dart programming, use String.split() method.
Syntax
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.
Examples
Split String by 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]
Split String by Delimiter String
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]
Split String by Comma
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]
Conclusion
In this Dart Tutorial, we learned how to split a string in Dart with delimiter substring using String.split() method.