js 字符串拼接方法,类似c#的StringBuilder

之前写的一个字符串拼接的方法,类似C#中的StringBuilder方法,在这分享给大家:

$.StringBuffer=function(){
        this._strings_ = new Array();
        if (typeof $.StringBuffer._initialized == "undefined") {
            $.StringBuffer.prototype = {
                append:function(str){
                    if(typeof str == 'string'){
                        this._strings_.push(str);
                    }
                },
                appendFormat:function(str){
                    var args = arguments;
                    str = str.replace(/\{(\d+)\}/g, function(m, i){
                        return args[++i];
                    });
                    this._strings_.push(str);
                },
                toString:function(){
                    if(typeof arguments[0]=='string'){
                        return this._strings_.join(arguments[0]);
                    }
                    return this._strings_.join("");
                },
                clear:function(){
                    this._strings_ = [];
                }
            };
            $.StringBuffer._initialized = true;
        };
    };

实质还是用的数组的拼接方法,并加了replace方法进行格式化字符串,示例:

var str = new $.StringBuffer();
str.append("<ul class='aa'>");
str.appendFormat("<li><a href='{0}' title='{1}'>{1}</a><li>","main.html","测试标题");
alert(str.toString());

对处理大量字符串拼接,在IE6浏览器上表现的速度和性能尤为突出,希望对大家有帮助,如果大家有更好的方法希望留下分享地址,:)

转载于:https://www.cnblogs.com/bjmumu/p/3182610.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值