The syntax of String.split() method is:
List<String> split(Pattern pattern)
This split() method of String splits the string at matches of pattern
and returns a list of substrings.
Parameter | Optional/Required | Description |
---|---|---|
pattern | required | the pattern to split the string at matches |
String.split() returns value of type List<String>
.
In this example,
str
containing '1-2-3-4-5'.split()
method to split the string at each occurrence of '-'.parts
.void main() {
String str = '1-2-3-4-5';
List<String> parts = str.split('-');
print('Parts: $parts');
}
Parts: [1, 2, 3, 4, 5]
In this example,
str
containing 'A,B,C,D,E'.split()
method to split the string at each occurrence of ','.parts
.void main() {
String str = 'A,B,C,D,E';
List<String> parts = str.split(',');
print('Parts: $parts');
}
Parts: [A, B, C, D, E]
In this example,
str
containing 'apple,banana,grape'.split()
method to split the string at each occurrence of ','.parts
.void main() {
String str = 'apple,banana,grape';
List<String> parts = str.split(',');
print('Parts: $parts');
}
Parts: [apple, banana, grape]
In this Dart tutorial, we learned about split() method of String: the syntax and few working examples with output and detailed explanation for each example.