当前位置: 首页 > 图文教程 > 网络编程 > Javascript > javascript StringBuilder类实现

Javascript
仿51JOB的地区选择效果(可选择多个地区)
Javascript打印网页部分内容的脚本
Ext面向对象开发实践(续)
js的闭包的一个示例说明
javascript 字符串连接的性能问题(多浏览器)
JavaScript脚本性能优化注意事项
js电信网通双线自动选择技巧
js DIV滚动条随机位置的设置技巧
Javascript日期对象的dateAdd与dateDiff方法
小试JavaScript多线程
jquery $.ajax入门应用一
JS 俄罗斯方块完美注释版代码
JavaScript在IE中“意外地调用了方法或属性访问”
设置下载不需要倒计时cookie(倒计时代码)
拖拉表格的JS函数
js刷新框架子页面的七种方法代码
FireFox与IE 下js兼容触发click事件的代码
js利用div背景,做一个竖线的效果。
javascript 贪吃蛇实现代码
JavaScript无提示关闭窗口(兼容IE/Firefox/Chrome)

Javascript 中的 javascript StringBuilder类实现


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-12   浏览: 211 ::
收藏到网摘: n/a

一个简单的StringBuilder类实现

复制代码 代码如下:

// 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("");
}

代码看上去很简单直接。实际上就是用array,push,join等来实现,以下是如何使用该类
复制代码 代码如下:

// 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);

非常简单,不需要太多的说明。如果你在.NET中用了StringBuilder,你也会知道这里如何用。