Skip to content

API StringBuffer

Liu.Yandong.Hanks edited this page Aug 21, 2026 · 3 revisions

StringBuffer

Mutable buffer for high-volume incremental string building.

Script API Reference

Overview

StringBuffer is for building substantial text, typically inside a loop, without allocating an intermediate string per concatenation. Read the result with toString to keep the buffer usable, or with stringAndRelease to take the text and release the buffer deterministically.

Warning

Do not keep using an instance after release or stringAndRelease. The buffer resources are gone at that point.

Quick Reference

Member Returns Summary
new StringBuffer([initialValue]) StringBuffer Creates a buffer, optionally seeded with text.
buffer.append(...values) null Appends values.
buffer.appendLine(...values) null Appends values followed by a line break.
buffer.insert(index, value) null Inserts text at an index.
buffer.clear() null Clears the buffer contents.
buffer.toString() string Returns the current contents.
buffer.release() null Releases the buffer resources.
buffer.stringAndRelease() string Returns the contents, then releases the buffer.

Constructors

new StringBuffer([initialValue])

Parameters

Name Type Required Description
initialValue string No Text the buffer starts with.

Returns

StringBuffer — a new buffer.

Example

var buffer = new StringBuffer("Hello");
return buffer.toString(); // "Hello"

Methods

buffer.append(...values)

Parameters

Name Type Required Description
values any Yes One or more values to append, passed as variadic arguments.

Returns

null.

Behavior

Each value is converted to text and appended in order.

Example

var buffer = new StringBuffer();
buffer.append("A", 1);
return buffer.toString(); // "A1"

buffer.appendLine(...values)

Parameters

Name Type Required Description
values any Yes One or more values to append, passed as variadic arguments.

Returns

null.

Behavior

Appends the values, then a line break.

Example

var buffer = new StringBuffer();
buffer.appendLine("first");
buffer.append("second");
return buffer.toString();

buffer.insert(index, value)

Parameters

Name Type Required Description
index number Yes Position at which the text is inserted.
value string Yes Text to insert.

Returns

null.

Example

var buffer = new StringBuffer("ac");
buffer.insert(1, "b");
return buffer.toString(); // "abc"

buffer.clear()

Parameters

None.

Returns

null.

Behavior

Empties the contents but keeps the buffer usable, unlike release.

Example

var buffer = new StringBuffer("old");
buffer.clear();
return buffer.toString(); // ""

buffer.toString()

Parameters

None.

Returns

string — the current contents.

Behavior

Does not release the buffer, so appending can continue afterwards.

Example

var buffer = new StringBuffer("value");
return buffer.toString(); // "value"

buffer.release()

Parameters

None.

Returns

null.

Behavior

Releases the internal resources. Call it when the accumulated text is no longer needed.

Example

var buffer = new StringBuffer("temporary");
buffer.release();
return 0;

buffer.stringAndRelease()

Parameters

None.

Returns

string — the current contents.

Behavior

Reads the text and releases the buffer in one step. This is the usual last call on a buffer.

Example

var buffer = new StringBuffer("done");
var text = buffer.stringAndRelease();
return text; // "done"

Clone this wiki locally