removeBetweenAll method

  1. @useResult
String removeBetweenAll(
  1. String start,
  2. String end, {
  3. bool inclusive = true,
})

Returns a new string with all content between start and end delimiters removed.

If inclusive is true (default), the delimiters themselves are also removed.

Implementation

@useResult
String removeBetweenAll(String start, String end, {bool inclusive = true}) {
  if (start.isEmpty) {
    return this;
  }

  if (this == start + end && inclusive) {
    return '';
  }

  final String found = between(start, end, endOptional: false);
  if (found.isEmpty && !contains(start + end)) {
    if (contains(start) && contains(end) && indexOf(start) < indexOf(end)) {
      if (inclusive && this == start + end) {
        return '';
      }
    } else {
      return this;
    }
  }
  final String pattern = inclusive ? (start + found + end) : found;
  if (pattern.isEmpty && !inclusive) {
    return this;
  }

  return replaceAll(pattern, '');
}