rtrim function

String rtrim(
  1. String str, [
  2. String? chars
])

Removes the given chars from the end of the string.

If chars is null, trailing whitespace is removed.

Example:

rtrim('hello  '); // 'hello'
rtrim('helloxx', 'x'); // 'hello'

Implementation

String rtrim(String str, [String? chars]) {
  if (chars == null) return str.trimRight();
  final set = chars.split('').toSet();
  var end = str.length;
  while (end > 0 && set.contains(str[end - 1])) {
    end--;
  }
  return str.substring(0, end);
}