removeEnd method

  1. @useResult
String removeEnd(
  1. String end
)

Returns a new string with end removed from the end, if it exists.

Suffix matching is UTF-16 code-unit based (via String.endsWith), so the cut point length - end.length is a guaranteed-valid code-unit boundary. Uses plain String.substring (not the grapheme-aware substringSafe) because the latter would reinterpret that code-unit index as a grapheme index, refusing to split a base+combining-mark cluster and leaving the suffix un-stripped. Code-unit slicing keeps the contract consistent: a bare combining mark passed as end is stripped, matching endsWith. Audited: 2026-06-12 11:26 EDT

Implementation

@useResult
String removeEnd(String end) => endsWith(end) ? substring(0, length - end.length) : this;