between method

String between(
  1. String start,
  2. String end, {
  3. bool endOptional = true,
  4. bool trim = true,
})

Extracts content between first occurrence of start and end.

If endOptional is true and end is not found, returns content after start.

Special case: if end is empty, it is treated as "not found", so with endOptional: true (default) the entire tail after start is returned, and with endOptional: false an empty string is returned.

Implementation

String between(String start, String end, {bool endOptional = true, bool trim = true}) {
  if (isEmpty || start.isEmpty) return '';
  final int startIndex = indexOf(start);
  if (startIndex == -1) return '';
  final int endIndex = end.isEmpty ? -1 : indexOf(end, startIndex + start.length);
  if (endIndex == -1) {
    if (endOptional) {
      final String content = substringSafe(startIndex + start.length);
      return trim ? content.trim() : content;
    }
    return '';
  }
  final String content = substringSafe(startIndex + start.length, endIndex);
  return trim ? content.trim() : content;
}