removeAfter method
- String pattern
Removes everything in the String
after the first match of the pattern
.
Example
String test = 'hello brother what a day today';
String afterString = test.removeAfter('brother'); // returns 'hello ';
Implementation
String removeAfter(String pattern) {
if (isBlank) {
return blankIfNull;
}
if (!contains(pattern)) {
return '';
}
List<String> patternWords = pattern.split(' ');
if (patternWords.isEmpty) {
return '';
}
int indexOfLastPatternWord = indexOf(patternWords.last);
if (patternWords.last.isEmpty) {
return '';
}
return substring(0, indexOfLastPatternWord);
}