当前位置: 代码迷 >> 综合 >> js模拟stringbuilder 测试通过
  详细解决方案

js模拟stringbuilder 测试通过

热度:31   发布时间:2024-01-21 14:06:07.0
/**  * 模拟StringBuilder  
*/  var  StringBuilder = function(){  this.vStrings = new Array();  this.flag = false;  if(!this.flag){  StringBuilder.prototype.append = function(str){  this.vStrings.push(str);  }  StringBuilder.prototype.toString = function(){  return this.vStrings.join("");  }  StringBuilder.prototype.charAt = function(index){  return this.toString().charAt(index);  }  StringBuilder.prototype.clear = function(){  this.vStrings.length = 0;  }  StringBuilder.prototype.Delete = function (start,end){  var tempString = this.toString();  var prevString = tempString.substring(0,start);  var nextString = end ?  tempString.substring(end) : tempString.substring(start+1);  this.clear();  this.append(prevString);  this.append(nextString);  }  StringBuilder.prototype.length = function(){  return this.toString().length;  }  StringBuilder.prototype.substring = function(start,end){  return this.toString().substring(start,end);  }  StringBuilder.prototype.replace = function(oldStr,newStr){  var newStr = newStr ? newStr : "";  var tempString =  this.toString().replace(new RegExp(oldStr,"g"),newStr);  this.clear();  this.append(tempString);  }  StringBuilder.prototype.indexOf = function (val){  return this.toString().indexOf(val);  }  StringBuilder.prototype.lastIndexOf = function(val){  return this.toString().lastIndexOf(val);  }  StringBuilder.prototype.insert = function (offset,str){  var prevString = this.substring(0,offset);  var middleString = str;  var nextString = this.substring(offset);  this.clear();  this.append(prevString);  this.append(middleString);  this.append(nextString);  }  this.flag = true;  }  }  

 

  相关解决方案