Class StringBuffer
- All Implemented Interfaces:
Serializable,Appendable,CharSequence,Comparable<StringBuffer>
String, but can be modified. At anypoint in time it contains some particular sequence of characters, butthe length and content of the sequence can be changed through certainmethod calls.String buffers are safe for use by multiple threads. The methodsare synchronized where necessary so that all the operations on anyparticular instance behave as if they occur in some serial orderthat is consistent with the order of the method calls made by each ofthe individual threads involved.
The principal operations on aStringBuffer are theappend andinsert methods, which areoverloaded so as to accept data of any type. Each effectivelyconverts a given datum to a string and then appends or inserts thecharacters of that string to the string buffer. Theappend method always adds these characters at the endof the buffer; theinsert method adds the characters ata specified point.
For example, ifz refers to a string buffer objectwhose current contents are"start", thenthe method callz.append("le") would cause the stringbuffer to contain"startle", whereasz.insert(4, "le") would alter the string buffer tocontain"starlet".
In general, if sb refers to an instance of aStringBuffer,thensb.append(x) has the same effect assb.insert(sb.length(), x).
Whenever an operation occurs involving a source sequence (such asappending or inserting from a source sequence), this class synchronizesonly on the string buffer performing the operation, not on the source.Note that whileStringBuffer is designed to be safe to useconcurrently from multiple threads, if the constructor or theappend orinsert operation is passed a source sequencethat is shared across threads, the calling code must ensurethat the operation has a consistent and unchanging view of the sourcesequence for the duration of the operation.This could be satisfied by the caller holding a lock during theoperation's call, by using an immutable source sequence, or by notsharing the source sequence across threads.
Every string buffer has a capacity. As long as the length of thecharacter sequence contained in the string buffer does not exceedthe capacity, it is not necessary to allocate a new internalbuffer array. If the internal buffer overflows, it isautomatically made larger.
Unless otherwise noted, passing anull argument to a constructoror method in this class will cause aNullPointerException to bethrown.
As of release JDK 5, this class has been supplemented with an equivalentclass designed for use by a single thread,StringBuilder. TheStringBuilder class should generally be used in preference tothis one, as it supports all of the same operations but it is faster, asit performs no synchronization.
- API Note:
StringBufferimplementsComparablebut does not overrideequals. Thus, the natural ordering ofStringBufferis inconsistent with equals. Care should be exercised ifStringBufferobjects are used as keys in aSortedMapor elements in aSortedSet.SeeComparable,SortedMap, orSortedSetfor more information.- Since:
- 1.0
- See Also:
Constructor Summary
ConstructorsConstructorDescriptionConstructs a string buffer with no characters in it and aninitial capacity of 16 characters.StringBuffer(int capacity) Constructs a string buffer with no characters in it andthe specified initial capacity.StringBuffer(CharSequence seq) Constructs a string buffer that contains the same charactersas the specifiedCharSequence.StringBuffer(String str) Constructs a string buffer initialized to the contents of thespecified string.Method Summary
Modifier and TypeMethodDescriptionappend(boolean b) Appends the string representation of thebooleanargument to the sequence.append(char c) Appends the string representation of thecharargument to this sequence.append(char[] str) Appends the string representation of thechararrayargument to this sequence.append(char[] str, int offset, int len) Appends the string representation of a subarray of thechararray argument to this sequence.append(double d) Appends the string representation of thedoubleargument to this sequence.append(float f) Appends the string representation of thefloatargument to this sequence.append(int i) Appends the string representation of theintargument to this sequence.append(long lng) Appends the string representation of thelongargument to this sequence.Appends the specifiedCharSequenceto thissequence.append(CharSequence s, int start, int end) Appends a subsequence of the specifiedCharSequenceto thissequence.Appends the string representation of theObjectargument.Appends the specified string to this character sequence.append(StringBuffer sb) Appends the specifiedStringBufferto this sequence.appendCodePoint(int codePoint) Appends the string representation of thecodePointargument to this sequence.intcapacity()Returns the current capacity.charcharAt(int index) Returns thecharvalue in this sequence at the specified index.chars()Returns a stream ofintzero-extending thecharvaluesfrom this sequence.intcodePointAt(int index) Returns the character (Unicode code point) at the specifiedindex.intcodePointBefore(int index) Returns the character (Unicode code point) before the specifiedindex.intcodePointCount(int beginIndex, int endIndex) Returns the number of Unicode code points in the specified textrange of this sequence.Returns a stream of code point values from this sequence.intcompareTo(StringBuffer another) Compares twoStringBufferinstances lexicographically.delete(int start, int end) Removes the characters in a substring of this sequence.deleteCharAt(int index) Removes thecharat the specified position in thissequence.voidensureCapacity(int minimumCapacity) Ensures that the capacity is at least equal to the specified minimum.voidgetChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) Copies characters from this sequence into the given destination array.intReturns the index within this string of the first occurrence of thespecified substring.intReturns the index within this string of the first occurrence of thespecified substring, starting at the specified index.insert(int offset, boolean b) Inserts the string representation of thebooleanargument into this sequence.insert(int offset, char c) Inserts the string representation of thecharargument into this sequence.insert(int offset, char[] str) Inserts the string representation of thechararrayargument into this sequence.insert(int index, char[] str, int offset, int len) Inserts the string representation of a subarray of thestrarray argument into this sequence.insert(int offset, double d) Inserts the string representation of thedoubleargument into this sequence.insert(int offset, float f) Inserts the string representation of thefloatargument into this sequence.insert(int offset, int i) Inserts the string representation of the secondintargument into this sequence.insert(int offset, long l) Inserts the string representation of thelongargument into this sequence.insert(int dstOffset,CharSequence s) Inserts the specifiedCharSequenceinto this sequence.insert(int dstOffset,CharSequence s, int start, int end) Inserts a subsequence of the specifiedCharSequenceintothis sequence.Inserts the string representation of theObjectargument into this character sequence.Inserts the string into this character sequence.intlastIndexOf(String str) Returns the index within this string of the last occurrence of thespecified substring.intlastIndexOf(String str, int fromIndex) Returns the index within this string of the last occurrence of thespecified substring, searching backward starting at the specified index.intlength()Returns the length (character count).intoffsetByCodePoints(int index, int codePointOffset) Returns the index within this sequence that is offset from thegivenindexbycodePointOffsetcodepoints.repeat(int codePoint, int count) Repeatscountcopies of the string representation of thecodePointargument to this sequence.repeat(CharSequence cs, int count) Appendscountcopies of the specifiedCharSequencecsto this sequence.Replaces the characters in a substring of this sequencewith characters in the specifiedString.reverse()Causes this character sequence to be replaced by the reverse ofthe sequence.voidsetCharAt(int index, char ch) The character at the specified index is set toch.voidsetLength(int newLength) Sets the length of the character sequence.subSequence(int start, int end) Returns a character sequence that is a subsequence of this sequence.substring(int start) Returns aStringthat contains a subsequence ofcharacters currently contained in this character sequence.substring(int start, int end) Returns aStringthat contains a subsequence ofcharacters currently contained in this sequence.toString()Returns a string representing the data in this sequence.voidAttempts to reduce storage used for the character sequence.Methods declared in class Object
clone,equals,finalize,getClass,hashCode,notify,notifyAll,wait,wait,waitMethods declared in interface CharSequence
chars,codePoints,isEmpty
Constructor Details
StringBuffer
public StringBuffer()Constructs a string buffer with no characters in it and aninitial capacity of 16 characters.StringBuffer
public StringBuffer(int capacity) Constructs a string buffer with no characters in it andthe specified initial capacity.- Parameters:
capacity- the initial capacity.- Throws:
NegativeArraySizeException- if thecapacityargument is less than0.
StringBuffer
Constructs a string buffer initialized to the contents of thespecified string. The initial capacity of the string buffer is16plus the length of the string argument.- Parameters:
str- the initial contents of the buffer.
StringBuffer
Constructs a string buffer that contains the same charactersas the specifiedCharSequence. The initial capacity ofthe string buffer is16plus the length of theCharSequenceargument.- Parameters:
seq- the sequence to copy.- Since:
- 1.5
Method Details
compareTo
Compares twoStringBufferinstances lexicographically. This methodfollows the same rules for lexicographical comparison as defined in theCharSequence.compare(this, another) method.For finer-grained, locale-sensitive String comparison, refer to
Collator.- Specified by:
compareToin interfaceComparable<StringBuffer>- Implementation Note:
- This method synchronizes on
this, the current object, but notStringBuffer anotherwith whichthis StringBufferis compared. - Parameters:
another- theStringBufferto be compared with- Returns:
- the value
0if thisStringBuffercontains the samecharacter sequence as that of the argumentStringBuffer; a negative integerif thisStringBufferis lexicographically less than theStringBufferargument; or a positive integer if thisStringBufferis lexicographically greater than theStringBufferargument. - Since:
- 11
length
public int length()Returns the length (character count).- Specified by:
lengthin interfaceCharSequence- Returns:
- the length of the sequence of characters currently represented by this object
capacity
public int capacity()Returns the current capacity. The capacity is the number of charactersthat can be stored (including already written characters), beyond whichan allocation will occur.- Returns:
- the current capacity
ensureCapacity
public void ensureCapacity(int minimumCapacity) Ensures that the capacity is at least equal to the specified minimum.If the current capacity is less than the argument, then a new internalarray is allocated with greater capacity. The new capacity is thelarger of:- The
minimumCapacityargument. - Twice the old capacity, plus
2.
minimumCapacityargument is nonpositive, thismethod takes no action and simply returns.Note that subsequent operations on this object can reduce theactual capacity below that requested here.- Parameters:
minimumCapacity- the minimum desired capacity.
- The
trimToSize
public void trimToSize()Attempts to reduce storage used for the character sequence.If the buffer is larger than necessary to hold its current sequence ofcharacters, then it may be resized to become more space efficient.Calling this method may, but is not required to, affect the valuereturned by a subsequent call to thecapacity()method.- Since:
- 1.5
setLength
public void setLength(int newLength) Sets the length of the character sequence.The sequence is changed to a new character sequencewhose length is specified by the argument. For every nonnegativeindexk less thannewLength, the character atindexk in the new character sequence is the same as thecharacter at indexk in the old sequence ifk is lessthan the length of the old character sequence; otherwise, it is thenull character'\u0000'.In other words, if thenewLengthargument is less thanthe current length, the length is changed to the specified length.If the
newLengthargument is greater than or equalto the current length, sufficient null characters('\u0000') are appended so thatlength becomes thenewLengthargument.The
newLengthargument must be greater than or equalto0.- Parameters:
newLength- the new length- Throws:
IndexOutOfBoundsException- if thenewLengthargument is negative.- See Also:
charAt
public char charAt(int index) Returns thecharvalue in this sequence at the specified index.The firstcharvalue is at index0, the next at index1, and so on, as in array indexing.The index argument must be greater than or equal to
0, and less than the length of this sequence.If the
charvalue specified by the index is asurrogate, the surrogatevalue is returned.- Specified by:
charAtin interfaceCharSequence- Parameters:
index- the index of the desiredcharvalue.- Returns:
- the
charvalue at the specified index. - Throws:
IndexOutOfBoundsException- ifindexis negative or greater than or equal tolength().- See Also:
codePointAt
public int codePointAt(int index) Returns the character (Unicode code point) at the specifiedindex. The index refers tocharvalues(Unicode code units) and ranges from0toCharSequence.length()- 1.If the
charvalue specified at the given indexis in the high-surrogate range, the following index is lessthan the length of this sequence, and thecharvalue at the following index is in thelow-surrogate range, then the supplementary code pointcorresponding to this surrogate pair is returned. Otherwise,thecharvalue at the given index is returned.- Parameters:
index- the index to thecharvalues- Returns:
- the code point value of the character at the
index - Throws:
IndexOutOfBoundsException- if theindexargument is negative or not less than the length of this sequence.- Since:
- 1.5
codePointBefore
public int codePointBefore(int index) Returns the character (Unicode code point) before the specifiedindex. The index refers tocharvalues(Unicode code units) and ranges from1toCharSequence.length().If the
charvalue at(index - 1)is in the low-surrogate range,(index - 2)is notnegative, and thecharvalue at(index -2)is in the high-surrogate range, then thesupplementary code point value of the surrogate pair isreturned. If thecharvalue atindex -1is an unpaired low-surrogate or a high-surrogate, thesurrogate value is returned.- Parameters:
index- the index following the code point that should be returned- Returns:
- the Unicode code point value before the given index.
- Throws:
IndexOutOfBoundsException- if theindexargument is less than 1 or greater than the length of this sequence.- Since:
- 1.5
codePointCount
public int codePointCount(int beginIndex, int endIndex) Returns the number of Unicode code points in the specified textrange of this sequence. The text range begins at the specifiedbeginIndexand extends to thecharatindexendIndex - 1. Thus the length (inchars) of the text range isendIndex-beginIndex. Unpaired surrogates withinthis sequence count as one code point each.- Parameters:
beginIndex- the index to the firstcharofthe text range.endIndex- the index after the lastcharofthe text range.- Returns:
- the number of Unicode code points in the specified textrange
- Throws:
IndexOutOfBoundsException- if thebeginIndexis negative, orendIndexis larger than the length of this sequence, orbeginIndexis larger thanendIndex.- Since:
- 1.5
offsetByCodePoints
public int offsetByCodePoints(int index, int codePointOffset) Returns the index within this sequence that is offset from thegivenindexbycodePointOffsetcodepoints. Unpaired surrogates within the text range given byindexandcodePointOffsetcount asone code point each.- Parameters:
index- the index to be offsetcodePointOffset- the offset in code points- Returns:
- the index within this sequence
- Throws:
IndexOutOfBoundsException- ifindexis negative or larger than the length of this sequence, or ifcodePointOffsetis positive and the subsequence starting withindexhas fewer thancodePointOffsetcode points, or ifcodePointOffsetis negative and the subsequence beforeindexhas fewer than the absolute value ofcodePointOffsetcode points.- Since:
- 1.5
getChars
public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) Copies characters from this sequence into the given destination array.The first character to be copied is at indexsrcBegin; the lastcharacter to be copied is at indexsrcEnd-1. The total number ofcharacters to be copied issrcEnd-srcBegin. Thecharacters are copied into the subarray ofdststartingat indexdstBeginand ending at index:dstbegin + (srcEnd-srcBegin) - 1- Specified by:
getCharsin interfaceCharSequence- Parameters:
srcBegin- start copying at this offset.srcEnd- stop copying at this offset.dst- the array to copy the data into.dstBegin- offset intodst.
setCharAt
public void setCharAt(int index, char ch) The character at the specified index is set toch. Thissequence is altered to represent a new character sequence that isidentical to the old character sequence, except that it contains thecharacterchat positionindex.The index argument must be greater than or equal to
0, and less than the length of this sequence.- Parameters:
index- the index of the character to modify.ch- the new character.- Throws:
IndexOutOfBoundsException- ifindexis negative or greater than or equal tolength().- See Also:
append
Appends the string representation of theObjectargument.The overall effect is exactly as if the argument were convertedto a string by the method
String.valueOf(Object),and the characters of that string were thenappendedto this character sequence.- Parameters:
obj- anObject.- Returns:
- a reference to this object.
append
Appends the specified string to this character sequence.The characters of the
Stringargument are appended, inorder, increasing the length of this sequence by the length of theargument. Ifstrisnull, then the fourcharacters"null"are appended.Letn be the length of this character sequence just prior toexecution of the
appendmethod. Then the character atindexk in the new character sequence is equal to the characterat indexk in the old character sequence, ifk is lessthann; otherwise, it is equal to the character at indexk-n in the argumentstr.- Parameters:
str- a string.- Returns:
- a reference to this object.
append
Appends the specifiedStringBufferto this sequence.The characters of the
StringBufferargument are appended,in order, to the contents of thisStringBuffer, increasing thelength of thisStringBufferby the length of the argument.Ifsbisnull, then the four characters"null"are appended to thisStringBuffer.Letn be the length of the old character sequence, the onecontained in the
StringBufferjust prior to execution of theappendmethod. Then the character at indexk inthe new character sequence is equal to the character at indexkin the old character sequence, ifk is less thann;otherwise, it is equal to the character at indexk-n in theargumentsb.This method synchronizes on
this, the destinationobject, but does not synchronize on the source (sb).- Parameters:
sb- theStringBufferto append.- Returns:
- a reference to this object.
- Since:
- 1.4
append
Appends the specifiedCharSequenceto thissequence.The characters of the
CharSequenceargument are appended,in order, increasing the length of this sequence by the length of theargument.The result of this method is exactly the same as if it were aninvocation of this.append(s, 0, s.length());
This method synchronizes on
this, the destinationobject, but does not synchronize on the source (s).If
sisnull, then the four characters"null"are appended.- Specified by:
appendin interfaceAppendable- Parameters:
s- theCharSequenceto append.- Returns:
- a reference to this object.
- Since:
- 1.5
append
Appends a subsequence of the specifiedCharSequenceto thissequence.Characters of the argument
s, starting atindexstart, are appended, in order, to the contents ofthis sequence up to the (exclusive) indexend. The lengthof this sequence is increased by the value ofend - start.Letn be the length of this character sequence just prior toexecution of the
appendmethod. Then the character atindexk in this character sequence becomes equal to thecharacter at indexk in this sequence, ifk is less thann; otherwise, it is equal to the character at indexk+start-n in the arguments.If
sisnull, then this method appendscharacters as if the s parameter was a sequence containing the fourcharacters"null".The contents are unspecified if the
CharSequenceis modified during the method call or an exception is thrownwhen accessing theCharSequence.- Specified by:
appendin interfaceAppendable- Parameters:
s- the sequence to append.start- the starting index of the subsequence to be appended.end- the end index of the subsequence to be appended.- Returns:
- a reference to this object.
- Throws:
IndexOutOfBoundsException- ifstartis negative, orstartis greater thanendorendis greater thans.length()- Since:
- 1.5
append
Appends the string representation of thechararrayargument to this sequence.The characters of the array argument are appended, in order, tothe contents of this sequence. The length of this sequenceincreases by the length of the argument.
The overall effect is exactly as if the argument were convertedto a string by the method
String.valueOf(char[]),and the characters of that string were thenappendedto this character sequence.- Parameters:
str- the characters to be appended.- Returns:
- a reference to this object.
append
Appends the string representation of a subarray of thechararray argument to this sequence.Characters of the
chararraystr, starting atindexoffset, are appended, in order, to the contentsof this sequence. The length of this sequence increasesby the value oflen.The overall effect is exactly as if the arguments were convertedto a string by the method
String.valueOf(char[],int,int),and the characters of that string were thenappendedto this character sequence.- Parameters:
str- the characters to be appended.offset- the index of the firstcharto append.len- the number ofchars to append.- Returns:
- a reference to this object.
- Throws:
IndexOutOfBoundsException- ifoffset < 0orlen < 0oroffset+len > str.length
append
Appends the string representation of thebooleanargument to the sequence.The overall effect is exactly as if the argument were convertedto a string by the method
String.valueOf(boolean),and the characters of that string were thenappendedto this character sequence.- Parameters:
b- aboolean.- Returns:
- a reference to this object.
append
Appends the string representation of thecharargument to this sequence.The argument is appended to the contents of this sequence.The length of this sequence increases by
1.The overall effect is exactly as if the argument were convertedto a string by the method
String.valueOf(char),and the character in that string were thenappendedto this character sequence.- Specified by:
appendin interfaceAppendable- Parameters:
c- achar.- Returns:
- a reference to this object.
append
Appends the string representation of theintargument to this sequence.The overall effect is exactly as if the argument were convertedto a string by the method
String.valueOf(int),and the characters of that string were thenappendedto this character sequence.- Parameters:
i- anint.- Returns:
- a reference to this object.
appendCodePoint
Appends the string representation of thecodePointargument to this sequence.The argument is appended to the contents of this sequence.The length of this sequence increases by
Character.charCount(codePoint).The overall effect is exactly as if the argument wereconverted to a
chararray by the methodCharacter.toChars(int)and the character in that arraywere thenappendedto this charactersequence.- Parameters:
codePoint- a Unicode code point- Returns:
- a reference to this object.
- Since:
- 1.5
append
Appends the string representation of thelongargument to this sequence.The overall effect is exactly as if the argument were convertedto a string by the method
String.valueOf(long),and the characters of that string were thenappendedto this character sequence.- Parameters:
lng- along.- Returns:
- a reference to this object.
append
Appends the string representation of thefloatargument to this sequence.The overall effect is exactly as if the argument were convertedto a string by the method
String.valueOf(float),and the characters of that string were thenappendedto this character sequence.- Parameters:
f- afloat.- Returns:
- a reference to this object.
append
Appends the string representation of thedoubleargument to this sequence.The overall effect is exactly as if the argument were convertedto a string by the method
String.valueOf(double),and the characters of that string were thenappendedto this character sequence.- Parameters:
d- adouble.- Returns:
- a reference to this object.
delete
Removes the characters in a substring of this sequence.The substring begins at the specifiedstartand extends tothe character at indexend - 1or to the end of thesequence if no such character exists. Ifstartis equal toend, no changes are made.- Parameters:
start- The beginning index, inclusive.end- The ending index, exclusive.- Returns:
- This object.
- Throws:
StringIndexOutOfBoundsException- ifstartis negative, greater thanlength(), or greater thanend.- Since:
- 1.2
deleteCharAt
Removes thecharat the specified position in thissequence. This sequence is shortened by onechar.Note: If the character at the given index is a supplementarycharacter, this method does not remove the entire character. Ifcorrect handling of supplementary characters is required,determine the number of
chars to remove by callingCharacter.charCount(thisSequence.codePointAt(index)),wherethisSequenceis this sequence.- Parameters:
index- Index ofcharto remove- Returns:
- This object.
- Throws:
StringIndexOutOfBoundsException- if theindexis negative or greater than or equal tolength().- Since:
- 1.2
replace
Replaces the characters in a substring of this sequencewith characters in the specifiedString. The substringbegins at the specifiedstartand extends to the characterat indexend - 1or to the end of thesequence if no such character exists. First thecharacters in the substring are removed and then the specifiedStringis inserted atstart. (Thissequence will be lengthened to accommodate thespecified String if necessary.)- Parameters:
start- The beginning index, inclusive.end- The ending index, exclusive.str- String that will replace previous contents.- Returns:
- This object.
- Throws:
StringIndexOutOfBoundsException- ifstartis negative, greater thanlength(), or greater thanend.- Since:
- 1.2
substring
Returns aStringthat contains a subsequence ofcharacters currently contained in this character sequence. Thesubstring begins at the specified index and extends to the end ofthis sequence.- Parameters:
start- The beginning index, inclusive.- Returns:
- A string containing the specified subsequence of characters.
- Throws:
StringIndexOutOfBoundsException- ifstartis less than zero, or greater than the length of this object.- Since:
- 1.2
subSequence
Returns a character sequence that is a subsequence of this sequence.An invocation of this method of the form
behaves in exactly the same way as the invocationsb.subSequence(begin, end)
This method is provided so that this class canimplement thesb.substring(begin, end)CharSequenceinterface.- Specified by:
subSequencein interfaceCharSequence- Parameters:
start- the start index, inclusive.end- the end index, exclusive.- Returns:
- the specified subsequence.
- Throws:
IndexOutOfBoundsException- ifstartorendare negative, ifendis greater thanlength(), or ifstartis greater thanend- Since:
- 1.4
substring
Returns aStringthat contains a subsequence ofcharacters currently contained in this sequence. Thesubstring begins at the specifiedstartandextends to the character at indexend - 1.- Parameters:
start- The beginning index, inclusive.end- The ending index, exclusive.- Returns:
- A string containing the specified subsequence of characters.
- Throws:
StringIndexOutOfBoundsException- ifstartorendare negative or greater thanlength(), orstartis greater thanend.- Since:
- 1.2
insert
Inserts the string representation of a subarray of thestrarray argument into this sequence. The subarray begins at thespecifiedoffsetand extendslenchars.The characters of the subarray are inserted into this sequence atthe position indicated byindex. The length of thissequence increases bylenchars.- Parameters:
index- position at which to insert subarray.str- Achararray.offset- the index of the firstcharin subarray to be inserted.len- the number ofchars in the subarray to be inserted.- Returns:
- This object
- Throws:
StringIndexOutOfBoundsException- ifindexis negative or greater thanlength(), oroffsetorlenare negative, or(offset+len)is greater thanstr.length.- Since:
- 1.2
insert
Inserts the string representation of theObjectargument into this character sequence.The overall effect is exactly as if the second argument wereconverted to a string by the method
String.valueOf(Object),and the characters of that string were theninsertedinto this charactersequence at the indicated offset.The
offsetargument must be greater than or equal to0, and less than or equal to thelengthof this sequence.- Parameters:
offset- the offset.obj- anObject.- Returns:
- a reference to this object.
- Throws:
StringIndexOutOfBoundsException- if the offset is invalid.
insert
Inserts the string into this character sequence.The characters of the
Stringargument are inserted, inorder, into this sequence at the indicated offset, moving up anycharacters originally above that position and increasing the lengthof this sequence by the length of the argument. Ifstrisnull, then the four characters"null"are inserted into this sequence.The character at indexk in the new character sequence isequal to:
- the character at indexk in the old character sequence, ifk is less than
offset - the character at indexk
-offsetin theargumentstr, ifk is not less thanoffsetbut is less thanoffset+str.length() - the character at indexk
-str.length()in theold character sequence, ifk is not less thanoffset+str.length()
The
offsetargument must be greater than or equal to0, and less than or equal to thelengthof this sequence.- Parameters:
offset- the offset.str- a string.- Returns:
- a reference to this object.
- Throws:
StringIndexOutOfBoundsException- if the offset is invalid.
- the character at indexk in the old character sequence, ifk is less than
insert
Inserts the string representation of thechararrayargument into this sequence.The characters of the array argument are inserted into thecontents of this sequence at the position indicated by
offset. The length of this sequence increases bythe length of the argument.The overall effect is exactly as if the second argument wereconverted to a string by the method
String.valueOf(char[]),and the characters of that string were theninsertedinto this charactersequence at the indicated offset.The
offsetargument must be greater than or equal to0, and less than or equal to thelengthof this sequence.- Parameters:
offset- the offset.str- a character array.- Returns:
- a reference to this object.
- Throws:
StringIndexOutOfBoundsException- if the offset is invalid.
insert
Inserts the specifiedCharSequenceinto this sequence.The characters of the
CharSequenceargument are inserted,in order, into this sequence at the indicated offset, moving upany characters originally above that position and increasing the lengthof this sequence by the length of the argument s.The result of this method is exactly the same as if it were aninvocation of this object's
insert(dstOffset, s, 0, s.length())method.The contents are unspecified if the
CharSequenceis modified during the method call or an exception is thrownwhen accessing theCharSequence.If
sisnull, then the four characters"null"are inserted into this sequence.- Parameters:
dstOffset- the offset.s- the sequence to be inserted- Returns:
- a reference to this object.
- Throws:
IndexOutOfBoundsException- if the offset is invalid.- Since:
- 1.5
insert
Inserts a subsequence of the specifiedCharSequenceintothis sequence.The subsequence of the argument
sspecified bystartandendare inserted,in order, into this sequence at the specified destination offset, movingup any characters originally above that position. The length of thissequence is increased byend - start.The character at indexk in this sequence becomes equal to:
- the character at indexk in this sequence, ifk is less than
dstOffset - the character at indexk
+start-dstOffsetinthe arguments, ifk is greater than or equal todstOffsetbut is less thandstOffset+end-start - the character at indexk
-(end-start)in thissequence, ifk is greater than or equal todstOffset+end-start
The
dstOffsetargument must be greater than or equal to0, and less than or equal to thelengthof this sequence.The start argument must be non-negative, and not greater than
end.The end argument must be greater than or equal to
start, and less than or equal to the length of s.If
sisnull, then this method insertscharacters as if the s parameter was a sequence containing the fourcharacters"null".The contents are unspecified if the
CharSequenceis modified during the method call or an exception is thrownwhen accessing theCharSequence.- Parameters:
dstOffset- the offset in this sequence.s- the sequence to be inserted.start- the starting index of the subsequence to be inserted.end- the end index of the subsequence to be inserted.- Returns:
- a reference to this object.
- Throws:
IndexOutOfBoundsException- ifdstOffsetis negative or greater thanthis.length(), orstartorendare negative, orstartis greater thanendorendis greater thans.length()- Since:
- 1.5
- the character at indexk in this sequence, ifk is less than
insert
Inserts the string representation of thebooleanargument into this sequence.The overall effect is exactly as if the second argument wereconverted to a string by the method
String.valueOf(boolean),and the characters of that string were theninsertedinto this charactersequence at the indicated offset.The
offsetargument must be greater than or equal to0, and less than or equal to thelengthof this sequence.- Parameters:
offset- the offset.b- aboolean.- Returns:
- a reference to this object.
- Throws:
StringIndexOutOfBoundsException- if the offset is invalid.
insert
Inserts the string representation of thecharargument into this sequence.The overall effect is exactly as if the second argument wereconverted to a string by the method
String.valueOf(char),and the character in that string were theninsertedinto this charactersequence at the indicated offset.The
offsetargument must be greater than or equal to0, and less than or equal to thelengthof this sequence.- Parameters:
offset- the offset.c- achar.- Returns:
- a reference to this object.
- Throws:
IndexOutOfBoundsException- if the offset is invalid.
insert
Inserts the string representation of the secondintargument into this sequence.The overall effect is exactly as if the second argument wereconverted to a string by the method
String.valueOf(int),and the characters of that string were theninsertedinto this charactersequence at the indicated offset.The
offsetargument must be greater than or equal to0, and less than or equal to thelengthof this sequence.- Parameters:
offset- the offset.i- anint.- Returns:
- a reference to this object.
- Throws:
StringIndexOutOfBoundsException- if the offset is invalid.
insert
Inserts the string representation of thelongargument into this sequence.The overall effect is exactly as if the second argument wereconverted to a string by the method
String.valueOf(long),and the characters of that string were theninsertedinto this charactersequence at the indicated offset.The
offsetargument must be greater than or equal to0, and less than or equal to thelengthof this sequence.- Parameters:
offset- the offset.l- along.- Returns:
- a reference to this object.
- Throws:
StringIndexOutOfBoundsException- if the offset is invalid.
insert
Inserts the string representation of thefloatargument into this sequence.The overall effect is exactly as if the second argument wereconverted to a string by the method
String.valueOf(float),and the characters of that string were theninsertedinto this charactersequence at the indicated offset.The
offsetargument must be greater than or equal to0, and less than or equal to thelengthof this sequence.- Parameters:
offset- the offset.f- afloat.- Returns:
- a reference to this object.
- Throws:
StringIndexOutOfBoundsException- if the offset is invalid.
insert
Inserts the string representation of thedoubleargument into this sequence.The overall effect is exactly as if the second argument wereconverted to a string by the method
String.valueOf(double),and the characters of that string were theninsertedinto this charactersequence at the indicated offset.The
offsetargument must be greater than or equal to0, and less than or equal to thelengthof this sequence.- Parameters:
offset- the offset.d- adouble.- Returns:
- a reference to this object.
- Throws:
StringIndexOutOfBoundsException- if the offset is invalid.
indexOf
Returns the index within this string of the first occurrence of thespecified substring.The returned index is the smallest value
kfor which:
If no such value ofthis.toString().startsWith(str, k)kexists, then-1is returned.- Parameters:
str- the substring to search for.- Returns:
- the index of the first occurrence of the specified substring, or
-1if there is no such occurrence. - Since:
- 1.4
indexOf
Returns the index within this string of the first occurrence of thespecified substring, starting at the specified index.The returned index is the smallest value
kfor which:
If no such value ofk >= Math.min(fromIndex, this.length()) && this.toString().startsWith(str, k)kexists, then-1is returned.- Parameters:
str- the substring to search for.fromIndex- the index from which to start the search.- Returns:
- the index of the first occurrence of the specified substring, starting at the specified index, or
-1if there is no such occurrence. - Since:
- 1.4
lastIndexOf
Returns the index within this string of the last occurrence of thespecified substring. The last occurrence of the empty string "" isconsidered to occur at the index valuethis.length().The returned index is the largest value
kfor which:
If no such value ofthis.toString().startsWith(str, k)kexists, then-1is returned.- Parameters:
str- the substring to search for.- Returns:
- the index of the last occurrence of the specified substring, or
-1if there is no such occurrence. - Since:
- 1.4
lastIndexOf
Returns the index within this string of the last occurrence of thespecified substring, searching backward starting at the specified index.The returned index is the largest value
kfor which:
If no such value ofk <= Math.min(fromIndex, this.length()) && this.toString().startsWith(str, k)kexists, then-1is returned.- Parameters:
str- the substring to search for.fromIndex- the index to start the search from.- Returns:
- the index of the last occurrence of the specified substring, searching backward from the specified index, or
-1if there is no such occurrence. - Since:
- 1.4
reverse
Causes this character sequence to be replaced by the reverse ofthe sequence. If there are any surrogate pairs included in thesequence, these are treated as single characters for thereverse operation. Thus, the order of the high-low surrogatesis never reversed.Letn be the character length of this character sequence(not the length incharvalues) just prior toexecution of thereversemethod. Then thecharacter at indexk in the new character sequence isequal to the character at indexn-k-1 in the oldcharacter sequence.Note that the reverse operation may result in producingsurrogate pairs that were unpaired low-surrogates andhigh-surrogates before the operation. For example, reversing"\uDC00\uD800" produces "\uD800\uDC00" which isa valid surrogate pair.
- Returns:
- a reference to this object.
- Since:
- 1.0.2
repeat
Repeatscountcopies of the string representation of thecodePointargument to this sequence.The length of this sequence increases by
counttimes thestring representation length.It is usual to use
charexpressions for code points. For example:// insert 10 asterisks into the buffersb.repeat('*', 10);- Parameters:
codePoint- code point to appendcount- number of times to copy- Returns:
- a reference to this object.
- Throws:
IllegalArgumentException- if the specifiedcodePointis not a valid Unicode code point or ifcountis negative.- Since:
- 21
repeat
Appendscountcopies of the specifiedCharSequencecsto this sequence.The length of this sequence increases by
counttimes theCharSequencelength.If
csisnull, then the four characters"null"are repeated into this sequence.The contents are unspecified if the
CharSequenceis modified during the method call or an exception is thrownwhen accessing theCharSequence.- Parameters:
cs- aCharSequencecount- number of times to copy- Returns:
- a reference to this object.
- Throws:
IllegalArgumentException- ifcountis negative- Since:
- 21
toString
Returns a string representing the data in this sequence.TheStringobject that is returned contains the charactersequence currently represented by this object. Subsequentchanges to this sequence do not affect the contents of thereturnedString.- Specified by:
toStringin interfaceCharSequence- Returns:
- a string representation of this sequence of characters.
chars
Returns a stream ofintzero-extending thecharvaluesfrom this sequence. Any char which maps to asurrogate code point is passedthrough uninterpreted.The stream binds to this sequence when the terminal stream operationcommences (specifically, for mutable sequences the spliterator for thestream islate-binding).If the sequence is modified during that operation then the result isundefined.
- Specified by:
charsin interfaceCharSequence- Returns:
- an IntStream of char values from this sequence
- Since:
- 9
codePoints
Returns a stream of code point values from this sequence. Any surrogatepairs encountered in the sequence are combined as if byCharacter.toCodePoint and the result is passedto the stream. Any other code units, including ordinary BMP characters,unpaired surrogates, and undefined code units, are zero-extended tointvalues which are then passed to the stream.The stream binds to this sequence when the terminal stream operationcommences (specifically, for mutable sequences the spliterator for thestream islate-binding).If the sequence is modified during that operation then the result isundefined.
- Specified by:
codePointsin interfaceCharSequence- Returns:
- an IntStream of Unicode code points from this sequence
- Since:
- 9