Dart StringBuffer writeAll()
Syntax & Examples


StringBuffer.writeAll() method

The `writeAll` method in Dart iterates over the given objects and writes them in sequence using a specified separator.


Syntax of StringBuffer.writeAll()

The syntax of StringBuffer.writeAll() method is:

 void writeAll(Iterable objects, [ String separator = "" ]) 

This writeAll() method of StringBuffer iterates over the given objects and writes them in sequence.

Parameters

ParameterOptional/RequiredDescription
objectsrequiredthe iterable objects to write
separatoroptionalthe separator used to join the objects (default is an empty string)

Return Type

StringBuffer.writeAll() returns value of type void.



✐ Examples

1 Write list of words with space separator

In this example,

We create a list `words` containing strings. We create a `StringBuffer` named `buffer` and use the `writeAll` method to write the words with a space separator.

Dart Program

void main() {
  List<String> words = ['Hello', 'world', 'from', 'Dart'];
  StringBuffer buffer = StringBuffer();
  buffer.writeAll(words, ' ');
  print(buffer.toString());
}

Output

Hello world from Dart

2 Write set of numbers with comma separator

In this example,

We create a set `numbers` containing integers. We create a `StringBuffer` named `buffer` and use the `writeAll` method to write the numbers with a comma separator.

Dart Program

void main() {
  Set<int> numbers = {1, 2, 3, 4, 5};
  StringBuffer buffer = StringBuffer();
  buffer.writeAll(numbers, ', ');
  print(buffer.toString());
}

Output

1, 2, 3, 4, 5

3 Write list of lines with newline separator

In this example,

We create a list `lines` containing strings. We create a `StringBuffer` named `buffer` and use the `writeAll` method to write the lines with a newline separator.

Dart Program

void main() {
  List<String> lines = ['Line 1', 'Line 2', 'Line 3'];
  StringBuffer buffer = StringBuffer();
  buffer.writeAll(lines, '\n');
  print(buffer.toString());
}

Output

Line 1
Line 2
Line 3

Summary

In this Dart tutorial, we learned about writeAll() method of StringBuffer: the syntax and few working examples with output and detailed explanation for each example.