setLength method

void setLength(
  1. int newLength
)

Sets the length of this StringBuilder.

If the new length is less than the current length, the StringBuilder is truncated. If the new length is greater, null characters are appended.

newLength the new length

Implementation

void setLength(int newLength) {
  String current = _buffer.toString();

  if (newLength < 0) {
    throw InvalidArgumentException('Length cannot be negative');
  }

  if (newLength < current.length) {
    _buffer.clear();
    _buffer.write(current.substring(0, newLength));
  } else if (newLength > current.length) {
    int padding = newLength - current.length;
    _buffer.write('\x00' * padding);
  }
}