removeRepeatedChars method

  1. @useResult
String removeRepeatedChars()

Collapses runs of the same character into one, keeping order.

Operates on UTF-16 code units, so it does not merge multi-unit emoji.

Example:

'aaabbbccaa'.removeRepeatedChars(); // 'abca'

Implementation

@useResult
String removeRepeatedChars() {
  if (length <= 1) return this;
  final StringBuffer sb = StringBuffer(this[0]);
  for (int i = 1; i < length; i++) {
    if (this[i] != this[i - 1]) sb.write(this[i]);
  }
  return sb.toString();
}