Home > Web Front-end > JS Tutorial > body text

javascript StringBuilder class implementation_javascript skills

WBOY
Release: 2016-05-16 18:57:28
Original
1046 people have browsed it

Copy code 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. In fact, it is implemented using array, push, join, etc. The following is how to use this class
Copy the code 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);

is very simple and does not require much explanation. If you have used StringBuilder in .NET, you will also know how to use it here.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!