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