手机
当前位置:查字典教程网 >编程开发 >Javascript教程 >javascript StringBuilder类实现
javascript StringBuilder类实现
摘要:复制代码代码如下://InitializesanewinstanceoftheStringBuilderclass//andappendst...

复制代码 代码如下:

// 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,你也会知道这里如何用。

【javascript StringBuilder类实现】相关文章:

理解javascript封装

javascript消除window.close()的提示窗口

javascript使用Promise对象实现异步编程

javascript中attachEvent用法

javascript的事件描述

javascript实现获取服务器时间

javascript下使用Promise封装FileReader

javascript实现简单的进度条

javascript实现简单的省市区三级联动

javascript事件冒泡实例分析

精品推荐
分类导航