A simple StringBuilder class implementation
The Code is as follows:
// Initializes a new instance of the StringBuilder class
// And appends the given value if supplied
Function StringBuilder (value)
{
This. strings = new Array ("");
This. append (value );
}
// Appends the given value to the end of this instance.
StringBuilder. prototype. append = function (value)
{
If (value)
{
This. strings. push (value );
}
}
// Clears the string buffer
StringBuilder. prototype. clear = function ()
{
This. strings. length = 1;
}
// Converts this instance to a String.
StringBuilder. prototype. toString = function ()
{
Return this. strings. join ("");
}
The code looks simple and straightforward. It is actually implemented using array, push, join, etc. The following describes how to use this class
The Code is as follows:
// Create a StringBuilder
Var sb = new StringBuilder ();
// Append some text
Sb. append ("Some of those preparing for international ");
Sb. append ("exams such as the TOEFL ");
Sb. append ("need extra practice for the listening section ");
// Get the full string value
Var s = sb. toString ();
Alert (s );
It is very simple and does not require much explanation. If you use StringBuilder in. NET, you will also know how to use it.