Skip to content

Commit 7bcce30

Browse files
jpsiitonenCommit Bot
authored andcommitted
Documentation update for StringBuffer
Added example code to StringBuffer. Closes dart-lang/sdk#47809 dart-lang/sdk#47809 GitOrigin-RevId: d12b7bb1bc4c309f0191698ee58b44ab7d0b8006 Change-Id: I59343029742bb60e8ea5151a345eed3a20268624 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/221600 Reviewed-by: Lasse Nielsen <lrn@google.com> Commit-Queue: Lasse Nielsen <lrn@google.com>
1 parent e12cea5 commit 7bcce30

File tree

1 file changed

+64
-1
lines changed

1 file changed

+64
-1
lines changed

sdk/lib/core/string_buffer.dart

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,71 @@ part of dart.core;
99
/// Allows for the incremental building of a string using `write*()` methods.
1010
/// The strings are concatenated to a single string only when [toString] is
1111
/// called.
12+
///
13+
/// Example:
14+
/// ```dart
15+
/// final buffer = StringBuffer('DART');
16+
/// print(buffer.length); // 4
17+
/// ```
18+
/// To add the string representation of an object, as returned by
19+
/// [Object.toString], to the buffer, use [write].
20+
/// Is also used for adding a string directly.
21+
/// ```
22+
/// buffer.write(' is open source');
23+
/// print(buffer.length); // 19
24+
/// print(buffer); // DART is open source
25+
///
26+
/// const int dartYear = 2011;
27+
/// buffer
28+
/// ..write(' since ') // Writes a string.
29+
/// ..write(dartYear); // Writes an int.
30+
/// print(buffer); // DART is open source since 2011
31+
/// print(buffer.length); // 30
32+
/// ```
33+
/// To add a newline after the object's string representation, use [writeln].
34+
/// Calling [writeln] with no argument adds a single newline to the buffer.
35+
/// ```
36+
/// buffer.writeln(); // Contains "DART is open source since 2011\n".
37+
/// buffer.writeln('-' * (buffer.length - 1)); // 30 '-'s and a newline.
38+
/// print(buffer.length); // 62
39+
/// ```
40+
/// To write multiple objects to the buffer, use [writeAll].
41+
/// ```
42+
/// const separator = '-';
43+
/// buffer.writeAll(['Dart', 'is', 'fun!'], separator);
44+
/// print(buffer.length); // 74
45+
/// print(buffer);
46+
/// // DART is open source since 2011
47+
/// // ------------------------------
48+
/// // Dart-is-fun!
49+
/// ```
50+
/// To add the string representation of a Unicode code point, `charCode`,
51+
/// to the buffer, use [writeCharCode].
52+
/// ```
53+
/// buffer.writeCharCode(0x0A); // LF (line feed)
54+
/// buffer.writeCharCode(0x44); // 'D'
55+
/// buffer.writeCharCode(0x61); // 'a'
56+
/// buffer.writeCharCode(0x72); // 'r'
57+
/// buffer.writeCharCode(0x74); // 't'
58+
/// print(buffer.length); // 79
59+
/// ```
60+
/// To convert the content to a single string, use [toString].
61+
/// ```
62+
/// final text = buffer.toString();
63+
/// print(text);
64+
/// // DART is open source since 2011
65+
/// // ------------------------------
66+
/// // Dart-is-fun!
67+
/// // Dart
68+
/// ```
69+
/// To clear the buffer, so that it can be reused, use [clear].
70+
/// ```
71+
/// buffer.clear();
72+
/// print(buffer.isEmpty); // true
73+
/// print(buffer.length); // 0
74+
/// ```
1275
class StringBuffer implements StringSink {
13-
/// Creates the string buffer with an initial content.
76+
/// Creates a string buffer containing the provided [content].
1477
external StringBuffer([Object content = ""]);
1578

1679
/// Returns the length of the content that has been accumulated so far.

0 commit comments

Comments
 (0)