partition method
Divides string into everything before pattern
, pattern
, and everything
after pattern
.
Example:
'word'.partition('or'); // ['w', 'or', 'd']
If pattern
is not found, the entire string is treated as coming before
pattern
.
Example:
'word'.partition('z'); // ['word', '', '']
Implementation
List<String> partition(Pattern pattern) {
final matches = pattern.allMatches(this);
if (matches.isEmpty) return [this, '', ''];
final matchStart = matches.first.start;
final matchEnd = matches.first.end;
return [
this.substring(0, matchStart),
this.substring(matchStart, matchEnd),
this.substring(matchEnd)
];
}