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

Javascript
javascript prototype的深度探索不是原型继承那么简单
兼容Firefox和IE的onpropertychange事件oninput
javascript兼容firefox的文本输入长度提示
js字符编码函数区别分析
js程序中美元符号$是什么
javascript 数组的方法集合
javascript编程必备_JS语法字典
javascript动画效果打开/关闭层
javascript键盘事件全面控制脚本代码
javascript键盘上下键的操作(选择)
JavaScript容错例外处理
js将网址转为urlencode类型
JScript中使用ADODB.Stream判断文件编码的代码
[原创]js循环输出图片,不足的要补0
js left,right,mid函数
javascript 网站常用的iframe分割
javascript下兼容firefox选取textarea文本的代码
JSON学习笔记
asp.net和asp下ACCESS的参数化查询
JavaScript入门学习书籍推荐

Javascript 中的 javascript StringBuilder类实现


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-12   浏览: 306 ::
收藏到网摘: 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,你也会知道这里如何用。