Why use StringBuffer in Dart instead of Iterable.join?

In Dart, you can efficiently combine strings in two ways: you can use the StringBuffer class and then convert it to String, or you can put all the substrings in a list and then call join ('') on them.

I don’t understand what the advantages of StringBuffer are and why I should use it instead of connecting to a List. Can someone explain?

+4
source share
2 answers

There is not much 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 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).

+5
source

StringBuffer is more efficient since it does not create a string object until you call toString.

Seth Lad blogged about the benefits here with some backup numbers.

There is a similar blog post about it here .

0
source

Source: https://habr.com/ru/post/1498551/


All Articles