[intro]     [up]

Class StringBuffer


A class used to improve the performance of string concatenations. When two strings objects are concatenated, a third object must be allocated to contain the new string. Probably the other two strings will not be used anymore, and stay as garbage on the memory. The way that garbage is removed depends on the browser, but the fact is that some garbage remains. When many concatenations are executed, the performance may be greatly improved by the StringBuffer.
It is internally implemented with an array. Each call to append() generates a new element on the array. When the toString() method is called, the array is joined into a single string.
Because the standard toString() method is used to build the string, when the buffer itself is transformed to a string (like on a alert(buffer), or str = "abc " + buffer), the internal string is used instead of somethig like [Object].

Example
var buffer = new StringBuffer();
buffer.append("This ");
buffer.append("is ");
//Nested callings - the append method returns the buffer itself
buffer.append("a ").append("test");
alert(buffer); //Message: "This is a test"


Constructor Arguments
Name Type Default Description
initialCapacity Numeric 10 The initial capacity for append operations without needing to resize the internal array (not bytes)

Properties
initialCapacity The initial capacity for append operations without needing to resize the internal array (not bytes)

Methods
append Appends a value
clear Clears the buffer
length Returns the buffered string length
toString Returns the buffered String



Properties
initialCapacity
The initial capacity for append operations without needing to resize the internal array (not bytes)





Methods
 
append
Appends a value
Arguments
Name Type Default Description
value Object   The value to append
 
clear
Clears the buffer
 
length
Returns the buffered string length
 
toString
Returns the buffered String