stringbuffer (java platform se 7 )

26
Overview Package Class Use Tree Deprecated Index Help Java™ Platform Standard Ed. 7 Prev Class Next Class Frames No Frames All Classes Summary: Nested | Field | Constr | Method Detail: Field | Constr | Method java.lang Class StringBuffer java.lang.Object java.lang.StringBuffer All Implemented Interfaces: Serializable, Appendable, CharSequence public final class StringBuffer extends Object implements Serializable, CharSequence A threadsafe, mutable sequence of characters. A string buffer is like a String, but can be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls. String buffers are safe for use by multiple threads. The methods are synchronized where necessary so that all the operations on any particular instance behave as if they occur in some serial order that is consistent with the order of the method calls made by each of the individual threads involved. The principal operations on a StringBuffer are the append and insert methods, which are overloaded so as to accept data of any type. Each effectively converts a given datum to a string and then appends or inserts the characters of that string to the string buffer. The append method always adds these characters at the end of the buffer; the insert method adds the characters at a specified point. For example, if z refers to a string buffer object whose current contents are "start", then the method call z.append("le") would cause the string buffer to contain "startle", whereas z.insert(4, "le") would alter the string buffer to contain "starlet". In general, if sb refers to an instance of a StringBuffer, then sb.append(x) has the same effect as sb.insert(sb.length(), x). Whenever an operation occurs involving a source sequence (such as appending or inserting from a source sequence) this class synchronizes only on the string buffer performing the operation, not on the source. Every string buffer has a capacity. As long as the length of the character sequence contained in the string buffer does not exceed the capacity, it is not necessary to allocate a new internal buffer array. If the internal buffer overflows, it is automatically made larger. As of release JDK 5, this class has been supplemented with an equivalent class designed for use by a single thread, StringBuilder. The StringBuilder class should generally be used in preference to this one, as it supports all of the same operations but it is faster, as it performs no synchronization. Since: JDK1.0 See Also: StringBuilder, String, Serialized Form Constructor Summary Constructors

Upload: alyson-chang

Post on 05-Nov-2015

235 views

Category:

Documents


1 download

DESCRIPTION

String buffer

