setString method

void setString(
  1. String encoded,
  2. BufferEncoding encoding, [
  3. int offset = 0,
  4. int? length,
])

Writes an [encoded] string to a region of the buffer.

The encoded string is written to the range [offset : offset+length]. If length is omitted, the range extends to the end of the buffer.

The range must satisy the relations 0offsetoffset+lengththis.length.

final Buffer buffer = Buffer(7);
const String value = '4869204B617465';
buffer.setString(value, BufferEncoding.hex, 0, 2);
print(buffer); // [72, 105, 0, 0, 0, 0, 0]
buffer.setString(value, BufferEncoding.hex, 0);
print(buffer); // [72, 105, 32, 75, 97, 116, 101]

Implementation

void setString(
  final String encoded,
  final BufferEncoding encoding, [
  final int offset = 0,
  final int? length,
]) {
  final Iterable<int> bytes = _decode(encoded, encoding);
  final int rngLength = length ?? min(this.length - offset, bytes.length);
  _data.setRange(offset, offset + rngLength, bytes);
}