问题描述
//Fig.15.14:StringBuilderInsertRemove.cs//DemonstratingmethodsInsertandRemoveofthe//StringBuilderclass.usingSystem;usingSystem.Windows.Forms;usingSystem.Text;namespaceStringBuilderInsertRemove{///<summary>///testtheInsertandRemovemethods///</summary>classStringBuilderInsertRemove{///<summary>///Themainentrypointfortheapplication.///</summary>[STAThread]staticvoidMain(string[]args){objectobjectValue="hello";stringstringValue="goodbye";char[]characterArray={'a','b','c','d','e','f'};boolbooleanValue=true;charcharacterValue='K';intintegerValue=7;longlongValue=10000000;floatfloatValue=2.5F;doubledoubleValue=33.333;StringBuilderbuffer=newStringBuilder();stringoutput;//insertvaluesintobufferbuffer.Insert(0,objectValue);buffer.Insert(0,"");buffer.Insert(0,stringValue);buffer.Insert(0,"");buffer.Insert(0,characterArray);buffer.Insert(0,"");buffer.Insert(0,booleanValue);buffer.Insert(0,"");buffer.Insert(0,characterValue);buffer.Insert(0,"");buffer.Insert(0,integerValue);buffer.Insert(0,"");buffer.Insert(0,longValue);buffer.Insert(0,"");buffer.Insert(0,floatValue);buffer.Insert(0,"");buffer.Insert(0,doubleValue);buffer.Insert(0,"");output="bufferafterinserts:n"+buffer.ToString()+"nn";buffer.Remove(10,1);//delete2in2.5buffer.Remove(3,4);//delete.333in33.333output+="bufferafterRemoves:n"+buffer.ToString();MessageBox.Show(output,"DemonstratingStringBuilder"+"InsertandRemovemethods",MessageBoxButtons.OK,MessageBoxIcon.Information);}//endmethodMain}//endclassStringBuilderInsertRemove}/****************************************************************************(C)Copyright2002byDeitel&Associates,Inc.andPrenticeHall.**AllRightsReserved.****DISCLAIMER:Theauthorsandpublisherofthisbookhaveusedtheir**besteffortsinpreparingthebook.Theseeffortsincludethe**development,research,andtestingofthetheoriesandprograms**todeterminetheireffectiveness.Theauthorsandpublishermake**nowarrantyofanykind,expressedorimplied,withregardtothese**programsortothedocumentationcontainedinthesebooks.Theauthors**andpublishershallnotbeliableinanyeventforincidentalor**consequentialdamagesinconnectionwith,orarisingoutof,the**furnishing,performance,oruseoftheseprograms.****************************************************************************/如上,而且buffer.Remove(3,4);和buffer.Remove(2,4);结果为什么都一样,不理解
解决方案
解决方案二:
空格是5个字符,前5个都是空格buffer.Remove(3,4);和buffer.Remove(2,4)就是删掉一个空格,当然也就是一样的了。你查一下就清楚,StartIndex应该在这里才能实现buffer.Remove(16,1);//delete2in2.5buffer.Remove(7,4);//delete.333in33.333
解决方案三:
空格是两个,粘过去变5个了,我知道了,头晕了,buffer.Remove(2,4);是33.3332.5100000007KTrueabcdefgoodbyehellobuffer.Remove(3,4);是33.3332.5100000007KTrueabcdefgoodbyehello