removeStart method

  1. @useResult
String? removeStart(
  1. String? start, {
  2. bool isCaseSensitive = true,
  3. bool trimFirst = false,
})

Returns a new string with start removed from the beginning, or null if the result is empty.

When isCaseSensitive is false, uses case-insensitive matching. When trimFirst is true, the string is trimmed before checking.

The prefix is matched and removed by UTF-16 code units, so an emoji or multi-code-unit start is removed exactly.

Example:

'prefix-body'.removeStart('prefix-');                     // 'body'
'👋 hi'.removeStart('👋 ');                               // 'hi'
'ABCdef'.removeStart('abc', isCaseSensitive: false);      // 'def'

Audited: 2026-06-12 11:26 EDT

Implementation

@useResult
String? removeStart(
  String? start, {
  bool isCaseSensitive = true,
  bool trimFirst = false,
}) {
  if (trimFirst) {
    return trim().removeStart(start, isCaseSensitive: isCaseSensitive);
  }

  if (start == null || start.isEmpty) {
    return this;
  }

  // start.length is a code-unit count, so cut with code-unit substring; the
  // grapheme-indexed substringSafe used before over-cut after an emoji prefix.
  if (isCaseSensitive) {
    return startsWith(start) ? substring(start.length).nullIfEmpty() : this;
  }

  // Compare the same code-unit span of this string, so the cut point matches
  // what was compared even when lowercasing changes a string's length.
  final bool hasPrefix =
      length >= start.length && substring(0, start.length).toLowerCase() == start.toLowerCase();

  return hasPrefix ? substring(start.length).nullIfEmpty() : this;
}