Loading

js实现StringBuffer

实现

function StringBuffer() {
    this.__strings__ = [];
};
StringBuffer.prototype.Append = function (str) {
    this.__strings__.push(str);
    return this;
};
//格式化字符串
StringBuffer.prototype.AppendFormat = function (str) {
    for (var i = 1; i < arguments.length; i++) {
        var parent = "\\{" + (i - 1) + "\\}";
        var reg = new RegExp(parent, "g")
        str = str.replace(reg, arguments[i]);
    }

    this.__strings__.push(str);
    return this;
}
StringBuffer.prototype.ToString = function () {
    return this.__strings__.join('');
};
StringBuffer.prototype.clear = function () {
    this.__strings__ = [];
}
StringBuffer.prototype.size = function () {
    return this.__strings__.length;
}

 

实例化调用

var sbHtml=new StringBuffer();
sbHtml.Append('hello');
sbHtml.Append('world');
   
console.log(sbHtml.ToString());

  

posted @ 2016-08-01 16:21  歪头儿在北京  阅读(8200)  评论(0编辑  收藏  举报