A simple stringbuilder class implementation
- // 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
- // 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.
(See more: http://www.codeproject.com/KB/scripting/stringbuilder.aspx)