/** * 模拟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; } }