TRANSCRIPT

  • 6/22/2015 StringBuffer (Java Platform SE 7 )

    http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html 1/26

    Overview Package Class Use Tree Deprecated Index HelpJavaPlatformStandardEd.7

    PrevClass NextClass Frames NoFrames AllClasses

    Summary:Nested|Field|Constr|Method Detail:Field|Constr|Method

    java.lang

    ClassStringBuffer

    java.lang.Objectjava.lang.StringBuffer

    AllImplementedInterfaces:

    Serializable,Appendable,CharSequence

    public final class StringBufferextends Object

    implements Serializable, CharSequence

    Athreadsafe,mutablesequenceofcharacters.AstringbufferislikeaString,butcanbemodified.Atanypointintimeitcontainssomeparticularsequenceofcharacters,butthelengthandcontentofthesequencecanbechangedthroughcertainmethodcalls.

    Stringbuffersaresafeforusebymultiplethreads.Themethodsaresynchronizedwherenecessarysothatalltheoperationsonanyparticularinstancebehaveasiftheyoccurinsomeserialorderthatisconsistentwiththeorderofthemethodcallsmadebyeachoftheindividualthreadsinvolved.

    TheprincipaloperationsonaStringBufferaretheappendandinsertmethods,whichareoverloadedsoastoacceptdataofanytype.Eacheffectivelyconvertsagivendatumtoastringandthenappendsorinsertsthecharactersofthatstringtothestringbuffer.Theappendmethodalwaysaddsthesecharactersattheendofthebuffertheinsertmethodaddsthecharactersataspecifiedpoint.

    Forexample,ifzreferstoastringbufferobjectwhosecurrentcontentsare"start",thenthemethodcallz.append("le")wouldcausethestringbuffertocontain"startle",whereasz.insert(4, "le")wouldalterthestringbuffertocontain"starlet".

    Ingeneral,ifsbreferstoaninstanceofaStringBuffer,thensb.append(x)hasthesameeffectassb.insert(sb.length(),x).

    Wheneveranoperationoccursinvolvingasourcesequence(suchasappendingorinsertingfromasourcesequence)thisclasssynchronizesonlyonthestringbufferperformingtheoperation,notonthesource.

    Everystringbufferhasacapacity.Aslongasthelengthofthecharactersequencecontainedinthestringbufferdoesnotexceedthecapacity,itisnotnecessarytoallocateanewinternalbufferarray.Iftheinternalbufferoverflows,itisautomaticallymadelarger.AsofreleaseJDK5,thisclasshasbeensupplementedwithanequivalentclassdesignedforusebyasinglethread,StringBuilder.TheStringBuilderclassshouldgenerallybeusedinpreferencetothisone,asitsupportsallofthesameoperationsbutitisfaster,asitperformsnosynchronization.

    Since:

    JDK1.0

    SeeAlso:

    StringBuilder,String,SerializedForm

    ConstructorSummary

    Constructors

  • 6/22/2015 StringBuffer (Java Platform SE 7 )

    http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html 2/26

    ConstructorandDescription

    StringBuffer()

    Constructsastringbufferwithnocharactersinitandaninitialcapacityof16characters.StringBuffer(CharSequenceseq)

    ConstructsastringbufferthatcontainsthesamecharactersasthespecifiedCharSequence.StringBuffer(intcapacity)

    Constructsastringbufferwithnocharactersinitandthespecifiedinitialcapacity.StringBuffer(Stringstr)

    Constructsastringbufferinitializedtothecontentsofthespecifiedstring.

    MethodSummary

    ModifierandType MethodandDescription

    StringBuffer append(booleanb)

    Appendsthestringrepresentationofthebooleanargumenttothesequence.StringBuffer append(charc)

    Appendsthestringrepresentationofthecharargumenttothissequence.StringBuffer append(char[]str)

    Appendsthestringrepresentationofthechararrayargumenttothissequence.StringBuffer append(char[]str, intoffset, intlen)

    Appendsthestringrepresentationofasubarrayofthechararrayargumenttothissequence.

    StringBuffer append(CharSequences)

    AppendsthespecifiedCharSequencetothissequence.StringBuffer append(CharSequences, intstart, intend)

    AppendsasubsequenceofthespecifiedCharSequencetothissequence.StringBuffer append(doubled)

    Appendsthestringrepresentationofthedoubleargumenttothissequence.StringBuffer append(floatf)

    Appendsthestringrepresentationofthefloatargumenttothissequence.StringBuffer append(inti)

    Appendsthestringrepresentationoftheintargumenttothissequence.StringBuffer append(longlng)

    Appendsthestringrepresentationofthelongargumenttothissequence.StringBuffer append(Objectobj)

    AppendsthestringrepresentationoftheObjectargument.StringBuffer append(Stringstr)

    Appendsthespecifiedstringtothischaractersequence.StringBuffer append(StringBuffersb)

    AppendsthespecifiedStringBuffertothissequence.StringBuffer appendCodePoint(intcodePoint)

    AppendsthestringrepresentationofthecodePointargumenttothissequence.int capacity()

    Returnsthecurrentcapacity.char charAt(intindex)

    Returnsthecharvalueinthissequenceatthespecifiedindex.

    Methods

  • 6/22/2015 StringBuffer (Java Platform SE 7 )

    http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html 3/26

    int codePointAt(intindex)

    Returnsthecharacter(Unicodecodepoint)atthespecifiedindex.int codePointBefore(intindex)

    Returnsthecharacter(Unicodecodepoint)beforethespecifiedindex.int codePointCount(intbeginIndex, intendIndex)

    ReturnsthenumberofUnicodecodepointsinthespecifiedtextrangeofthissequence.

    StringBuffer delete(intstart, intend)

    Removesthecharactersinasubstringofthissequence.StringBuffer deleteCharAt(intindex)

    Removesthecharatthespecifiedpositioninthissequence.void ensureCapacity(intminimumCapacity)

    Ensuresthatthecapacityisatleastequaltothespecifiedminimum.void getChars(intsrcBegin, intsrcEnd, char[]dst, intdstBegin)

    Charactersarecopiedfromthissequenceintothedestinationcharacterarraydst.int indexOf(Stringstr)

    Returnstheindexwithinthisstringofthefirstoccurrenceofthespecifiedsubstring.int indexOf(Stringstr, intfromIndex)

    Returnstheindexwithinthisstringofthefirstoccurrenceofthespecifiedsubstring,startingatthespecifiedindex.

    StringBuffer insert(intoffset, booleanb)

    Insertsthestringrepresentationofthebooleanargumentintothissequence.StringBuffer insert(intoffset, charc)

    Insertsthestringrepresentationofthecharargumentintothissequence.StringBuffer insert(intoffset, char[]str)

    Insertsthestringrepresentationofthechararrayargumentintothissequence.StringBuffer insert(intindex, char[]str, intoffset, intlen)

    Insertsthestringrepresentationofasubarrayofthestrarrayargumentintothissequence.

    StringBuffer insert(intdstOffset, CharSequences)

    InsertsthespecifiedCharSequenceintothissequence.StringBuffer insert(intdstOffset, CharSequences, intstart, intend)

    InsertsasubsequenceofthespecifiedCharSequenceintothissequence.StringBuffer insert(intoffset, doubled)

    Insertsthestringrepresentationofthedoubleargumentintothissequence.

    StringBuffer insert(intoffset, floatf)

    Insertsthestringrepresentationofthefloatargumentintothissequence.StringBuffer insert(intoffset, inti)

    Insertsthestringrepresentationofthesecondintargumentintothissequence.StringBuffer insert(intoffset, longl)

    Insertsthestringrepresentationofthelongargumentintothissequence.StringBuffer insert(intoffset, Objectobj)

    InsertsthestringrepresentationoftheObjectargumentintothischaractersequence.

    StringBuffer insert(intoffset, Stringstr)

    Insertsthestringintothischaractersequence.int lastIndexOf(Stringstr)

    Returnstheindexwithinthisstringoftherightmostoccurrenceofthespecifiedsubstring.

    int lastIndexOf(Stringstr, intfromIndex)

  • 6/22/2015 StringBuffer (Java Platform SE 7 )

    http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html 4/26

    Returnstheindexwithinthisstringofthelastoccurrenceofthespecifiedsubstring.int length()

    Returnsthelength(charactercount).int offsetByCodePoints(intindex, intcodePointOffset)

    ReturnstheindexwithinthissequencethatisoffsetfromthegivenindexbycodePointOffsetcodepoints.

    StringBuffer replace(intstart, intend, Stringstr)

    ReplacesthecharactersinasubstringofthissequencewithcharactersinthespecifiedString.

    StringBuffer reverse()

    Causesthischaractersequencetobereplacedbythereverseofthesequence.void setCharAt(intindex, charch)

    Thecharacteratthespecifiedindexissettoch.void setLength(intnewLength)

    Setsthelengthofthecharactersequence.CharSequence subSequence(intstart, intend)

    Returnsanewcharactersequencethatisasubsequenceofthissequence.String substring(intstart)

    ReturnsanewStringthatcontainsasubsequenceofcharacterscurrentlycontainedinthischaractersequence.

    String substring(intstart, intend)

    ReturnsanewStringthatcontainsasubsequenceofcharacterscurrentlycontainedinthissequence.

    String toString()

    Returnsastringrepresentingthedatainthissequence.void trimToSize()

    Attemptstoreducestorageusedforthecharactersequence.

    Methodsinheritedfromclassjava.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

    ConstructorDetail

    StringBuffer

    publicStringBuffer()

    Constructsastringbufferwithnocharactersinitandaninitialcapacityof16characters.

    StringBuffer

    publicStringBuffer(intcapacity)

    Constructsastringbufferwithnocharactersinitandthespecifiedinitialcapacity.

    Parameters:

    capacitytheinitialcapacity.

    Throws:

  • 6/22/2015 StringBuffer (Java Platform SE 7 )

    http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html 5/26

    NegativeArraySizeExceptionifthecapacityargumentislessthan0.

    StringBuffer

    publicStringBuffer(Stringstr)

    Constructsastringbufferinitializedtothecontentsofthespecifiedstring.Theinitialcapacityofthestringbufferis16plusthelengthofthestringargument.

    Parameters:

    strtheinitialcontentsofthebuffer.

    Throws:

    NullPointerExceptionifstrisnull

    StringBuffer

    publicStringBuffer(CharSequenceseq)

    ConstructsastringbufferthatcontainsthesamecharactersasthespecifiedCharSequence.Theinitialcapacityofthestringbufferis16plusthelengthoftheCharSequenceargument.

    IfthelengthofthespecifiedCharSequenceislessthanorequaltozero,thenanemptybufferofcapacity16isreturned.

    Parameters:

    seqthesequencetocopy.

    Throws:

    NullPointerExceptionifseqisnull

    Since:

    1.5

    MethodDetail

    length

    publicintlength()

    Returnsthelength(charactercount).

    Specifiedby:

    lengthininterfaceCharSequence

    Returns:

    thelengthofthesequenceofcharacterscurrentlyrepresentedbythisobject

  • 6/22/2015 StringBuffer (Java Platform SE 7 )

    http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html 6/26

    capacity

    publicintcapacity()

    Returnsthecurrentcapacity.Thecapacityistheamountofstorageavailablefornewlyinsertedcharacters,beyondwhichanallocationwilloccur.

    Returns:

    thecurrentcapacity

    ensureCapacity

    publicvoidensureCapacity(intminimumCapacity)

    Ensuresthatthecapacityisatleastequaltothespecifiedminimum.Ifthecurrentcapacityislessthantheargument,thenanewinternalarrayisallocatedwithgreatercapacity.Thenewcapacityisthelargerof:

    TheminimumCapacityargument.Twicetheoldcapacity,plus2.

    IftheminimumCapacityargumentisnonpositive,thismethodtakesnoactionandsimplyreturns.

    Parameters:

    minimumCapacitytheminimumdesiredcapacity.

    trimToSize

    publicvoidtrimToSize()

    Attemptstoreducestorageusedforthecharactersequence.Ifthebufferislargerthannecessarytoholditscurrentsequenceofcharacters,thenitmayberesizedtobecomemorespaceefficient.Callingthismethodmay,butisnotrequiredto,affectthevaluereturnedbyasubsequentcalltothecapacity()method.

    Since:

    1.5

    setLength

    publicvoidsetLength(intnewLength)

    Setsthelengthofthecharactersequence.Thesequenceischangedtoanewcharactersequencewhoselengthisspecifiedbytheargument.ForeverynonnegativeindexklessthannewLength,thecharacteratindexkinthenewcharactersequenceisthesameasthecharacteratindexkintheoldsequenceifkislessthanthelengthoftheoldcharactersequenceotherwise,itisthenullcharacter'\u0000'.Inotherwords,ifthenewLengthargumentislessthanthecurrentlength,thelengthischangedtothespecifiedlength.

    IfthenewLengthargumentisgreaterthanorequaltothecurrentlength,sufficientnullcharacters('\u0000')areappendedsothatlengthbecomesthenewLengthargument.

    ThenewLengthargumentmustbegreaterthanorequalto0.

    Parameters:

    newLengththenewlength

    Throws:

    IndexOutOfBoundsExceptionifthenewLengthargumentisnegative.

  • 6/22/2015 StringBuffer (Java Platform SE 7 )

    http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html 7/26

    SeeAlso:

    length()

    charAt

    publiccharcharAt(intindex)

    Returnsthecharvalueinthissequenceatthespecifiedindex.Thefirstcharvalueisatindex0,thenextat

    index1,andsoon,asinarrayindexing.

    Theindexargumentmustbegreaterthanorequalto0,andlessthanthelengthofthissequence.

    Ifthecharvaluespecifiedbytheindexisasurrogate,thesurrogatevalueisreturned.

    Specifiedby:

    charAtininterfaceCharSequence

    Parameters:

    indextheindexofthedesiredcharvalue.

    Returns:

    thecharvalueatthespecifiedindex.

    Throws:

    IndexOutOfBoundsExceptionifindexisnegativeorgreaterthanorequaltolength().

    SeeAlso:

    length()

    codePointAt

    publicintcodePointAt(intindex)

    Returnsthecharacter(Unicodecodepoint)atthespecifiedindex.Theindexreferstocharvalues(Unicodecode

    units)andrangesfrom0tolength() - 1.

    Ifthecharvaluespecifiedatthegivenindexisinthehighsurrogaterange,thefollowingindexislessthanthe

    lengthofthissequence,andthecharvalueatthefollowingindexisinthelowsurrogaterange,thenthe

    supplementarycodepointcorrespondingtothissurrogatepairisreturned.Otherwise,thecharvalueatthegiven

    indexisreturned.

    Parameters:

    indextheindextothecharvalues

    Returns:

    thecodepointvalueofthecharacterattheindex

    Since:

    1.5

    codePointBefore

  • 6/22/2015 StringBuffer (Java Platform SE 7 )

    http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html 8/26

    publicintcodePointBefore(intindex)

    Returnsthecharacter(Unicodecodepoint)beforethespecifiedindex.Theindexreferstocharvalues(Unicodecodeunits)andrangesfrom1tolength().

    Ifthecharvalueat(index - 1)isinthelowsurrogaterange,(index - 2)isnotnegative,andthecharvalueat(index - 2)isinthehighsurrogaterange,thenthesupplementarycodepointvalueofthesurrogatepairisreturned.Ifthecharvalueatindex - 1isanunpairedlowsurrogateorahighsurrogate,thesurrogatevalueisreturned.

    Parameters:

    indextheindexfollowingthecodepointthatshouldbereturned

    Returns:

    theUnicodecodepointvaluebeforethegivenindex.

    Since:

    1.5

    codePointCount

    publicintcodePointCount(intbeginIndex,

    intendIndex)

    ReturnsthenumberofUnicodecodepointsinthespecifiedtextrangeofthissequence.ThetextrangebeginsatthespecifiedbeginIndexandextendstothecharatindexendIndex - 1.Thusthelength(inchars)ofthetextrangeisendIndex-beginIndex.Unpairedsurrogateswithinthissequencecountasonecodepointeach.

    Parameters:

    beginIndextheindextothefirstcharofthetextrange.

    endIndextheindexafterthelastcharofthetextrange.

    Returns:

    thenumberofUnicodecodepointsinthespecifiedtextrange

    Since:

    1.5

    offsetByCodePoints

    publicintoffsetByCodePoints(intindex,

    intcodePointOffset)

    ReturnstheindexwithinthissequencethatisoffsetfromthegivenindexbycodePointOffsetcodepoints.UnpairedsurrogateswithinthetextrangegivenbyindexandcodePointOffsetcountasonecodepointeach.

    Parameters:

    indextheindextobeoffset

    codePointOffsettheoffsetincodepoints

    Returns:

    theindexwithinthissequence

    Since:

  • 6/22/2015 StringBuffer (Java Platform SE 7 )

    http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html 9/26

    1.5

    getChars

    publicvoidgetChars(intsrcBegin,

    intsrcEnd,

    char[]dst,

    intdstBegin)

    Charactersarecopiedfromthissequenceintothedestinationcharacterarraydst.ThefirstcharactertobecopiedisatindexsrcBeginthelastcharactertobecopiedisatindexsrcEnd-1.ThetotalnumberofcharacterstobecopiedissrcEnd-srcBegin.ThecharactersarecopiedintothesubarrayofdststartingatindexdstBeginandendingatindex:

    dstbegin + (srcEnd-srcBegin) - 1

    Parameters:

    srcBeginstartcopyingatthisoffset.

    srcEndstopcopyingatthisoffset.

    dstthearraytocopythedatainto.

    dstBeginoffsetintodst.

    Throws:

    NullPointerExceptionifdstisnull.

    IndexOutOfBoundsExceptionifanyofthefollowingistrue:srcBeginisnegativedstBeginisnegativethesrcBeginargumentisgreaterthanthesrcEndargument.srcEndisgreaterthanthis.length().dstBegin+srcEnd-srcBeginisgreaterthandst.length

    setCharAt

    publicvoidsetCharAt(intindex,

    charch)

    Thecharacteratthespecifiedindexissettoch.Thissequenceisalteredtorepresentanewcharactersequencethatisidenticaltotheoldcharactersequence,exceptthatitcontainsthecharacterchatpositionindex.

    Theindexargumentmustbegreaterthanorequalto0,andlessthanthelengthofthissequence.

    Parameters:

    indextheindexofthecharactertomodify.

    chthenewcharacter.

    Throws:

    IndexOutOfBoundsExceptionifindexisnegativeorgreaterthanorequaltolength().

    SeeAlso:

    length()

  • 6/22/2015 StringBuffer (Java Platform SE 7 )

    http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html 10/26

    append

    publicStringBufferappend(Objectobj)

    AppendsthestringrepresentationoftheObjectargument.

    TheoveralleffectisexactlyasiftheargumentwereconvertedtoastringbythemethodString.valueOf(Object),andthecharactersofthatstringwerethenappendedtothischaractersequence.

    Parameters:

    objanObject.

    Returns:

    areferencetothisobject.

    append

    publicStringBufferappend(Stringstr)

    Appendsthespecifiedstringtothischaractersequence.

    ThecharactersoftheStringargumentareappended,inorder,increasingthelengthofthissequencebythelengthoftheargument.Ifstrisnull,thenthefourcharacters"null"areappended.

    Letnbethelengthofthischaractersequencejustpriortoexecutionoftheappendmethod.Thenthecharacteratindexkinthenewcharactersequenceisequaltothecharacteratindexkintheoldcharactersequence,ifkislessthannotherwise,itisequaltothecharacteratindexknintheargumentstr.

    Parameters:

    strastring.

    Returns:

    areferencetothisobject.

    append

    publicStringBufferappend(StringBuffersb)

    AppendsthespecifiedStringBuffertothissequence.

    ThecharactersoftheStringBufferargumentareappended,inorder,tothecontentsofthisStringBuffer,increasingthelengthofthisStringBufferbythelengthoftheargument.Ifsbisnull,thenthefourcharacters"null"areappendedtothisStringBuffer.

    Letnbethelengthoftheoldcharactersequence,theonecontainedintheStringBufferjustpriortoexecutionoftheappendmethod.Thenthecharacteratindexkinthenewcharactersequenceisequaltothecharacteratindexkintheoldcharactersequence,ifkislessthannotherwise,itisequaltothecharacteratindexknintheargumentsb.

    Thismethodsynchronizesonthis(thedestination)objectbutdoesnotsynchronizeonthesource(sb).

    Parameters:

    sbtheStringBuffertoappend.

    Returns:

    areferencetothisobject.

  • 6/22/2015 StringBuffer (Java Platform SE 7 )

    http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html 11/26

    Since:

    1.4

    append

    publicStringBufferappend(CharSequences)

    AppendsthespecifiedCharSequencetothissequence.

    ThecharactersoftheCharSequenceargumentareappended,inorder,increasingthelengthofthissequencebythelengthoftheargument.

    Theresultofthismethodisexactlythesameasifitwereaninvocationofthis.append(s,0,s.length())

    Thismethodsynchronizesonthis(thedestination)objectbutdoesnotsynchronizeonthesource(s).

    Ifsisnull,thenthefourcharacters"null"areappended.

    Specifiedby:

    appendininterfaceAppendable

    Parameters:

    stheCharSequencetoappend.

    Returns:

    areferencetothisobject.

    Since:

    1.5

    append

    publicStringBufferappend(CharSequences, intstart, intend)

    AppendsasubsequenceofthespecifiedCharSequencetothissequence.

    Charactersofthearguments,startingatindexstart,areappended,inorder,tothecontentsofthissequenceuptothe(exclusive)indexend.Thelengthofthissequenceisincreasedbythevalueofend - start.

    Letnbethelengthofthischaractersequencejustpriortoexecutionoftheappendmethod.Thenthecharacteratindexkinthischaractersequencebecomesequaltothecharacteratindexkinthissequence,ifkislessthannotherwise,itisequaltothecharacteratindexk+startninthearguments.

    Ifsisnull,thenthismethodappendscharactersasifthesparameterwasasequencecontainingthefourcharacters"null".

    Specifiedby:

    appendininterfaceAppendable

    Parameters:

    sthesequencetoappend.

    startthestartingindexofthesubsequencetobeappended.

    endtheendindexofthesubsequencetobeappended.

  • 6/22/2015 StringBuffer (Java Platform SE 7 )

    http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html 12/26

    Returns:

    areferencetothisobject.

    Throws:

    IndexOutOfBoundsExceptionifstartisnegative,orstartisgreaterthanendorendisgreaterthans.length()

    Since:

    1.5

    append

    publicStringBufferappend(char[]str)

    Appendsthestringrepresentationofthechararrayargumenttothissequence.

    Thecharactersofthearrayargumentareappended,inorder,tothecontentsofthissequence.Thelengthofthissequenceincreasesbythelengthoftheargument.

    TheoveralleffectisexactlyasiftheargumentwereconvertedtoastringbythemethodString.valueOf(char[]),andthecharactersofthatstringwerethenappendedtothischaractersequence.

    Parameters:

    strthecharacterstobeappended.

    Returns:

    areferencetothisobject.

    append

    publicStringBufferappend(char[]str,

    intoffset,

    intlen)

    Appendsthestringrepresentationofasubarrayofthechararrayargumenttothissequence.

    Charactersofthechararraystr,startingatindexoffset,areappended,inorder,tothecontentsofthissequence.Thelengthofthissequenceincreasesbythevalueoflen.

    TheoveralleffectisexactlyasiftheargumentswereconvertedtoastringbythemethodString.valueOf(char[],int,int),andthecharactersofthatstringwerethenappendedtothischaractersequence.

    Parameters:

    strthecharacterstobeappended.

    offsettheindexofthefirstchartoappend.

    lenthenumberofcharstoappend.

    Returns:

    areferencetothisobject.

    Throws:

    IndexOutOfBoundsExceptionifoffset < 0orlen < 0oroffset+len > str.length

  • 6/22/2015 StringBuffer (Java Platform SE 7 )

    http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html 13/26

    append

    publicStringBufferappend(booleanb)

    Appendsthestringrepresentationofthebooleanargumenttothesequence.

    TheoveralleffectisexactlyasiftheargumentwereconvertedtoastringbythemethodString.valueOf(boolean),andthecharactersofthatstringwerethenappendedtothischaractersequence.

    Parameters:

    baboolean.

    Returns:

    areferencetothisobject.

    append

    publicStringBufferappend(charc)

    Appendsthestringrepresentationofthecharargumenttothissequence.

    Theargumentisappendedtothecontentsofthissequence.Thelengthofthissequenceincreasesby1.

    TheoveralleffectisexactlyasiftheargumentwereconvertedtoastringbythemethodString.valueOf(char),andthecharacterinthatstringwerethenappendedtothischaractersequence.

    Specifiedby:

    appendininterfaceAppendable

    Parameters:

    cachar.

    Returns:

    areferencetothisobject.

    append

    publicStringBufferappend(inti)

    Appendsthestringrepresentationoftheintargumenttothissequence.

    TheoveralleffectisexactlyasiftheargumentwereconvertedtoastringbythemethodString.valueOf(int),andthecharactersofthatstringwerethenappendedtothischaractersequence.

    Parameters:

    ianint.

    Returns:

    areferencetothisobject.

    appendCodePoint

    publicStringBufferappendCodePoint(intcodePoint)

  • 6/22/2015 StringBuffer (Java Platform SE 7 )

    http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html 14/26

    AppendsthestringrepresentationofthecodePointargumenttothissequence.

    Theargumentisappendedtothecontentsofthissequence.ThelengthofthissequenceincreasesbyCharacter.charCount(codePoint).

    TheoveralleffectisexactlyasiftheargumentwereconvertedtoachararraybythemethodCharacter.toChars(int)andthecharacterinthatarraywerethenappendedtothischaractersequence.

    Parameters:

    codePointaUnicodecodepoint

    Returns:

    areferencetothisobject.

    Since:

    1.5

    append

    publicStringBufferappend(longlng)

    Appendsthestringrepresentationofthelongargumenttothissequence.

    TheoveralleffectisexactlyasiftheargumentwereconvertedtoastringbythemethodString.valueOf(long),andthecharactersofthatstringwerethenappendedtothischaractersequence.

    Parameters:

    lngalong.

    Returns:

    areferencetothisobject.

    append

    publicStringBufferappend(floatf)

    Appendsthestringrepresentationofthefloatargumenttothissequence.

    TheoveralleffectisexactlyasiftheargumentwereconvertedtoastringbythemethodString.valueOf(float),andthecharactersofthatstringwerethenappendedtothischaractersequence.

    Parameters:

    fafloat.

    Returns:

    areferencetothisobject.

    append

    publicStringBufferappend(doubled)

    Appendsthestringrepresentationofthedoubleargumenttothissequence.

    Theoveralleffectisexactlyasiftheargumentwereconvertedtoastringbythemethod

  • 6/22/2015 StringBuffer (Java Platform SE 7 )

    http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html 15/26

    String.valueOf(double),andthecharactersofthatstringwerethenappendedtothischaractersequence.

    Parameters:

    dadouble.

    Returns:

    areferencetothisobject.

    delete

    publicStringBufferdelete(intstart,

    intend)

    Removesthecharactersinasubstringofthissequence.Thesubstringbeginsatthespecifiedstartandextendstothecharacteratindexend - 1ortotheendofthesequenceifnosuchcharacterexists.Ifstartisequaltoend,nochangesaremade.

    Parameters:

    startThebeginningindex,inclusive.

    endTheendingindex,exclusive.

    Returns:

    Thisobject.

    Throws:

    StringIndexOutOfBoundsExceptionifstartisnegative,greaterthanlength(),orgreaterthanend.

    Since:

    1.2

    deleteCharAt

    publicStringBufferdeleteCharAt(intindex)

    Removesthecharatthespecifiedpositioninthissequence.Thissequenceisshortenedbyonechar.

    Note:Ifthecharacteratthegivenindexisasupplementarycharacter,thismethoddoesnotremovetheentirecharacter.Ifcorrecthandlingofsupplementarycharactersisrequired,determinethenumberofcharstoremovebycallingCharacter.charCount(thisSequence.codePointAt(index)),wherethisSequenceisthissequence.

    Parameters:

    indexIndexofchartoremove

    Returns:

    Thisobject.

    Throws:

    StringIndexOutOfBoundsExceptioniftheindexisnegativeorgreaterthanorequaltolength().

    Since:

    1.2

  • 6/22/2015 StringBuffer (Java Platform SE 7 )

    http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html 16/26

    replace

    publicStringBufferreplace(intstart,

    intend,

    Stringstr)

    ReplacesthecharactersinasubstringofthissequencewithcharactersinthespecifiedString.Thesubstringbeginsatthespecifiedstartandextendstothecharacteratindexend - 1ortotheendofthesequenceifnosuchcharacterexists.FirstthecharactersinthesubstringareremovedandthenthespecifiedStringisinsertedatstart.(ThissequencewillbelengthenedtoaccommodatethespecifiedStringifnecessary.)

    Parameters:

    startThebeginningindex,inclusive.

    endTheendingindex,exclusive.

    strStringthatwillreplacepreviouscontents.

    Returns:

    Thisobject.

    Throws:

    StringIndexOutOfBoundsExceptionifstartisnegative,greaterthanlength(),orgreaterthanend.

    Since:

    1.2

    substring

    publicStringsubstring(intstart)

    ReturnsanewStringthatcontainsasubsequenceofcharacterscurrentlycontainedinthischaractersequence.Thesubstringbeginsatthespecifiedindexandextendstotheendofthissequence.

    Parameters:

    startThebeginningindex,inclusive.

    Returns:

    Thenewstring.

    Throws:

    StringIndexOutOfBoundsExceptionifstartislessthanzero,orgreaterthanthelengthofthisobject.

    Since:

    1.2

    subSequence

    publicCharSequencesubSequence(intstart,

    intend)

    Returnsanewcharactersequencethatisasubsequenceofthissequence.

    Aninvocationofthismethodoftheform

  • 6/22/2015 StringBuffer (Java Platform SE 7 )

    http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html 17/26

    sb.subSequence(begin,end)

    behavesinexactlythesamewayastheinvocation

    sb.substring(begin,end)

    ThismethodisprovidedsothatthisclasscanimplementtheCharSequenceinterface.

    Specifiedby:

    subSequenceininterfaceCharSequence

    Parameters:

    startthestartindex,inclusive.

    endtheendindex,exclusive.

    Returns:

    thespecifiedsubsequence.

    Throws:

    IndexOutOfBoundsExceptionifstartorendarenegative,ifendisgreaterthanlength(),orifstartisgreaterthanend

    Since:

    1.4

    substring

    publicStringsubstring(intstart,

    intend)

    ReturnsanewStringthatcontainsasubsequenceofcharacterscurrentlycontainedinthissequence.Thesubstringbeginsatthespecifiedstartandextendstothecharacteratindexend - 1.

    Parameters:

    startThebeginningindex,inclusive.

    endTheendingindex,exclusive.

    Returns:

    Thenewstring.

    Throws:

    StringIndexOutOfBoundsExceptionifstartorendarenegativeorgreaterthanlength(),orstartisgreaterthanend.

    Since:

    1.2

    insert

    publicStringBufferinsert(intindex,

    char[]str,

    intoffset,

    intlen)

  • 6/22/2015 StringBuffer (Java Platform SE 7 )

    http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html 18/26

    Insertsthestringrepresentationofasubarrayofthestrarrayargumentintothissequence.Thesubarraybeginsatthespecifiedoffsetandextendslenchars.Thecharactersofthesubarrayareinsertedintothissequenceatthepositionindicatedbyindex.Thelengthofthissequenceincreasesbylenchars.

    Parameters:

    indexpositionatwhichtoinsertsubarray.

    strAchararray.

    offsettheindexofthefirstcharinsubarraytobeinserted.

    lenthenumberofcharsinthesubarraytobeinserted.

    Returns:

    Thisobject

    Throws:

    StringIndexOutOfBoundsExceptionifindexisnegativeorgreaterthanlength(),oroffsetorlenarenegative,or(offset+len)isgreaterthanstr.length.

    Since:

    1.2

    insert

    publicStringBufferinsert(intoffset,

    Objectobj)

    InsertsthestringrepresentationoftheObjectargumentintothischaractersequence.

    TheoveralleffectisexactlyasifthesecondargumentwereconvertedtoastringbythemethodString.valueOf(Object),andthecharactersofthatstringweretheninsertedintothischaractersequenceattheindicatedoffset.

    Theoffsetargumentmustbegreaterthanorequalto0,andlessthanorequaltothelengthofthissequence.

    Parameters:

    offsettheoffset.

    objanObject.

    Returns:

    areferencetothisobject.

    Throws:

    StringIndexOutOfBoundsExceptioniftheoffsetisinvalid.

    insert

    publicStringBufferinsert(intoffset,

    Stringstr)

    Insertsthestringintothischaractersequence.

    ThecharactersoftheStringargumentareinserted,inorder,intothissequenceattheindicatedoffset,movingupanycharactersoriginallyabovethatpositionandincreasingthelengthofthissequencebythelengthoftheargument.Ifstrisnull,thenthefourcharacters"null"areinsertedintothissequence.

  • 6/22/2015 StringBuffer (Java Platform SE 7 )

    http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html 19/26

    Thecharacteratindexkinthenewcharactersequenceisequalto:

    thecharacteratindexkintheoldcharactersequence,ifkislessthanoffsetthecharacteratindexk-offsetintheargumentstr,ifkisnotlessthanoffsetbutislessthanoffset+str.length()

    thecharacteratindexk-str.length()intheoldcharactersequence,ifkisnotlessthanoffset+str.length()

    Theoffsetargumentmustbegreaterthanorequalto0,andlessthanorequaltothelengthofthissequence.

    Parameters:

    offsettheoffset.

    strastring.

    Returns:

    areferencetothisobject.

    Throws:

    StringIndexOutOfBoundsExceptioniftheoffsetisinvalid.

    insert

    publicStringBufferinsert(intoffset,

    char[]str)

    Insertsthestringrepresentationofthechararrayargumentintothissequence.

    Thecharactersofthearrayargumentareinsertedintothecontentsofthissequenceatthepositionindicatedbyoffset.Thelengthofthissequenceincreasesbythelengthoftheargument.

    TheoveralleffectisexactlyasifthesecondargumentwereconvertedtoastringbythemethodString.valueOf(char[]),andthecharactersofthatstringweretheninsertedintothischaractersequenceattheindicatedoffset.

    Theoffsetargumentmustbegreaterthanorequalto0,andlessthanorequaltothelengthofthissequence.

    Parameters:

    offsettheoffset.

    stracharacterarray.

    Returns:

    areferencetothisobject.

    Throws:

    StringIndexOutOfBoundsExceptioniftheoffsetisinvalid.

    insert

    publicStringBufferinsert(intdstOffset,

    CharSequences)

    InsertsthespecifiedCharSequenceintothissequence.

    ThecharactersoftheCharSequenceargumentareinserted,inorder,intothissequenceattheindicatedoffset,movingupanycharactersoriginallyabovethatpositionandincreasingthelengthofthissequencebythelengthofthearguments.

  • 6/22/2015 StringBuffer (Java Platform SE 7 )

    http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html 20/26

    Theresultofthismethodisexactlythesameasifitwereaninvocationofthisobject'sinsert(dstOffset,s,0,

    s.length())method.

    Ifsisnull,thenthefourcharacters"null"areinsertedintothissequence.

    Parameters:

    dstOffsettheoffset.

    sthesequencetobeinserted

    Returns:

    areferencetothisobject.

    Throws:

    IndexOutOfBoundsExceptioniftheoffsetisinvalid.

    Since:

    1.5

    insert

    publicStringBufferinsert(intdstOffset,

    CharSequences,

    intstart,

    intend)

    InsertsasubsequenceofthespecifiedCharSequenceintothissequence.

    Thesubsequenceoftheargumentsspecifiedbystartandendareinserted,inorder,intothissequenceatthe

    specifieddestinationoffset,movingupanycharactersoriginallyabovethatposition.Thelengthofthissequence

    isincreasedbyend - start.

    Thecharacteratindexkinthissequencebecomesequalto:

    thecharacteratindexkinthissequence,ifkislessthandstOffsetthecharacteratindexk+start-dstOffsetinthearguments,ifkisgreaterthanorequaltodstOffsetbutislessthandstOffset+end-start

    thecharacteratindexk-(end-start)inthissequence,ifkisgreaterthanorequaltodstOffset+end-start

    ThedstOffsetargumentmustbegreaterthanorequalto0,andlessthanorequaltothelengthofthis

    sequence.

    Thestartargumentmustbenonnegative,andnotgreaterthanend.

    Theendargumentmustbegreaterthanorequaltostart,andlessthanorequaltothelengthofs.

    Ifsisnull,thenthismethodinsertscharactersasifthesparameterwasasequencecontainingthefour

    characters"null".

    Parameters:

    dstOffsettheoffsetinthissequence.

    sthesequencetobeinserted.

    startthestartingindexofthesubsequencetobeinserted.

    endtheendindexofthesubsequencetobeinserted.

    Returns:

    areferencetothisobject.

  • 6/22/2015 StringBuffer (Java Platform SE 7 )

    http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html 21/26

    Throws:

    IndexOutOfBoundsExceptionifdstOffsetisnegativeorgreaterthanthis.length(),orstartorendarenegative,orstartisgreaterthanendorendisgreaterthans.length()

    Since:

    1.5

    insert

    publicStringBufferinsert(intoffset,

    booleanb)

    Insertsthestringrepresentationofthebooleanargumentintothissequence.

    TheoveralleffectisexactlyasifthesecondargumentwereconvertedtoastringbythemethodString.valueOf(boolean),andthecharactersofthatstringweretheninsertedintothischaractersequenceattheindicatedoffset.

    Theoffsetargumentmustbegreaterthanorequalto0,andlessthanorequaltothelengthofthissequence.

    Parameters:

    offsettheoffset.

    baboolean.

    Returns:

    areferencetothisobject.

    Throws:

    StringIndexOutOfBoundsExceptioniftheoffsetisinvalid.

    insert

    publicStringBufferinsert(intoffset,

    charc)

    Insertsthestringrepresentationofthecharargumentintothissequence.

    TheoveralleffectisexactlyasifthesecondargumentwereconvertedtoastringbythemethodString.valueOf(char),andthecharacterinthatstringweretheninsertedintothischaractersequenceattheindicatedoffset.

    Theoffsetargumentmustbegreaterthanorequalto0,andlessthanorequaltothelengthofthissequence.

    Parameters:

    offsettheoffset.

    cachar.

    Returns:

    areferencetothisobject.

    Throws:

    IndexOutOfBoundsExceptioniftheoffsetisinvalid.

  • 6/22/2015 StringBuffer (Java Platform SE 7 )

    http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html 22/26

    insert

    publicStringBufferinsert(intoffset,

    inti)

    Insertsthestringrepresentationofthesecondintargumentintothissequence.

    TheoveralleffectisexactlyasifthesecondargumentwereconvertedtoastringbythemethodString.valueOf(int),andthecharactersofthatstringweretheninsertedintothischaractersequenceattheindicatedoffset.

    Theoffsetargumentmustbegreaterthanorequalto0,andlessthanorequaltothelengthofthissequence.

    Parameters:

    offsettheoffset.

    ianint.

    Returns:

    areferencetothisobject.

    Throws:

    StringIndexOutOfBoundsExceptioniftheoffsetisinvalid.

    insert

    publicStringBufferinsert(intoffset,

    longl)

    Insertsthestringrepresentationofthelongargumentintothissequence.

    TheoveralleffectisexactlyasifthesecondargumentwereconvertedtoastringbythemethodString.valueOf(long),andthecharactersofthatstringweretheninsertedintothischaractersequenceattheindicatedoffset.

    Theoffsetargumentmustbegreaterthanorequalto0,andlessthanorequaltothelengthofthissequence.

    Parameters:

    offsettheoffset.

    lalong.

    Returns:

    areferencetothisobject.

    Throws:

    StringIndexOutOfBoundsExceptioniftheoffsetisinvalid.

    insert

    publicStringBufferinsert(intoffset,

    floatf)

    Insertsthestringrepresentationofthefloatargumentintothissequence.

    TheoveralleffectisexactlyasifthesecondargumentwereconvertedtoastringbythemethodString.valueOf(float),andthecharactersofthatstringweretheninsertedintothischaractersequenceat

  • 6/22/2015 StringBuffer (Java Platform SE 7 )

    http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html 23/26

    theindicatedoffset.

    Theoffsetargumentmustbegreaterthanorequalto0,andlessthanorequaltothelengthofthissequence.

    Parameters:

    offsettheoffset.

    fafloat.

    Returns:

    areferencetothisobject.

    Throws:

    StringIndexOutOfBoundsExceptioniftheoffsetisinvalid.

    insert

    publicStringBufferinsert(intoffset,

    doubled)

    Insertsthestringrepresentationofthedoubleargumentintothissequence.

    TheoveralleffectisexactlyasifthesecondargumentwereconvertedtoastringbythemethodString.valueOf(double),andthecharactersofthatstringweretheninsertedintothischaractersequenceattheindicatedoffset.

    Theoffsetargumentmustbegreaterthanorequalto0,andlessthanorequaltothelengthofthissequence.

    Parameters:

    offsettheoffset.

    dadouble.

    Returns:

    areferencetothisobject.

    Throws:

    StringIndexOutOfBoundsExceptioniftheoffsetisinvalid.

    indexOf

    publicintindexOf(Stringstr)

    Returnstheindexwithinthisstringofthefirstoccurrenceofthespecifiedsubstring.Theintegerreturnedisthesmallestvalueksuchthat:

    this.toString().startsWith(str, k)

    istrue.

    Parameters:

    stranystring.

    Returns:

    ifthestringargumentoccursasasubstringwithinthisobject,thentheindexofthefirstcharacterofthefirst

  • 6/22/2015 StringBuffer (Java Platform SE 7 )

    http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html 24/26

    suchsubstringisreturnedifitdoesnotoccurasasubstring,-1isreturned.

    Throws:

    NullPointerExceptionifstrisnull.

    Since:

    1.4

    indexOf

    publicintindexOf(Stringstr,

    intfromIndex)

    Returnstheindexwithinthisstringofthefirstoccurrenceofthespecifiedsubstring,startingatthespecifiedindex.Theintegerreturnedisthesmallestvaluekforwhich:

    k >= Math.min(fromIndex, this.length()) &&

    this.toString().startsWith(str, k)

    Ifnosuchvalueofkexists,then1isreturned.

    Parameters:

    strthesubstringforwhichtosearch.

    fromIndextheindexfromwhichtostartthesearch.

    Returns:

    theindexwithinthisstringofthefirstoccurrenceofthespecifiedsubstring,startingatthespecifiedindex.

    Throws:

    NullPointerExceptionifstrisnull.

    Since:

    1.4

    lastIndexOf

    publicintlastIndexOf(Stringstr)

    Returnstheindexwithinthisstringoftherightmostoccurrenceofthespecifiedsubstring.Therightmostemptystring""isconsideredtooccurattheindexvaluethis.length().Thereturnedindexisthelargestvalueksuchthat

    this.toString().startsWith(str, k)

    istrue.

    Parameters:

    strthesubstringtosearchfor.

    Returns:

    ifthestringargumentoccursoneormoretimesasasubstringwithinthisobject,thentheindexofthefirstcharacterofthelastsuchsubstringisreturned.Ifitdoesnotoccurasasubstring,-1isreturned.

  • 6/22/2015 StringBuffer (Java Platform SE 7 )

    http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html 25/26

    Throws:

    NullPointerExceptionifstrisnull.

    Since:

    1.4

    lastIndexOf

    publicintlastIndexOf(Stringstr,

    intfromIndex)

    Returnstheindexwithinthisstringofthelastoccurrenceofthespecifiedsubstring.Theintegerreturnedisthelargestvalueksuchthat:

    k

  • 6/22/2015 StringBuffer (Java Platform SE 7 )

    http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html 26/26

    Overview Package Class Use Tree Deprecated Index HelpJavaPlatformStandardEd.7

    PrevClass NextClass Frames NoFrames AllClasses

    Summary:Nested|Field|Constr|Method Detail:Field|Constr|Method

    SubmitabugorfeatureForfurtherAPIreferenceanddeveloperdocumentation,seeJavaSEDocumentation.Thatdocumentationcontainsmoredetailed,developertargeteddescriptions,withconceptualoverviews,definitionsofterms,workarounds,andworkingcodeexamples.Copyright1993,2014,Oracleand/oritsaffiliates.Allrightsreserved.

    toString

    publicStringtoString()

    Returnsastringrepresentingthedatainthissequence.AnewStringobjectisallocatedandinitializedtocontainthecharactersequencecurrentlyrepresentedbythisobject.ThisStringisthenreturned.SubsequentchangestothissequencedonotaffectthecontentsoftheString.

    Specifiedby:

    toStringininterfaceCharSequence

    Returns:

    astringrepresentationofthissequenceofcharacters.