- java.lang.Object
-
- java.lang.StringBuffer
-
- All implemented interfaces
-
Serializable,Appendable,CharSequence,Comparable<StringBuffer>
public final class StringBuffer extends Object implements Serializable, Comparable<StringBuffer>, CharSequence
A thread-safe, mutable sequence of characters. A string buffer is similar toString, but can be modified. At any point in time it contains some specific sequence of characters, but the length and content of the sequence can be changed by certain method calls.String buffers are safe for use by multiple threads. TheseMethodsInmustwantwhenenterlinesame步, in order toconvenientany特decideExampleTopofAllOperationallTablepresent得OKliketheywith某itemsstringlineSequenceAppear,这andplace涉andofeachseparatelyThreadplaceenterOf the lineMethodsCallofSequenceconsistent.
StringBufferThe main operation on it isappendandinsertmethods, which are overloaded to accept any type of data. Each effectively converts the given data to a string, and then appends or inserts the characters of that string into the string buffer.appendThe method always appends these characters at the end of the buffer;insertThe method adds characters at the specified point.For example, if
zRefers to its current content as"start"of the string buffer object, then the method callz.append("le")will cause the string buffer to contain"startle", whereasz.insert(4, "le")Will change the string buffer so that it contains"starlet"。In general, if one is referring to an instance
StringBuffer, thensb.append(x)has the same effectsb.insert(sb.length(), x)。Whenever an operation involving a source sequence occurs (such as appending or inserting from the source sequence), this class synchronizes only on the string buffer performing the operation, not on the source. Note that although
StringBufferDesigned to be safely used by multiple threads concurrently, but if the constructor orappendorinsertIf the operation is passed a source sequence that is shared across threads, the calling code must ensure that the operation has a consistent and immutable view of the source sequence for the duration of the operation. This can be satisfied by a caller holding a lock during the operation invocation, by using immutable source sequences, or by not sharing source sequences across threads.Each string buffer has a capacity. As long as the length of the character sequence contained in this string buffer does not exceed the capacity, it is not necessary to allocate a new internal buffer array. If the internal buffer overflows, it automatically becomes larger.
Unless otherwise specified, it will
nullparameter passed tonullthe constructor or method in ... will cause ... to be thrown.NullPointerException。Since JDK 5, this class has been supplemented with an equivalent class designed for a single thread.
StringBuilder。 should generally be preferredStringBuilderclass, because it supports all the same operations, but is faster because it does not perform synchronization.- API Note:
-
StringBufferImplementationComparablebut does not overrideequals。 Therefore,StringBufferThe natural ordering is inconsistent with equals. ifStringBufferobject used asStringBufferkey in orSortedMapelements,SortedSet。 seeComparable,SortedMap, orSortedSetFor more information. - Starting from the following version:
- 1.0
- See also:
-
StringBuilder,String, Serialized Form
-
-
Constructor Summary
Constructor Constructor Description StringBuffer()Constructs a string buffer with no characters in it and an initial capacity of 16 characters.StringBuffer(int capacity)Constructs a string buffer with no characters and the specified initial capacity.StringBuffer(CharSequence seq)Constructs a string buffer that contains the same characters as the specifiedCharSequencethe same character.StringBuffer(String str)Constructs a string buffer initialized to the content of the specified string.
-
Method Summary
All methods Instance Methods Specific Methods Variables and types Methods Description StringBufferappend(boolean b)willbooleanAppends the string representation of the argument to the sequence.StringBufferappend(char c)willcharAppends the string representation of the argument to this sequence.StringBufferappend(char[] str)willcharThe string representation of the array argument is appended to this sequence.StringBufferappend(char[] str, int offset, int len)willcharAppends the string representation of a subarray of the array argument to this sequence.StringBufferappend(double d)willdoubleAppends the string representation of the argument to this sequence.StringBufferappend(float f)willfloatAppends the string representation of the argument to this sequence.StringBufferappend(int i)willintAppends the string representation of the argument to this sequence.StringBufferappend(long lng)willlongAppends the string representation of the argument to this sequence.StringBufferappend(CharSequence s)the specifiedCharSequenceAppends to this sequence.StringBufferappend(CharSequence s, int start, int end)the specifiedCharSequenceThe sequence is appended to this sequence.StringBufferappend(Object obj)AppendObjectThe string representation of the argument.StringBufferappend(String str)Appends the specified string to this character sequence.StringBufferappend(StringBuffer sb)the specifiedStringBufferAppends to this sequence.StringBufferappendCodePoint(int codePoint)willcodePointAppends the string representation of the argument to this sequence.intcapacity()Returns the current capacity.charcharAt(int index)Returns the character at the specified index in this sequencecharvalue.IntStreamchars()returninta stream of the elements in this sequencecharThe value is zero-extended.intcodePointAt(int index)Returns the character (Unicode code point) at the specified index.intcodePointBefore(int index)Returns the character (Unicode code point) before the specified index.intcodePointCount(int beginIndex, int endIndex)Returns the number of Unicode code points in the specified text range of this sequence.IntStreamcodePoints()Returns a stream of code point values from this sequence.intcompareTo(StringBuffer another)StringBufferCompares two sequences lexicographicallyStringBufferinstance.StringBufferdelete(int start, int end)Deletes the characters in a substring of this sequence.StringBufferdeleteCharAt(int index)Removes from this list the element at the specified positionchar。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 to the destination character array.dst。intindexOf(String str)Returns the index within the string of the first occurrence of the specified substring.intindexOf(String str, int fromIndex)Returns the index within the string of the first occurrence of the specified substring, starting at the specified index.StringBufferinsert(int offset, boolean b)willbooleanInserts the string representation of the argument into this sequence.StringBufferinsert(int offset, char c)willcharInserts the string representation of the argument into this sequence.StringBufferinsert(int offset, char[] str)willcharThe string representation of the array argument is inserted into this sequence.StringBufferinsert(int index, char[] str, int offset, int len)willstrInserts the string representation of a subarray of the array argument into this sequence.StringBufferinsert(int offset, double d)willdoubleInserts the string representation of the argument into this sequence.StringBufferinsert(int offset, float f)willfloatInserts the string representation of the argument into this sequence.StringBufferinsert(int offset, int i)the secondintThe string representation of the argument is inserted into this sequence.StringBufferinsert(int offset, long l)willlongInserts the string representation of the argument into this sequence.StringBufferinsert(int dstOffset, CharSequence s)the specifiedCharSequenceinserted into this sequence.StringBufferinsert(int dstOffset, CharSequence s, int start, int end)the specifiedCharSequenceInserts the sequence into this sequence.StringBufferinsert(int offset, Object obj)willObjectThe string representation of the argument is inserted into this character sequence.StringBufferinsert(int offset, String str)Inserts the string into this character sequence.intlastIndexOf(String str)Returns the index within the string of the last occurrence of the specified substring.intlastIndexOf(String str, int fromIndex)Returns the index within the string of the last occurrence of the specified substring, searching backward from the specified index.intoffsetByCodePoints(int index, int codePointOffset)Returns the index in this sequence that starts from the givenindexOffsetcodePointOffsetcode point.StringBufferreplace(int start, int end, String str)using the specifiedStringcharacters replace the characters in a substring of this sequence.StringBufferreverse()This character sequence is caused by the reverse replacement of the 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.CharSequencesubSequence(int start, int end)Returns a new character sequence that is a subsequence of this sequence.Stringsubstring(int start)returns a newString, which contains a subsequence of the characters currently contained in this character sequence.Stringsubstring(int start, int end)returns a newString, containing a subsequence of the characters currently contained in this sequence.voidtrimToSize()Attempt to reduce the storage space used for the character sequence.-
Class declaring the method java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Interface declaring the method java.lang.CharSequence
chars, codePoints, length, toString
-
-
-
-
Constructor Detail
-
StringBuffer
public StringBuffer()
Constructs a string buffer with no characters in it and an initial capacity of 16 characters.
-
StringBuffer
public StringBuffer(int capacity)
Constructs a string buffer with no characters and the specified initial capacity.- Parameter
-
capacity- the initial capacity. - Exception
-
NegativeArraySizeException- ifcapacitythe argument is less than0。
-
StringBuffer
public StringBuffer(String str)
Constructs a string buffer initialized to the content of the specified string. The initial capacity of the string buffer is16plus the length of the string argument.- Parameter
-
strThe initial contents of the buffer.
-
StringBuffer
public StringBuffer(CharSequence seq)
Constructs a string buffer that contains the same characters as the specifiedCharSequencethe same character. The initial capacity of the string buffer is16addCharSequencethe length of the argument.if the specified
CharSequenceIf the length is less than or equal to zero, returns a capacity of16the empty buffer.- Parameter
-
seq- the sequence to be copied. - Starting from the following version:
- 1.5
-
-
Method Detail
-
compareTo
public int compareTo(StringBuffer another)
StringBufferCompares two sequences lexicographicallyStringBufferinstance. This method followsCharSequence.compare(this, another)The same rules of dictionary comparison as defined in the method.For fine-grained, locale-sensitive String comparison, see
Collator。- Specified by:
-
compareTo, interfaceComparable<StringBuffer> - Implementation Note:
- this method in
thisSynchronizes on (the current object), but not withthis StringBufferis synchronized, whereasStringBuffer anotherandthis StringBufferCompare. - Parameter
-
another-StringBuffercomparedStringBuffer - Result
-
Value
0if thisStringBuffercontains the same characters as the parameterStringBufferthe same character sequence; if thisStringBufferlexicographically less thanStringBufferparameters,StringBuffera negative integer; if thisStringBufferlexicographically greater thanStringBufferparameters,StringBuffera positive integer. - Starting from the following version:
- 11
-
capacity
public int capacity()
Returns the current capacity. Capacity is the amount of storage available for newly inserted characters, beyond which allocation will occur.- Result
- 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, a new internal array with a larger capacity is allocated. The new capacity is the larger of:-
minimumCapacityargument. - Twice the old capacity, plus
2。
minimumCapacityIf the parameter is non-positive, this method does nothing and simply returns. Note that subsequent operations on this object can reduce the actual capacity below the capacity requested here.- Parameter
-
minimumCapacityThe minimum capacity required.
-
-
trimToSize
public void trimToSize()
Attempt to reduce the storage space used for the character sequence. If the buffer is larger than necessary to hold its current sequence of characters, then it may be resized to be more space efficient. Calling this method may (but is not required to) affect subsequent calls.capacity()the value returned by the method.- Starting from the following version:
- 1.5
-
setLength
public void setLength(int newLength)
Sets the length of the character sequence. The sequence is changed to a new character sequence whose length is specified by the argument. for every non-negative index less thanknewLength, at the index of the new character sequencekThe character at the position is the same at the old sequence index.kifkcharacters less than the length of the original character sequence; Otherwise, it is the null character'\u0000'。 In other words, ifnewLengthIf the argument is less than the current length, then the length will be changed to the specified length.if
newLengthIf the parameter is greater than or equal to the current length, then sufficient null characters ('\u0000'), so that length becomesnewLengthparameters.newLengthThe argument must be greater than or equal to0。- Parameter
-
newLength- new length - Exception
-
IndexOutOfBoundsException- ifnewLengththe parameter is negative. - See also:
-
CharSequence.length()
-
charAt
public char charAt(int index)
Returns the character at the specified index in this sequencecharvalue. firstcharvalue at index0, the next is at index1, and so on, as shown in the array indices.The index parameter must be greater than or equal to
0, and less than the length of this sequence.if the index specified
charValue issurrogate, then a surrogate value is returned.- Specified by:
-
charAtIn the interfaceCharSequence - Parameter
-
index- requiredcharthe index of the value. - Result
- at the specified index
charvalue. - Exception
-
IndexOutOfBoundsException- ifindexis negative or greater than or equal tolength()。 - See also:
-
CharSequence.length()
-
codePointAt
public int codePointAt(int index)
Returns the character (Unicode code point) at the specified index. index referencecharvalue (Unicode code units), ranging from0toCharSequence.length()- 1。If the specified at the given index
charIf the value is in the high surrogate range, then the following index is less than the length of this sequence, and the value at the following indexcharIf the value is in the low-surrogate range, then the supplementary code point corresponding to this surrogate pair is returned. Otherwise, return the one at the given indexcharvalue.- Parameter
-
index-charthe index of the value - Result
-
indexthe code point value of the character at - Exception
-
IndexOutOfBoundsException- ifindexThe argument is negative or not less than the length of this sequence. - Starting from the following version:
- 1.5
-
codePointBefore
public int codePointBefore(int index)
Returns the character (Unicode code point) before the specified index. index referencecharvalue (Unicode code units), ranging from1toCharSequence.length()。if
charAt value(index - 1)is in the low surrogate range,(index - 2)is not negative, andcharAt value(index - 2)is in the high surrogate range, then the supplementary code point value of the surrogate pair is returned. ifcharAt valueindex - 1If it is an unpaired low surrogate or a high surrogate, then the surrogate value is returned.- Parameter
-
indexThe index after the code point to be returned. - Result
- The Unicode code point value preceding the given index.
- Exception
-
IndexOutOfBoundsException- ifindexThe argument is less than 1 or greater than the length of this sequence. - Starting from the following version:
- 1.5
-
codePointCount
public int codePointCount(int beginIndex, int endIndex)Returns the number of Unicode code points in the specified text range of this sequence. The text range starts at the specifiedbeginIndex, and extends tocharat indexendIndex - 1。 Therefore, the length of the text range (incharin s) isendIndex-beginIndex。 Unpaired surrogates in this sequence count as one code point each.- Parameter
-
beginIndexThe first of the text range.charthe index of. -
endIndexThe last of the text range.charthe index after. - Result
- The number of Unicode code points in the specified text range.
- Exception
-
IndexOutOfBoundsException- ifbeginIndexis negative, orendIndexis greater than the length of this sequence, orbeginIndexgreater thanendIndex。 - Starting from the following version:
- 1.5
-
offsetByCodePoints
public int offsetByCodePoints(int index, int codePointOffset)Returns the index in this sequence that starts from the givenindexOffsetcodePointOffsetcode point.indexandcodePointOffsetUnpaired surrogates in the given text range count as one code point each.- Parameter
-
indexThe index to be offset. -
codePointOffsetThe offset in code points. - Result
- an index in this sequence.
- Exception
-
IndexOutOfBoundsException- ifindexIs negative or greater than the length of this sequence, or ifcodePointOffsetis positive and fromindexthe substring startingcodePointOffsetcode point is less thancodePointOffset, orcodePointOffsetis negative andindexthe substring beforeindexis less than the absolute value of the absolute valuecodePointOffsetcode point. - Starting from the following version:
- 1.5
-
getChars
public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)Copies characters from this sequence to the destination character array.dst。 The first character to be copied is the indexsrcBegin; The last character to be copied is located at indexsrcEnd-1。 the total number of characters to be copied issrcEnd-srcBegin。 characters are copied todstin the subarray, from indexdstBeginStarts at and ends at the index:dstbegin + (srcEnd-srcBegin) - 1- Parameter
-
srcBegin- start copying at this offset. -
srcEnd- stop copying at this offset. -
dst- the array into which data is to be copied. -
dstBegin- offset todst。 - Exception
-
IndexOutOfBoundsException- if any of the following conditions is satisfied:-
srcBeginis negative -
dstBeginis negative -
srcBeginthe argument is greater thansrcEndparameters. -
srcEndgreater thanthis.length()。 -
dstBegin+srcEnd-srcBegingreater thandst.length
-
-
setCharAt
public void setCharAt(int index, char ch)The character at the specified index is set toch。 The sequence is altered to represent a new character sequence that is the same as the original character sequence, except that it contains characters.chat positionindex。The index parameter must be greater than or equal to
0, and less than the length of this sequence.- Parameter
-
indexThe index of the character to be modified. -
ch- the new character. - Exception
-
IndexOutOfBoundsException- ifindexis negative or greater than or equal tolength()。 - See also:
-
CharSequence.length()
-
append
public StringBuffer append(Object obj)
AppendObjectThe string representation of the argument.The overall effect is the same as through the method
String.valueOf(Object)Converts the argument to a stringexactly the same, and the characters of that string are thenappendedto this character sequence.- Parameter
-
obj-Object。 - Result
- A reference to this object.
-
append
public StringBuffer append(String str)
Appends the specified string to this character sequence.StringThe characters of the argument are appended in order, increasing the length of this sequence by the length of the argument. ifstrYesnull, then append four characters"null"。designnis performed
appendthe length of this character sequence before the method. ifkless thann, then the index in the new character sequencekthe character at ... is equal to the character at index ... in the old character sequence.kcharacter at; Otherwise, it is equal to the parameter.strindex inkncharacter at.- Parameter
-
strA string. - Result
- A reference to this object.
-
append
public StringBuffer append(StringBuffer sb)
the specifiedStringBufferAppends to this sequence.the character described
StringBufferparameters appended, in order, to the contentStringBuffer, increasing its lengthStringBufferby the length of the argument. ifsbYesnull, then the four characters"null"will be appended to thisStringBuffer。ordernis the length of the old character sequence, that is, during execution
appendbefore the methodStringBufferthe length of the contained character sequence. ifkless thann, then the index in the new character sequencekthe character at ... is equal to the character at index ... in the old character sequence.kcharacter at; Otherwise, it is equal to the parameter.sbindex inkncharacter at.This method on the target object
thissynchronized, but not synchronized on the source (sb)。- Parameter
-
sb- to be appended.StringBuffer。 - Result
- A reference to this object.
- Starting from the following version:
- 1.4
-
append
public StringBuffer append(CharSequence s)
the specifiedCharSequenceAppends to this sequence.CharSequenceThe characters of the argument are appended in order, increasing the length of this sequence by the length of the argument.The result of this method is exactly the same as calling this.append(s, 0, s.length());
This method on the target object
thissynchronized, but not synchronized on the source (s)。if
sYesnull, then append four characters"null"。- Specified by:
-
appendinterfaceAppendable - Parameter
-
s- to be appended.CharSequence。 - Result
- A reference to this object.
- Starting from the following version:
- 1.5
-
append
public StringBuffer append(CharSequence s, int start, int end)
the specifiedCharSequenceThe sequence is appended to this sequence.from index
startthe start parametersCharacters are appended to the contents of this sequence in order, up to (but not including) the index.end。 the length of this sequence increases byend - startthe value of.designnis performed
appendthe length of this character sequence before the method. then, ifkless thann, then the index in the character sequenceAt kthe character becomes equal to the index in this sequencekcharacter at; Otherwise, it is equal to the parameter.sindex ink + start-nAt thes。if
sYesnull, then this method appends characters as if the s parameter were a sequence containing four characters"null"sequence of"null"。- Specified by:
-
appendIn the interfaceAppendable - Parameter
-
sThe sequence to be appended. -
start- the start index of the subsequence to be appended. -
end- the end index of the subsequence to be appended. - Result
- A reference to this object.
- Exception
-
IndexOutOfBoundsException- ifstartis negative, orstartgreater thanendorendgreater thans.length() - Starting from the following version:
- 1.5
-
append
public StringBuffer append(char[] str)
willcharThe string representation of the array argument is appended to this sequence.The characters of the array argument are appended, in order, to the contents of this sequence. The length of this sequence increases by the length of the argument.
The overall effect is just as if through the method
String.valueOf(char[])Converts the argument to a stringSame, and the characters of that string are thenappendedto this character sequence.- Parameter
-
strThe character to be appended. - Result
- A reference to this object.
-
append
public StringBuffer append(char[] str, int offset, int len)
willcharAppends the string representation of a subarray of the array argument to this sequence.from index
offsetstartingcharArraystrThe characters are appended to the contents of this sequence in order. The value of the length increase of this sequencelen。The overall effect is just as if through the method
String.valueOf(char[],int,int)Converts the argument to a stringSame, and the characters of that string are thenappendedto this character sequence.- Parameter
-
strThe character to be appended. -
offsetThe first to be appended.charthe index of. -
len- to be appended.charthe number of s. - Result
- A reference to this object.
- Exception
-
IndexOutOfBoundsException- if it isoffset < 0orlen < 0oroffset+len > str.length
-
append
public StringBuffer append(boolean b)
willbooleanAppends the string representation of the argument to the sequence.The overall effect is the same as through the method
String.valueOf(boolean)Converts the argument to a stringexactly the same, and the characters of that string are thenappendedto this character sequence.- Parameter
-
b- aboolean。 - Result
- A reference to this object.
-
append
public StringBuffer append(char c)
willcharAppends the string representation of the argument to this sequence.The argument is appended to the contents of this sequence. The length of this sequence increases
1。The overall effect with the method
String.valueOf(char)Converts the argument to a stringexactly the same, then the characters in the string areappended。- Specified by:
-
appendIn the interfaceAppendable - Parameter
-
c- achar。 - Result
- A reference to this object.
-
append
public StringBuffer append(int i)
willintAppends the string representation of the argument to this sequence.The overall effect is just as if through the method
String.valueOf(int)Converts the argument to a stringSame, and the characters of that string are thenappendedto this character sequence.- Parameter
-
i-int。 - Result
- A reference to this object.
-
appendCodePoint
public StringBuffer appendCodePoint(int codePoint)
willcodePointAppends the string representation of the argument to this sequence.The argument is appended to the contents of this sequence. The length of this sequence increases
Character.charCount(codePoint)。The overall effect is the same as through the method
Character.toChars(int)converts the argument tochararrayexactly the same,and the characters in the array are thenappendedto this character sequence.- Parameter
-
codePoint- Unicode code point - Result
- A reference to this object.
- Starting from the following version:
- 1.5
-
append
public StringBuffer append(long lng)
willlongAppends the string representation of the argument to this sequence.The overall effect is the same as through the method
String.valueOf(long)Converts the argument to a stringexactly the same, then the characters of this string are those of the specified character sequenceappended。- Parameter
-
lng- along。 - Result
- A reference to this object.
-
append
public StringBuffer append(float f)
willfloatAppends the string representation of the argument to this sequence.The overall effect is the same as through the method
String.valueOf(float)Converts the argument to a stringexactly the same, then the characters of this string are those of the specified character sequenceappended。- Parameter
-
f- afloat。 - Result
- A reference to this object.
-
append
public StringBuffer append(double d)
willdoubleAppends the string representation of the argument to this sequence.The overall effect with the method
String.valueOf(double)Converts the argument to a stringexactly the same, and then the characters of the string areappended。- Parameter
-
d- adouble。 - Result
- A reference to this object.
-
delete
public StringBuffer delete(int start, int end)
Deletes the characters in a substring of this sequence. Substring from the specifiedstartstarts, if no such character exists, extends to the indexend - 1the character at, or the end of, the sequence. ifstartequalsend, then no changes are made.- Parameter
-
startThe start index, inclusive. -
endThe end index, exclusive. - Result
- this object.
- Exception
-
StringIndexOutOfBoundsException- ifstartis negative, greater thanlength(), or greater thanend。 - Starting from the following version:
- 1.2
-
deleteCharAt
public StringBuffer deleteCharAt(int index)
Removes from this list the element at the specified positionchar。 this sequence consists of acharshortened.Note: If the character at the given index is a supplementary character, this method does not delete the entire character. If accurate handling of supplementary characters is required, determine the count
charby calling deleteCharacter.charCount(thisSequence.codePointAt(index)), wherethisSequencefor this sequence.- Parameter
-
indexThe index number to be deleted.char - Result
- this object.
- Exception
-
StringIndexOutOfBoundsException- ifindexis negative or greater than or equal tolength()。 - Starting from the following version:
- 1.2
-
replace
public StringBuffer replace(int start, int end, String str)
using the specifiedStringcharacters replace the characters in a substring of this sequence. Substring from the specifiedstartbegins, and if no such character exists, then extends to the indexend - 1the character at, or the end of, the sequence. First remove the characters in the substring, then atStringInserts the specifiedstart。 (If necessary, this sequence will be extended to accommodate the specified string.)- Parameter
-
startThe start index, inclusive. -
endThe end index, exclusive. -
strThe string that will replace the prior contents. - Result
- this object.
- Exception
-
StringIndexOutOfBoundsException- ifstartis negative, greater thanlength(), or greater thanend。 - Starting from the following version:
- 1.2
-
substring
public String substring(int start)
returns a newString, which contains a subsequence of the characters currently contained in this character sequence. The substring starts at the specified index and extends to the end of this sequence.- Parameter
-
startThe start index, inclusive. - Result
- a new string.
- Exception
-
StringIndexOutOfBoundsException- ifstartis less than zero, or greater than the length of this object. - Starting from the following version:
- 1.2
-
subSequence
public CharSequence subSequence(int start, int end)
Returns a new character sequence that is a subsequence of this sequence.The form of calling this method
The behavior is exactly the same as callingsb.subSequence(begin, end)This method is provided so that this class can implementsb.substring(begin, end)CharSequenceInterface.- Specified by:
-
subSequence, interfaceCharSequence - Parameter
-
startThe start index, inclusive. -
endThe end index, exclusive. - Result
- the specified subsequence.
- Exception
-
IndexOutOfBoundsException- ifstartorendis negative, ifendgreater thanlength(), orstartgreater thanend - Starting from the following version:
- 1.4
-
substring
public String substring(int start, int end)
returns a newString, containing a subsequence of the characters currently contained in this sequence. Substring from the specifiedstartstarts, and extends to the indexend - 1character at.- Parameter
-
startThe start index, inclusive. -
endThe end index, exclusive. - Result
- a new string.
- Exception
-
StringIndexOutOfBoundsException- ifstartorendis negative or greater thanlength(), orstartgreater thanend。 - Starting from the following version:
- 1.2
-
insert
public StringBuffer insert(int index, char[] str, int offset, int len)
willstrThe string representation of the subarray of the array argument is inserted into this sequence. The subarray from the specifiedoffsetstarts at and extends tolenchars。 the characters of the subarray are inindexinserted into this sequence at the indicated position. The length of this sequence increaseslenchars。- Parameter
-
indexThe position at which the subarray is inserted. -
str- Achararray. -
offsetThe first in the subarray to be inserted.charthe index of. -
lenIn the subarray to be inserted.charthe number of s. - Result
- this object
- Exception
-
StringIndexOutOfBoundsException- ifindexis negative or greater thanlength(), oroffsetorlenis negative, or(offset+len)greater thanstr.length。 - Starting from the following version:
- 1.2
-
insert
public StringBuffer insert(int offset, Object obj)
willObjectThe string representation of the argument is inserted into this character sequence.The overall effect is the same as through the method
String.valueOf(Object)Converts the second argument to a stringexactly the samethen the characters of this string are inserted into the character sequence at the specified offsetinserted。offsetThe argument must be greater than or equal to0, and is less than or equal to this sequence'slength 。- Parameter
-
offset- the offset. -
obj-Object。 - Result
- A reference to this object.
- Exception
-
StringIndexOutOfBoundsExceptionIf the offset is invalid.
-
insert
public StringBuffer insert(int offset, String str)
Inserts the string into this character sequence.StringThe characters of the argument are inserted into this sequence at the specified offset in order, shifting any characters originally above that position upward, and increasing the length of this sequence by the length of the argument. ifstrYesnull, then the four characters"null"Inserted into this sequence.Index in the new character sequencekthe character at the position is equal to:
- ifkless than
offset, then the index in the old character sequencekthe character at - Parameter
strthe index withink-offsetthe characters, ifknot less thanoffsetbut less thanoffset+str.length() - the index in the old character sequence isk
-str.length()the characters, ifknot less thanoffset+str.length()
offsetThe argument must be greater than or equal to0, and is less than or equal to this sequence'slength 。- Parameter
-
offset- the offset. -
strA string. - Result
- A reference to this object.
- Exception
-
StringIndexOutOfBoundsExceptionIf the offset is invalid.
- ifkless than
-
insert
public StringBuffer insert(int offset, char[] str)
willcharThe string representation of the array argument is inserted into this sequence.The characters of the array argument at
offsetat the indicated position into the contents of this sequence. The length of this sequence increases by the length of the argument.The overall effect is the same as through the method
String.valueOf(char[])Converts the second argument to a stringexactly the samethen the characters of this string are inserted into the character sequence at the specified offsetinserted。offsetThe argument must be greater than or equal to0, and is less than or equal to this sequence'slength 。- Parameter
-
offset- the offset. -
str- the character array. - Result
- A reference to this object.
- Exception
-
StringIndexOutOfBoundsExceptionIf the offset is invalid.
-
insert
public StringBuffer insert(int dstOffset, CharSequence s)
the specifiedCharSequenceinserted into this sequence.CharSequenceInserts the characters of the parameter into this sequence at the specified offset, shifting up any characters initially above that position, and increasing the length of this sequence by the length of parameter s.The result of this method is the same as calling this object's
insertthe (dstOffset, s, 0, s.length()) methodexactly the same 。if
sYesnull, then the four characters"null"Inserted into this sequence.- Parameter
-
dstOffset- the offset. -
sThe sequence to be inserted - Result
- A reference to this object.
- Exception
-
IndexOutOfBoundsExceptionIf the offset is invalid. - Starting from the following version:
- 1.5
-
insert
public StringBuffer insert(int dstOffset, CharSequence s, int start, int end)
the specifiedCharSequenceInserts the sequence into this sequence.subsequence of the argument
sby specifyingstartandendare inserted at the specified destination offset in this sequence, moving up any characters originally above that position. the length of this sequence increases byend - start。index in this sequencekthe character at the position becomes equal to:
- ifkless than
dstOffset, then the index in this sequencekthe character at - Parameter
sthe index withink+start-dstOffsetthe characters, ifkgreater than or equal todstOffsetbut less thandstOffset+end-start - ifkgreater than or equal to
dstOffset+end-start, then the index in this sequence isk-(end-start)characters of
dstOffsetThe argument must be greater than or equal to0, and is less than or equal to this sequence'slength 。The start parameter must be non-negative and not greater than
end。the end parameter must be greater than or equal to
start, and less than or equal to the length of s.if
sYesnull, then this method inserts characters as if the s parameter were a string containing four characters"null"sequence of"null"。- Parameter
-
dstOffsetThe offset in this sequence. -
sThe sequence to be inserted. -
start- the start index of the subsequence to be inserted. -
end- the end index of the subsequence to be inserted. - Result
- A reference to this object.
- Exception
-
IndexOutOfBoundsException- ifdstOffsetis negative or greater thanthis.length(), orstartorendis negative, orstartgreater thanendorendgreater thans.length() - Starting from the following version:
- 1.5
- ifkless than
-
insert
public StringBuffer insert(int offset, boolean b)
willbooleanInserts the string representation of the argument into this sequence.The overall effect is the same as through the method
String.valueOf(boolean)Converts the second argument to a stringexactly the samethen the characters of this string are inserted into the character sequence at the specified offsetinserted。offsetThe argument must be greater than or equal to0, and less than or equal to this sequence'slength 。- Parameter
-
offset- the offset. -
b- aboolean。 - Result
- A reference to this object.
- Exception
-
StringIndexOutOfBoundsExceptionIf the offset is invalid.
-
insert
public StringBuffer insert(int offset, char c)
willcharInserts the string representation of the argument into this sequence.The overall effect is the same as through the method
String.valueOf(char)Converts the second argument to a stringexactly the same, and then the characters in this string are inserted into this character sequence at the specified offsetinserted。offsetThe argument must be greater than or equal to0, and less than or equal to this sequence'slength 。- Parameter
-
offset- offset. -
c- achar。 - Result
- A reference to this object.
- Exception
-
IndexOutOfBoundsExceptionIf the offset is invalid.
-
insert
public StringBuffer insert(int offset, int i)
the secondintInserts the string representation of the argument into this sequence.The overall effect is the same as through the method
String.valueOf(int)Converts the second argument to a stringexactly the samethen the characters of this string are inserted into the character sequence at the specified offsetinserted。offsetThe argument must be greater than or equal to0, and less than or equal to this sequence'slength 。- Parameter
-
offset- the offset. -
i-int。 - Result
- A reference to this object.
- Exception
-
StringIndexOutOfBoundsExceptionIf the offset is invalid.
-
insert
public StringBuffer insert(int offset, long l)
willlongInserts the string representation of the argument into this sequence.The overall effect is the same as through the method
String.valueOf(long)Converts the second argument to a stringexactly the samethen the characters of this string are inserted into the character sequence at the specified offsetinserted。offsetThe argument must be greater than or equal to0, and less than or equal to this sequence'slength 。- Parameter
-
offset- offset. -
l- along。 - Result
- A reference to this object.
- Exception
-
StringIndexOutOfBoundsExceptionIf the offset is invalid.
-
insert
public StringBuffer insert(int offset, float f)
willfloatInserts the string representation of the argument into this sequence.The overall effect is the same as through the method
String.valueOf(float)Converts the second argument to a stringexactly the samethen the characters of this string are inserted into the character sequence at the specified offsetinserted。offsetThe argument must be greater than or equal to0, and less than or equal to this sequence'slength 。- Parameter
-
offset- the offset. -
f- afloat。 - Result
- A reference to this object.
- Exception
-
StringIndexOutOfBoundsExceptionIf the offset is invalid.
-
insert
public StringBuffer insert(int offset, double d)
willdoubleInserts the string representation of the argument into this sequence.The overall effect is the same as through the method
String.valueOf(double)Converts the second argument to a stringexactly the same, and then the characters of this string are inserted into this character sequence at the specified offsetinserted。offsetThe argument must be greater than or equal to0, and less than or equal to this sequence'slength 。- Parameter
-
offset- offset. -
d- adouble。 - Result
- A reference to this object.
- Exception
-
StringIndexOutOfBoundsExceptionIf the offset is invalid.
-
indexOf
public int indexOf(String str)
Returns the index within the string of the first occurrence of the specified substring.The returned index is the minimum value
k, where:if there is no such valuethis.toString().startsWith(str, k)kexists, then-1returns.- Parameter
-
str- the substring to search for. - Result
- The index of the first occurrence of the specified substring, or if no such occurrence,
-1。 - Starting from the following version:
- 1.4
-
indexOf
public int indexOf(String str, int fromIndex)
Returns the index within the string of the first occurrence of the specified substring, starting at the specified index.The returned index is the minimum value
k, where:if there is no such valuek >= Math.min(fromIndex, this.length()) && this.toString().startsWith(str, k)kexists, then-1returns.- Parameter
-
str- the substring to search for. -
fromIndex- the index from which to start the search. - Result
- The index of the first occurrence of the specified substring, starting at the specified index, or if no such occurrence,
-1。 - Starting from the following version:
- 1.4
-
lastIndexOf
public int lastIndexOf(String str)
Returns the index within the string of the last occurrence of the specified substring. The last occurrence of the empty string "" is considered to occur at the index valuethis.length()。The returned index is the maximum value
k, where:if there is no such valuethis.toString().startsWith(str, k)kexists, then-1returns.- Parameter
-
str- the substring to search for. - Result
- The index of the last occurrence of the specified substring, or if no such occurrence,
-1。 - Starting from the following version:
- 1.4
-
lastIndexOf
public int lastIndexOf(String str, int fromIndex)
Returns the index within the string of the last occurrence of the specified substring, searching backward from the specified index.The returned index is the maximum value
k, where:if there is no such valuek <= Math.min(fromIndex, this.length()) && this.toString().startsWith(str, k)kexists, then-1returns.- Parameter
-
str- the substring to search for. -
fromIndex- the index from which to start the search. - Result
- The index of the last occurrence of the specified substring, searching backward from the specified index, or if no such occurrence
-1search. - Starting from the following version:
- 1.4
-
reverse
public StringBuffer reverse()
This character sequence is caused by the reverse replacement of the sequence. If the sequence contains any surrogate pairs, these surrogate pairs are treated as a single character for the reverse operation. Therefore, the order of high and low surrogates will never be reversed. designnis performedreversethe character length of this character sequence before the method (notcharthe length in the value). Then, the index in the new character sequencekthe character at ... is equal to the character at index ... in the old character sequence.nk-1character at.Note that the reverse operation may result in producing surrogate pairs that were unpaired low and high surrogates before the operation. For example, reversing “\ uDC00 \ uD800” generates “\ uD800 \ uDC00”, which is a valid surrogate pair.
- Result
- A reference to this object.
- Starting from the following version:
- 1.0.2
-
chars
public IntStream chars()
returninta stream of the elements in this sequencecharThe value is zero-extended. toof the surrogate code pointAny character is uninterpreted.When a terminal stream operation begins, the stream is bound to the sequence (specifically, for mutable sequences, the stream'sspliteratorYeslate-binding )。 If the sequence is modified during the operation, the result is undefined.
- Specified by:
-
charsIn the interfaceCharSequence - Result
- An IntStream of char values from this sequence.
- Starting from the following version:
- 9
-
codePoints
public IntStream codePoints()
Returns a stream of code point values from this sequence. Any surrogate pairs encountered in the sequence are combined, just likesame as Character.toCodePoint, the result is passed to the stream. Any other code unit (including ordinary BMP characters, unpaired surrogates, and undefined code units) is zero-extended tointthe value, then passed to the stream.When a terminal stream operation begins, the stream is associated with the sequence (specifically, for mutable sequences, the stream'sspliteratorYeslate-binding )。 If the sequence is modified during the operation, the result is undefined.
- Specified by:
-
codePointsInCharSequence - Result
- The IntStream of Unicode code points points to this sequence.
- Starting from the following version:
- 9
-
-