/** * 可变长字符串,相比较加号更节省内存空间,对于大量字符串拼接时建议使用。 * 示:{@code * var html = new StringBuilder(); * html.append('<li></li>'); html.toString(); } * */ function StringBuilder() { this._strings = []; } StringBuilder.prototype.append = function(val) { this._strings.push(val); } StringBuilder.prototype.clear = function() { this._strings.length = 0; } StringBuilder.prototype.toString = function() { return this._strings.join(''); } /* * * 格式化字符串,形同C#的String.Format, * 示:{@code * '<li>{0}</li>'.format('结算中');} * */ String.prototype.format = function() { var args = arguments; return this.replace(/{(d+)}/g, function(m, i) { return args[i]; }); }