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 (this.isBlank) {
    return this;
  }

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