js实现StringBuffer的简单实例
实现:
functionStringBuffer(){
this.__strings__=[];
};
StringBuffer.prototype.Append=function(str){
this.__strings__.push(str);
returnthis;
};
//格式化字符串
StringBuffer.prototype.AppendFormat=function(str){
for(vari=1;i<arguments.length;i++){
varparent="\\{"+(i-1)+"\\}";
varreg=newRegExp(parent,"g")
str=str.replace(reg,arguments[i]);
}
this.__strings__.push(str);
returnthis;
}
StringBuffer.prototype.ToString=function(){
returnthis.__strings__.join('');
};
StringBuffer.prototype.clear=function(){
this.__strings__=[];
}
StringBuffer.prototype.size=function(){
returnthis.__strings__.length;
}
实例化调用
varsbHtml=newStringBuffer();
sbHtml.Append('hello');
sbHtml.Append('world');
console.log(sbHtml.ToString());
以上这篇js实现StringBuffer的简单实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。