Dart StringBuffer
last modified October 18, 2026
In this article we show how to concatenate strings efficiently in Dart language
using the StringBuffer class.
A string is a sequence of UTF-16 code units. It is used to represent some text
in a program. Strings are immutable; each concatenation with the +
operator creates a new string object.
StringBuffer is a class that is used to create new strings efficiently
by concatenation. It contains write, writeAll,
writeln, writeCharCode and clear member
functions for string manipulation. The content is turned into a string with
toString. A StringBuffer is also a
StringSink, so it can be passed to code that writes text, such as
write in custom toString implementations.
Dart StringBuffer simple example
The following is a simple Dart program which uses StringBuffer.
void main() {
final msg = StringBuffer('There are');
msg.write(' three ');
msg.writeAll(['hawks', 'in', 'the sky'], ' ');
final output = msg.toString();
print(output);
}
The program dynamically builds a new string and prints it to the console.
final msg = StringBuffer('There are');
We create a new instance of StringBuffer. We add some initial text.
msg.write(' three ');
We insert another string with write. The method accepts any object
and converts it to a string with toString.
msg.writeAll(['hawks', 'in', 'the sky'], ' ');
We insert multiple strings with writeAll. The first parameter is an
iterable of objects, the second parameter is a separator.
final output = msg.toString();
Finally, we turn the string buffer into a single string with
toString.
$ dart main.dart There are three hawks in the sky
Dart StringBuffer writeln
The writeln method writes a new string followed by a newline
character.
void main() {
final msg = StringBuffer('blue sky\n');
msg.writeln('old owl');
msg.writeln('lonely wolf');
msg.writeln('strict regime');
print(msg.length);
print(msg.toString());
}
The example creates a string consisting of several lines of text data. In
addition, we print the size of the text with length, which is the
number of UTF-16 code units, including the newline characters.
$ dart main.dart 43 blue sky old owl lonely wolf strict regime
Dart StringBuffer in a loop
The typical use of StringBuffer is building a string in a loop,
where the number of pieces is not known in advance.
void main() {
final buf = StringBuffer();
for (var i = 1; i <= 5; i++) {
if (buf.isNotEmpty) {
buf.write(', ');
}
buf.write('item$i');
}
buf.writeCharCode(0x21);
print(buf);
}
We build a comma-separated list. The writeCharCode method appends
a character given by its code point; here 0x21 is the exclamation mark. The
print function calls toString on the buffer.
$ dart main.dart item1, item2, item3, item4, item5!
Dart StringBuffer isEmpty
We can check if a string buffer is empty with isEmpty. The buffer
can be emptied with clear.
void main() {
final msg = StringBuffer();
check(msg);
msg.writeAll(['an', 'old', 'hawk', 'in', 'the', 'sky'], ' ');
check(msg);
print(msg.toString());
msg.clear();
check(msg);
}
void check(StringBuffer msg) {
if (msg.isEmpty) {
print('the buffer is empty');
} else {
print('the buffer is not empty');
}
}
In the program, we build a string with StringBuffer and use the
isEmpty property to check, if the buffer is empty.
$ dart main.dart the buffer is empty the buffer is not empty an old hawk in the sky the buffer is empty
Dart StringBuffer vs. string concatenation
When many pieces are appended, repeated concatenation copies the growing string
over and over, while StringBuffer collects the pieces and joins
them once. The following program measures the difference.
void main() {
const n = 50000;
final sw = Stopwatch()..start();
var s = '';
for (var i = 0; i < n; i++) {
s += 'x$i,';
}
print('concatenation: ${sw.elapsedMilliseconds} ms, length ${s.length}');
sw
..reset()
..start();
final buf = StringBuffer();
for (var i = 0; i < n; i++) {
buf.write('x$i,');
}
final res = buf.toString();
print('StringBuffer: ${sw.elapsedMilliseconds} ms, length ${res.length}');
}
We append 50,000 small strings with both techniques. Each += copies
the whole string built so far, so the time of the concatenation grows
quadratically with the number of pieces, whereas the buffer grows linearly. The
exact timings depend on the machine and the Dart version, but the difference
is dramatic. The two results have the same length.
$ dart main.dart concatenation: 2582 ms, length 338890 StringBuffer: 5 ms, length 338890
Source
Dart StringBuffer - API reference
In this article we have covered the StringBuffer class in Dart.
Author
List all Dart tutorials.