commonSuffix method

  1. @useResult
String commonSuffix()

Returns the longest common suffix of all strings in this list.

Returns empty string if list is empty or any element is empty. Compares whole characters (grapheme clusters), so the result never starts with half an emoji or a stray combining mark.

Example:

['ending', 'ding'].commonSuffix();  // 'ding'
['a😀', 'b😁'].commonSuffix();  // ''

Audited: 2026-06-12 11:26 EDT

Implementation

@useResult
String commonSuffix() {
  if (isEmpty) return '';
  // Graphemes, reversed so the shared prefix count gives the suffix length.
  // The code-unit scan split surrogate pairs that share a low surrogate.
  List<String> suffix = this[0].characters.toList().reversed.toList();
  for (final String s in skip(1)) {
    final List<String> other = s.characters.toList().reversed.toList();
    suffix = suffix.sublist(0, _leadingMatchCount(suffix, other));
    if (suffix.isEmpty) return '';
  }
  return suffix.reversed.join();
}