2007-07-21
javaScript 的 StringBuffer 类
关键字: javaScriptjava 中的字符串拼装一般都不直接用String ,而是使用 StringBuffer,主要是出于性能考虑
javaScript 中也是一样的,直接使用 “2”+“3” 这种字符串相加对于性能消耗是很大的。
但是 javaScript 中没有现成的 StringBuffer 类,要自己封装,从网上找了几个,都写得太简单了
于是自己动手改写了一个,封装了一下,提供给大家使用。
js 代码
- /**
- * StringBuffer Class, to join two string is the most use
- * @author xiaofei 2007-7-20
- */
- function StringBuffer()
- {
- this._strings = [];
- if(arguments.length==1)
- {
- this._strings.push(arguments[0]);
- }
- }
- StringBuffer.prototype.append = function(str)
- {
- this._strings.push(str);
- return this;
- }
- StringBuffer.prototype.toString = function()
- {
- return this._strings.join("");
- }
- /* 返回长度 */
- StringBuffer.prototype.length = function()
- {
- var str = this._strings.join("");
- return str.length;
- }
- /* 删除后几位字符 */
- StringBuffer.prototype.del = function(num)
- {
- var len = this.length();
- var str = this.toString();
- str = str.slice(0,len-num);
- this._strings = [];
- this._strings.push(str);
- }
发表评论
- 浏览: 9122 次

- 详细资料
搜索本博客
最近加入圈子
最新评论
-
ajax jsp 无刷新上传文件
加进度条就要修改后台处理文件的部分,上传进行中接收到查询请求返回当前进度。挺麻烦 ...
-- by s79 -
ajax jsp 无刷新上传文件
为什么 不加上一个 进度条呢? 其不是更好呢?
-- by wangjian3q -
ajax jsp 无刷新上传文件
<Iframe></Iframe>用得好!!!
-- by dxai -
javaScript 中 call 函数 ...
有道理。按照hax的说法,感觉js的方法调用,和python的实例方法调用一样, ...
-- by 笨笨狗 -
javaScript 中 call 函数 ...
楼主不要引用jscript文档来说“官方”,官方你要么拿netscape的文档, ...
-- by hax






评论排行榜