Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why to use StringBuffer in Dart instead of Iterable.join?

Tags:

dart

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?

like image 309
Samuel Hapak Avatar asked Aug 23 '13 07:08

Samuel Hapak


People also ask

Why is StringBuffer faster?

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.

What is StringBuffer in Dart?

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.

Why StringBuilder is more efficient than StringBuffer?

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.

How do you concatenate two strings in darts?

By ' + ' Operator In Dart, we can use the '+' operator to concatenate the strings. Example: Using '+' operator to concatenate strings in Dart.


1 Answers

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).

like image 107
Fox32 Avatar answered Sep 20 '22 19:09

Fox32