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的简单实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。
热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短