js实现C#的StringBuilder效果完整实例
本文实例讲述了js实现C#的StringBuilder效果。分享给大家供大家参考,具体如下:
/*
#####################DONOTMODIFYTHISHEADER#####################
#Title:StringBuilderClass#
#Description:SimulatestheC#StringBuilderClassinJavascript.#
#Author:AdamSmith#
#Email:ibulwark@hotmail.com#
#Date:November12,2006#
#####################################################################
*/
//SimulatestheC#StringBuilderClassinJavascript.
//Parameter["stringToAdd"]-Thestringtoadd.
StringBuilder=function(stringToAdd)
{
varh=newArray();
if(stringToAdd){h[0]=stringToAdd;}
this.Append=Append;
this.AppendLine=AppendLine;
this.ToString=ToString;
this.Clear=Clear;
this.Length=Length;
this.Replace=Replace;
this.Remove=Remove;
this.Insert=Insert;
this.GetType=GetType;
//Appendsthestringrepresentationofaspecifiedobjecttotheendofthisinstance.
//Parameter["stringToAppend"]-Thestringtoappend.
functionAppend(stringToAppend)
{
h[h.length]=stringToAppend;
}
//Appendsthestringrepresentationofaspecifiedobjecttotheendofthisinstancewithacarriagereturnandlinefeed.
//Parameter["stringToAppend"]-Thestringtoappend.
functionAppendLine(stringToAppend)
{
h[h.length]=stringToAppend;
h[h.length]="\r\n";
}
//ConvertsaStringBuildertoaString.
functionToString()
{
if(!h){return"";}
if(h.length<2){return(h[0])?h[0]:"";}
vara=h.join('');
h=newArray();
h[0]=a;
returna;
}
//ClearstheStringBuilder
functionClear()
{
h=newArray();
}
//GetstheStringBuilderLength
functionLength()
{
if(!h){return0;}
if(h.length<2){return(h[0])?h[0].length:0;}
vara=h.join('');
h=newArray();
h[0]=a;
returna.length;
}
//Replacesalloccurrencesofaspecifiedcharacterorstringinthisinstancewithanotherspecifiedcharacterorstring.
//Parameter["oldValue"]-Thestringtoreplace.
//Parameter["newValue"]-ThestringthatreplacesoldValue.
//Parameter["caseSensitive"]-Trueorfalseforcasereplace.
//ReturnValue-AreferencetothisinstancewithallinstancesofoldValuereplacedbynewValue.
functionReplace(oldValue,newValue,caseSensitive)
{
varr=newRegExp(oldValue,(caseSensitive==true)?'g':'gi');
varb=h.join('').replace(r,newValue);
h=newArray();
h[0]=b;
returnthis;
}
//Removesthespecifiedrangeofcharactersfromthisinstance.
//Parameter["startIndex"]-Thepositionwhereremovalbegins.
//Parameter["length"]-Thenumberofcharacterstoremove.
//ReturnValue-Areferencetothisinstanceaftertheexciseoperationhasoccurred.
functionRemove(startIndex,length)
{
vars=h.join('');
h=newArray();
if(startIndex<1){h[0]=s.substring(length,s.length);}
if(startIndex>s.length){h[0]=s;}
else
{
h[0]=s.substring(0,startIndex);
h[1]=s.substring(startIndex+length,s.length);
}
returnthis;
}
//Insertsthestringrepresentationofaspecifiedobjectintothisinstanceataspecifiedcharacterposition.
//Parameter["index"]-Thepositionatwhichtoinsert.
//Parameter["value"]-Thestringtoinsert.
//ReturnValue-Areferencetothisinstanceaftertheinsertoperationhasoccurred.
functionInsert(index,value)
{
vars=h.join('');
h=newArray();
if(index<1){h[0]=value;h[1]=s;}
if(index>=s.length){h[0]=s;h[1]=value;}
else
{
h[0]=s.substring(0,index);
h[1]=value;
h[2]=s.substring(index,s.length);
}
returnthis;
}
//Getsthetype
functionGetType()
{
return"StringBuilder";
}
};
希望本文所述对大家JavaScript程序设计有所帮助。