squeeze method

String squeeze(
  1. String char
)

Squeezes the String by removing repeats of a given character.

Example

String foo = 'foofoofoofoofoo';
String fooSqueezed = foo.squeeze('o'); // 'fofofofofo';

Implementation

String squeeze(String char) {
  if (isBlank) {
    return blankIfNull;
  }

  var sb = '';
  for (var i = 0; i < length; i++) {
    if (i == 0 || this[i - 1] != this[i] || (this[i - 1] == this[i] && this[i] != char)) {
      sb += this[i];
    }
  }
  return sb;
}