allBefore method

String allBefore(
  1. Pattern pattern
)

Searches the string for the last occurrence of a pattern and returns everything before that occurrence.

Returns '' if no occurrences were found.

Example:

'value=1'.allBefore('=');          // 'value'
'i like turtles'.allBefore('like') // 'i '

Implementation

String allBefore(Pattern pattern) {
  var matchIterator = pattern.allMatches(this).iterator;

  Match? match;
  while (matchIterator.moveNext()) {
    match = matchIterator.current;
  }

  if (match != null) {
    return substring(0, match.start);
  }
  return '';
}