Dart StringBuffer toString()
Syntax & Examples


StringBuffer.toString() method

The `toString` method in Dart returns the contents of the string buffer as a concatenated string.


Syntax of StringBuffer.toString()

The syntax of StringBuffer.toString() method is:

 String toString() 

This toString() method of StringBuffer returns the contents of string buffer as a concatenated string.

Return Type

StringBuffer.toString() returns value of type String.



✐ Examples

1 Converting buffer with content to string

In this example,

  1. We create a `StringBuffer` named `buffer` with the content 'Hello'.
  2. We convert the buffer to a string using the `toString()` method and print it to standard output.

Dart Program

void main() {
  StringBuffer buffer = StringBuffer('Hello');
  print(buffer.toString());
}

Output

Hello

2 Converting buffer with written content to string

In this example,

  1. We create a `StringBuffer` named `buffer` without any initial content.
  2. We use the `write()` method to add 'World' to the buffer.
  3. We convert the buffer to a string using the `toString()` method and print it to standard output.

Dart Program

void main() {
  StringBuffer buffer = StringBuffer();
  buffer.write('World');
  print(buffer.toString());
}

Output

World

3 Converting buffer with character codes to string

In this example,

  1. We create a `StringBuffer` named `buffer` without any initial content.
  2. We use the `writeCharCode()` method to add the character codes 65 ('A'), 66 ('B'), and 67 ('C') to the buffer.
  3. We convert the buffer to a string using the `toString()` method and print it to standard output.

Dart Program

void main() {
  StringBuffer buffer = StringBuffer();
  buffer.writeCharCode(65);
  buffer.writeCharCode(66);
  buffer.writeCharCode(67);
  print(buffer.toString());
}

Output

ABC

Summary

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