In Dart, you can concatenate Strings effectively by two ways: you can use StringBuffer class and then convert it to the String, or you can put all of your substrings into the List and then call join('') on them.
I do not understand, what are the pluses of the StringBuffer and why should I use it instead of joining List. Could someone please explain?
The StringBuffer class is used to represent characters that can be modified. The significant performance difference between these two classes is that StringBuffer is faster than String when performing simple concatenations. In String manipulation code, character strings are routinely concatenated.
A class for concatenating strings efficiently. Allows for the incremental building of a string using write*() methods. The strings are concatenated to a single string only when toString is called.
String is immutable whereas StringBuffer and StringBuilder are mutable classes. StringBuffer is thread-safe and synchronized whereas StringBuilder is not. That's why StringBuilder is faster than StringBuffer.
By ' + ' Operator In Dart, we can use the '+' operator to concatenate the strings. Example: Using '+' operator to concatenate strings in Dart.
There isn't a big difference. If you already have a list of strings, there is no difference in using StringBuffer.writeAll
or Iterable.join
. The Iterable.join
method uses a StringBuffer
internaly:
String join([String separator = ""]) {
StringBuffer buffer = new StringBuffer();
buffer.writeAll(this, separator);
return buffer.toString();
}
From the Dart documentation (click on the code button on the right).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With