removeLastChar method

  1. @useResult
String removeLastChar()

Returns a new string with the last grapheme cluster removed.

Counts by grapheme cluster (via substringSafe), so a trailing emoji or base+combining-mark sequence is removed whole and never split into an orphaned surrogate or a stranded mark. Empty in, empty out. Audited: 2026-06-12 11:26 EDT

Implementation

@useResult
String removeLastChar() {
  // Grapheme length, not String.length: substringSafe is grapheme-indexed, so
  // passing a code-unit count would miscount whenever the trailing grapheme
  // spans multiple code units.
  final int graphemeCount = characters.length;
  return graphemeCount < 1 ? '' : substringSafe(0, graphemeCount - 1);
}