removePrefix method

  1. @useResult
String removePrefix(
  1. String prefix
)

Removes prefix from the start if present; otherwise returns this. Audited: 2026-06-12 11:26 EDT

Implementation

@useResult
// Use code-unit `substring`, not the grapheme-indexed `substringSafe`:
// `startsWith` and `prefix.length` are code-unit measures, so the slice must
// be too. Mixing a code-unit length into a grapheme index dropped the wrong
// count when the prefix or content held astral chars (e.g. '😀ab'.removePrefix('😀')).
String removePrefix(String prefix) => startsWith(prefix) ? substring(prefix.length) : this;