JavascriptStringBuilder class implementation _ javascript skills

Source: Internet
Author: User
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.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.