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

Javascript
纯JS半透明Tip效果代码
JavaScript 读URL参数增强改进版版
JS对URL字符串进行编码/解码分析
javescript完整操作Table的增加行,删除行的列子大全
js可填可选的下拉框
prototype Element学习笔记(篇一)
prototype Element学习笔记(篇二)
prototype Element学习笔记(Element篇三)
Prototype使用指南之selector.js说明
不唐突的JavaScript的七条准则整理收集
js判断变量是否空值的代码
Firefox getBoxObjectFor getBoundingClientRect联系
Div自动滚动到末尾的代码
javascript笔试题目附答案
javascript引导程序
js身份证验证超强脚本
JS给元素注册事件的代码
JavaScript函数、方法、对象代码
JS写的数字拼图小游戏代码[学习参考]
关于B/S判断浏览器断开的问题讨论

Javascript 中的 javascript StringBuilder类实现


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