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

Javascript
很多人都是用下面的js刷新站IP和PV
javascript仿126邮箱TAB切换效果
js右下角弹出窗口,点击可关闭效果
javascript使用window.name解决跨域问题
DIV层之拖动、关闭、打开效果代码
Javascript条件判断使用小技巧总结
javascript获取不重复的随机数的方法比较
Firefox下设为主页的JavaScript代码
JavaScript编制留言簿程序代码
javascript表格随机排序代码
javascript一些不错的函数脚本代码
javascript之textarea打字机效果提示代码推荐
js实现的类似QQ的等级的代码
js检查是否全是中文
[原创]js判断是否有中文的脚本_js判断中文方法集合
js判断输入是否中文,数字,身份证等等js函数集合
js直接编辑当前cookie的脚本
jquery 必填项判断表单是否为空的方法
javascript高亮效果的二种实现方法
JavaScript基本入门语法集合

Javascript 中的 javascript StringBuilder类实现


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