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

Javascript
JavaScript 渐变效果页面图片控制
JavaScript blog式日历控件新算法
Javascript 读后台cookie代码
js 弹簧效果代码
JavaScript滑移效果代码
javascript字符串拆分成单个字符相加和不超过10,求最终值
Prototype中dom对象方法汇总
Jquery与Prototype混合用法对比
javascript下IE与FF兼容函数收集
突破winxp sp2/win2003 sp2超强弹窗代码
js点击出现场层层外点击层消失的代码
Javascript模拟scroll滚动效果脚本
javascript各种复制代码收集
javascript的trim,ltrim,rtrim自定义函数
仿3721首页模块拖曳移动效果js代码[可拖曳层移动层]
Javascript拖拽系列文章1之offsetParent属性
Discuz! 6.1_jQuery兼容问题
js日历控件(可精确到分钟)
javascript DOM实用学习资料
javascript实现的树型下拉框改进版

Javascript 中的 javascript StringBuilder类实现


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