removeBefore method

String? removeBefore(
  1. 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 (this.isBlank) {
    return this;
  }

  if (!this!.contains(pattern)) {
    return '';
  }

  List<String> patternWords = pattern.split(' ');

  if (patternWords.isEmpty) {
    return '';
  }
  int indexOfFirstPatternWord = this!.indexOf(patternWords.first);

  if (patternWords.last.length == 0) {
    return '';
  }

  return this!.substring(
    indexOfFirstPatternWord + 1,
    this!.length,
  );
}