JavaScript StringBuilder class implements _javascript techniques

Source: Internet
Author: User

Copy Code code 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 is instance to a String.
StringBuilder.prototype.toString = function ()
{
Return This.strings.join ("");
}

The code looks simple and straightforward. is actually implemented with Array,push,join, and the following is how to use the class
Copy Code code 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);

Very simple, do not need too much description. If you're in. NET with StringBuilder, you will also know how to use it here.

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.