`
wsql
  • 浏览: 11715992 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
文章分类
社区版块
存档分类
最新评论

StringBuffer

 
阅读更多

Overview Package Class Use Tree Deprecated Index Help
Java™ Platform
Standard Ed. 6
PREV CLASSNEXT CLASS FRAMESNO FRAMESAll Classes
SUMMARY: NESTED | FIELD |CONSTR|METHOD DETAIL: FIELD |CONSTR|METHOD

java.lang
Class StringBuffer

java.lang.Objectextended by java.lang.StringBuffer
All Implemented Interfaces:
Serializable,Appendable,CharSequence

public final classStringBuffer
extendsObject
implementsSerializable,CharSequence

Athread-safe(线程安全), mutable sequence of characters. A string buffer(缓冲区) is like aString, but can bemodified(修改). 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 bymultiple threads(多线程). The methods are synchronized(同步) where necessary(必要)so that(所以) all the operations(操作) on any particular(特定) instance behave as(像) if they occur(发生) in someserial(串行的) order(序列) that isconsistent with(一致) the order(顺序) of themethod calls(调用方法) made byeach of the individual(每个独立的) threads involved(涉及的).

Theprincipal(最重要的) operations on aStringBufferare theappendandinsertmethods, 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. Theappendmethod always adds these characters at the end(末端) of the buffer; theinsertmethod adds the characters at a specified point.

For example, ifzrefers(属于) to a string buffer object whose current contents are "start", then the method callz.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 aStringBuffer, thensb.append(x)has the same effect(效果) assb.insert(sb.length(), x).

Whenever an operationoccurs 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 toallocate(分配)a newinternal(内部) buffer array. If the internal buffer overflows(溢出), it isautomatically(自动的)made larger(增大). As of release(发布) JDK 5, this class has been supplemented(增加) with anequivalent class(等价类) designed for use by a single thread,StringBuilder. TheStringBuilderclass shouldgenerally(一般) 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
StringBuffer()
Constructs a string buffer with no characters in it and an initial capacity of 16 characters.
StringBuffer(CharSequenceseq)
Constructs a string buffer that contains the same characters as the specifiedCharSequence.
StringBuffer(int capacity)
Constructs a string buffer with no characters in it and the specified initial capacity.
StringBuffer(Stringstr)
Constructs a string buffer initialized to the contents of the specified string.
Method Summary
StringBuffer append(boolean b)
Appends the string representation of thebooleanargument to the sequence.
StringBuffer append(char c)
Appends the string representation of thecharargument to this sequence.
StringBuffer append(char[] str)
Appends the string representation of thechararray argument to this sequence.
StringBuffer append(char[] str, int offset, int len)
Appends the string representation of a subarray of thechararray argument to this sequence.
StringBuffer append(CharSequences)
Appends the specifiedCharSequenceto this sequence.
StringBuffer append(CharSequences, int start, int end)
Appends a subsequence of the specifiedCharSequenceto this sequence.
StringBuffer append(double d)
Appends the string representation of thedoubleargument to this sequence.
StringBuffer append(float f)
Appends the string representation of thefloatargument to this sequence.
StringBuffer append(int i)
Appends the string representation of theintargument to this sequence.
StringBuffer append(long lng)
Appends the string representation of thelongargument to this sequence.
StringBuffer append(Objectobj)
Appends the string representation of theObjectargument.
StringBuffer append(Stringstr)
Appends the specified string to this character sequence.
StringBuffer append(StringBuffersb)
Appends the specifiedStringBufferto this sequence.
StringBuffer appendCodePoint(int codePoint)
Appends the string representation of thecodePointargument to this sequence.
int capacity()
Returns the current capacity.
char charAt(int index)
Returns thecharvalue in this sequence at the specified index.
int codePointAt(int index)
Returns the character (Unicode code point) at the specified index.
int codePointBefore(int index)
Returns the character (Unicode code point) before the specified index.
int codePointCount(int beginIndex, int endIndex)
Returns the number of Unicode code points in the specified text range of this sequence.
StringBuffer delete(int start, int end)
Removes the characters in a substring of this sequence.
StringBuffer deleteCharAt(int index)
Removes thecharat the specified position in this sequence.
void ensureCapacity(int minimumCapacity)
Ensures that the capacity is at least equal to the specified minimum.
void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
Characters are copied from this sequence into the destination character arraydst.
int indexOf(Stringstr)
Returns the index within this string of the first occurrence of the specified substring.
int indexOf(Stringstr, int fromIndex)
Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.
StringBuffer insert(int offset, boolean b)
Inserts the string representation of thebooleanargument into this sequence.
StringBuffer insert(int offset, char c)
Inserts the string representation of thecharargument into this sequence.
StringBuffer insert(int offset, char[] str)
Inserts the string representation of thechararray argument into this sequence.
StringBuffer insert(int index, char[] str, int offset, int len)
Inserts the string representation of a subarray of thestrarray argument into this sequence.
StringBuffer insert(int dstOffset,CharSequences)
Inserts the specifiedCharSequenceinto this sequence.
StringBuffer insert(int dstOffset,CharSequences, int start, int end)
Inserts a subsequence of the specifiedCharSequenceinto this sequence.
StringBuffer insert(int offset, double d)
Inserts the string representation of thedoubleargument into this sequence.
StringBuffer insert(int offset, float f)
Inserts the string representation of thefloatargument into this sequence.
StringBuffer insert(int offset, int i)
Inserts the string representation of the secondintargument into this sequence.
StringBuffer insert(int offset, long l)
Inserts the string representation of thelongargument into this sequence.
StringBuffer insert(int offset,Objectobj)
Inserts the string representation of theObjectargument into this character sequence.
StringBuffer insert(int offset,Stringstr)
Inserts the string into this character sequence.
int lastIndexOf(Stringstr)
Returns the index within this string of the rightmost occurrence of the specified substring.
int lastIndexOf(Stringstr, int fromIndex)
Returns the index within this string of the last occurrence of the specified substring.
int length()
Returns the length (character count).
int offsetByCodePoints(int index, int codePointOffset)
Returns the index within this sequence that is offset from the givenindexbycodePointOffsetcode points.
StringBuffer replace(int start, int end,Stringstr)
Replaces the characters in a substring of this sequence with characters in the specifiedString.
StringBuffer reverse()
Causes this character sequence to be replaced by the reverse of the sequence.
void setCharAt(int index, char ch)
The character at the specified index is set toch.
void setLength(int newLength)
Sets the length of the character sequence.
CharSequence subSequence(int start, int end)
Returns a new character sequence that is a subsequence of this sequence.
String substring(int start)
Returns a newStringthat contains a subsequence of characters currently contained in this character sequence.
String substring(int start, int end)
Returns a newStringthat contains a subsequence of characters currently contained in this sequence.
String toString()
Returns a string representing the data in this sequence.
void trimToSize()
Attempts to reduce storage used for the character sequence.
Methods inherited from class java.lang.Object
clone,equals,finalize,getClass,hashCode,notify,notifyAll,wait,wait,wait

Constructor Detail

StringBuffer

publicStringBuffer()
Constructs a string buffer with no characters in it and an initial capacity of 16 characters.

StringBuffer

publicStringBuffer(int capacity)
Constructs a string buffer with no characters in it and the specified initial capacity.
Parameters:
capacity- the initial capacity.
Throws:
NegativeArraySizeException- if thecapacityargument is less than0.

StringBuffer

publicStringBuffer(Stringstr)
Constructs a string buffer initialized to the contents of the specified string. The initial capacity of the string buffer is16plus the length of the string argument.
Parameters:
str- the initial contents of the buffer.
Throws:
NullPointerException- ifstrisnull

StringBuffer

publicStringBuffer(CharSequenceseq)
Constructs a string buffer that contains the same characters as the specifiedCharSequence. The initial capacity of the string buffer is16plus the length of theCharSequenceargument.

If the length of the specifiedCharSequenceis less than or equal to zero, then an empty buffer of capacity16is returned.

Parameters:
seq- the sequence to copy.
Throws:
NullPointerException- ifseqisnull
Since:
1.5
Method Detail

length

public intlength()
Returns the length (character count).
Specified by:
lengthin interfaceCharSequence
Returns:
the length of the sequence of characters currently represented by this object

capacity

public intcapacity()
Returns the current capacity. The capacity is the amount of storage available for newly inserted characters, beyond which an allocation will occur.
Returns:
the current capacity

ensureCapacity

public voidensureCapacity(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 internal array is allocated with greater capacity. The new capacity is the larger of:
  • TheminimumCapacityargument.
  • Twice the old capacity, plus2.
If theminimumCapacityargument is nonpositive, this method takes no action and simply returns.
Parameters:
minimumCapacity- the minimum desired capacity.

trimToSize

public voidtrimToSize()
Attempts to reduce storage 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 become more space efficient. Calling this method may, but is not required to, affect the value returned by a subsequent call to thecapacity()method.
Since:
1.5

setLength

public voidsetLength(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 nonnegative indexkless thannewLength, the character at indexkin the new character sequence is the same as the character at indexkin the old sequence ifkis less than the length of the old character sequence; otherwise, it is the null character'\u0000'. In other words, if thenewLengthargument is less than the current length, the length is changed to the specified length.

If thenewLengthargument is greater than or equal to the current length, sufficient null characters ('\u0000') are appended so that length becomes thenewLengthargument.

ThenewLengthargument must be greater than or equal to0.

Parameters:
newLength- the new length
Throws:
IndexOutOfBoundsException- if thenewLengthargument is negative.
See Also:
length()

charAt

public charcharAt(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 to0, and less than the length of this sequence.

If thecharvalue specified by the index is asurrogate, the surrogate value is returned.

Specified by:
charAtin interfaceCharSequence
Parameters:
index- the index of the desiredcharvalue.
Returns:
thecharvalue at the specified index.
Throws:
IndexOutOfBoundsException- ifindexis negative or greater than or equal tolength().
See Also:
length()

codePointAt

public intcodePointAt(int index)
Returns the character (Unicode code point) at the specified index. The index refers tocharvalues (Unicode code units) and ranges from0tolength()- 1.

If thecharvalue specified at the given index is in the high-surrogate range, the following index is less than the length of this sequence, and thecharvalue at the following index is in the low-surrogate range, then the supplementary code point corresponding 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 theindex
Since:
1.5

codePointBefore

public intcodePointBefore(int index)
Returns the character (Unicode code point) before the specified index. The index refers tocharvalues (Unicode code units) and ranges from1tolength().

If thecharvalue at(index - 1)is in the low-surrogate range,(index - 2)is not negative, and thecharvalue at(index - 2)is in the high-surrogate range, then the supplementary code point value of the surrogate pair is returned. If thecharvalue atindex - 1is an unpaired low-surrogate or a high-surrogate, the surrogate 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.
Since:
1.5

codePointCount

public intcodePointCount(int beginIndex, int endIndex)
Returns the number of Unicode code points in the specified text range of this sequence. The text range begins at the specifiedbeginIndexand extends to thecharat indexendIndex - 1. Thus the length (inchars) of the text range isendIndex-beginIndex. Unpaired surrogates within this sequence count as one code point each.
Parameters:
beginIndex- the index to the firstcharof the text range.
endIndex- the index after the lastcharof the text range.
Returns:
the number of Unicode code points in the specified text range
Since:
1.5

offsetByCodePoints

public intoffsetByCodePoints(int index, int codePointOffset)
Returns the index within this sequence that is offset from the givenindexbycodePointOffsetcode points. Unpaired surrogates within the text range given byindexandcodePointOffsetcount as one code point each.
Parameters:
index- the index to be offset
codePointOffset- the offset in code points
Returns:
the index within this sequence
Since:
1.5

getChars

public voidgetChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
Characters are copied from this sequence into the destination character arraydst. The first character to be copied is at indexsrcBegin; the last character to be copied is at indexsrcEnd-1. The total number of characters to be copied issrcEnd-srcBegin. The characters are copied into the subarray ofdststarting at indexdstBeginand ending at index:
dstbegin + (srcEnd-srcBegin) - 1
Parameters:
srcBegin- start copying at this offset.
srcEnd- stop copying at this offset.
dst- the array to copy the data into.
dstBegin- offset intodst.
Throws:
NullPointerException- ifdstisnull.
IndexOutOfBoundsException- if any of the following is true:
  • srcBeginis negative
  • dstBeginis negative
  • thesrcBeginargument is greater than thesrcEndargument.
  • srcEndis greater thanthis.length().
  • dstBegin+srcEnd-srcBeginis greater thandst.length

setCharAt

public voidsetCharAt(int index, char ch)
The character at the specified index is set toch. This sequence is altered to represent a new character sequence that is identical to the old character sequence, except that it contains the characterchat positionindex.

The index argument must be greater than or equal to0, 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:
length()

append

publicStringBufferappend(Objectobj)
Appends the string representation of theObjectargument.

The argument is converted to a string as if by the methodString.valueOf, and the characters of that string are then appended to this sequence.

Parameters:
obj- anObject.
Returns:
a reference to this object.
See Also:
String.valueOf(java.lang.Object),append(java.lang.String)

append

publicStringBufferappend(Stringstr)
Appends the specified string to this character sequence.

The characters of theStringargument are appended, in order, increasing the length of this sequence by the length of the argument. Ifstrisnull, then the four characters"null"are appended.

Letnbe the length of this character sequence just prior to execution of theappendmethod. Then the character at indexkin the new character sequence is equal to the character at indexkin the old character sequence, ifkis less thann; otherwise, it is equal to the character at indexk-nin the argumentstr.

Parameters:
str- a string.
Returns:
a reference to this object.

append

publicStringBufferappend(StringBuffersb)
Appends the specifiedStringBufferto this sequence.

The characters of theStringBufferargument are appended, in order, to the contents of thisStringBuffer, increasing the length of thisStringBufferby the length of the argument. Ifsbisnull, then the four characters"null"are appended to thisStringBuffer.

Letnbe the length of the old character sequence, the one contained in theStringBufferjust prior to execution of theappendmethod. Then the character at indexkin the new character sequence is equal to the character at indexkin the old character sequence, ifkis less thann; otherwise, it is equal to the character at indexk-nin the argumentsb.

This method synchronizes onthis(the destination) object but does not synchronize on the source (sb).

Parameters:
sb- theStringBufferto append.
Returns:
a reference to this object.
Since:
1.4

append

publicStringBufferappend(CharSequences)
Appends the specifiedCharSequenceto this sequence.

The characters of theCharSequenceargument 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 if it were an invocation of this.append(s, 0, s.length());

This method synchronizes on this (the destination) object but does not synchronize on the source (s).

Ifsisnull, 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

publicStringBufferappend(CharSequences, int start, int end)
Appends a subsequence of the specifiedCharSequenceto this sequence.

Characters of the arguments, starting at indexstart, are appended, in order, to the contents of this sequence up to the (exclusive) indexend. The length of this sequence is increased by the value ofend - start.

Letnbe the length of this character sequence just prior to execution of theappendmethod. Then the character at indexkin this character sequence becomes equal to the character at indexkin this sequence, ifkis less thann; otherwise, it is equal to the character at indexk+start-nin the arguments.

Ifsisnull, then this method appends characters as if the s parameter was a sequence containing the four characters"null".

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- ifstartorendare negative, orstartis greater thanendorendis greater thans.length()
Since:
1.5

append

publicStringBufferappend(char[] str)
Appends the string representation of thechararray argument 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 exactly as if the argument were converted to a string by the methodString.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

publicStringBufferappend(char[] str, int offset, int len)
Appends the string representation of a subarray of thechararray argument to this sequence.

Characters of thechararraystr, starting at indexoffset, are appended, in order, to the contents of this sequence. The length of this sequence increases by the value oflen.

The overall effect is exactly as if the arguments were converted to a string by the methodString.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.

append

publicStringBufferappend(boolean b)
Appends the string representation of thebooleanargument to the sequence.

The argument is converted to a string as if by the methodString.valueOf, and the characters of that string are then appended to this sequence.

Parameters:
b- aboolean.
Returns:
a reference to this object.
See Also:
String.valueOf(boolean),append(java.lang.String)

append

publicStringBufferappend(char c)
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 by1.

The overall effect is exactly as if the argument were converted to a string by the methodString.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

publicStringBufferappend(int i)
Appends the string representation of theintargument to this sequence.

The argument is converted to a string as if by the methodString.valueOf, and the characters of that string are then appended to this sequence.

Parameters:
i- anint.
Returns:
a reference to this object.
See Also:
String.valueOf(int),append(java.lang.String)

appendCodePoint

publicStringBufferappendCodePoint(int codePoint)
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 byCharacter.charCount(codePoint).

The overall effect is exactly as if the argument were converted to achararray by the methodCharacter.toChars(int)and the character in that array were thenappendedto this character sequence.

Parameters:
codePoint- a Unicode code point
Returns:
a reference to this object.
Since:
1.5

append

publicStringBufferappend(long lng)
Appends the string representation of thelongargument to this sequence.

The argument is converted to a string as if by the methodString.valueOf, and the characters of that string are then appended to this sequence.

Parameters:
lng- along.
Returns:
a reference to this object.
See Also:
String.valueOf(long),append(java.lang.String)

append

publicStringBufferappend(float f)
Appends the string representation of thefloatargument to this sequence.

The argument is converted to a string as if by the methodString.valueOf, and the characters of that string are then appended to this string sequence.

Parameters:
f- afloat.
Returns:
a reference to this object.
See Also:
String.valueOf(float),append(java.lang.String)

append

publicStringBufferappend(double d)
Appends the string representation of thedoubleargument to this sequence.

The argument is converted to a string as if by the methodString.valueOf, and the characters of that string are then appended to this sequence.

Parameters:
d- adouble.
Returns:
a reference to this object.
See Also:
String.valueOf(double),append(java.lang.String)

delete

publicStringBufferdelete(int start, int end)
Removes the characters in a substring of this sequence. The substring begins at the specifiedstartand extends to the character at indexend - 1or to the end of the sequence 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

publicStringBufferdeleteCharAt(int index)
Removes thecharat the specified position in this sequence. This sequence is shortened by onechar.

Note: If the character at the given index is a supplementary character, this method does not remove the entire character. If correct handling of supplementary characters is required, determine the number ofchars 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

publicStringBufferreplace(int start, int end,Stringstr)
Replaces the characters in a substring of this sequence with characters in the specifiedString. The substring begins at the specifiedstartand extends to the character at indexend - 1or to the end of the sequence if no such character exists. First the characters in the substring are removed and then the specifiedStringis inserted atstart. (This sequence will be lengthened to accommodate the specified 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

publicStringsubstring(int start)
Returns a newStringthat contains a subsequence of characters currently contained in this character sequence. The substring begins at the specified index and extends to the end of this sequence.
Parameters:
start- The beginning index, inclusive.
Returns:
The new string.
Throws:
StringIndexOutOfBoundsException- ifstartis less than zero, or greater than the length of this object.
Since:
1.2

subSequence

publicCharSequencesubSequence(int start, int end)
Returns a new character sequence that is a subsequence of this sequence.

An invocation of this method of the form

sb.subSequence(begin, end)
behaves in exactly the same way as the invocation
sb.substring(begin, end)
This method is provided so that this class can implement theCharSequenceinterface.
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

publicStringsubstring(int start, int end)
Returns a newStringthat contains a subsequence of characters currently contained in this sequence. The substring begins at the specifiedstartand extends to the character at indexend - 1.
Parameters:
start- The beginning index, inclusive.
end- The ending index, exclusive.
Returns:
The new string.
Throws:
StringIndexOutOfBoundsException- ifstartorendare negative or greater thanlength(), orstartis greater thanend.
Since:
1.2

insert

publicStringBufferinsert(int index, char[] str, int offset, int len)
Inserts the string representation of a subarray of thestrarray argument into this sequence. The subarray begins at the specifiedoffsetand extendslenchars. The characters of the subarray are inserted into this sequence at the position indicated byindex. The length of this sequence 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

publicStringBufferinsert(int offset,Objectobj)
Inserts the string representation of theObjectargument into this character sequence.

The second argument is converted to a string as if by the methodString.valueOf, and the characters of that string are then inserted into this sequence at the indicated offset.

The offset argument must be greater than or equal to0, and less than or equal to the length of this sequence.

Parameters:
offset- the offset.
obj- anObject.
Returns:
a reference to this object.
Throws:
StringIndexOutOfBoundsException- if the offset is invalid.
See Also:
String.valueOf(java.lang.Object),insert(int, java.lang.String),length()

insert

publicStringBufferinsert(int offset,Stringstr)
Inserts the string into this character sequence.

The characters of theStringargument are inserted, in order, into this sequence at the indicated offset, moving up any characters originally above that position and increasing the length of this sequence by the length of the argument. Ifstrisnull, then the four characters"null"are inserted into this sequence.

The character at indexkin the new character sequence is equal to:

  • the character at indexkin the old character sequence, ifkis less thanoffset
  • the character at indexk-offsetin the argumentstr, ifkis not less thanoffsetbut is less thanoffset+str.length()
  • the character at indexk-str.length()in the old character sequence, ifkis not less thanoffset+str.length()

The offset argument must be greater than or equal to0, and less than or equal to the length of this sequence.

Parameters:
offset- the offset.
str- a string.
Returns:
a reference to this object.
Throws:
StringIndexOutOfBoundsException- if the offset is invalid.
See Also:
length()

insert

publicStringBufferinsert(int offset, char[] str)
Inserts the string representation of thechararray argument into this sequence.

The characters of the array argument are inserted into the contents of this sequence at the position indicated byoffset. The length of this sequence increases by the length of the argument.

The overall effect is exactly as if the argument were converted to a string by the methodString.valueOf(char[])and the characters of that string were theninsertedinto this character sequence at the position indicated byoffset.

Parameters:
offset- the offset.
str- a character array.
Returns:
a reference to this object.
Throws:
StringIndexOutOfBoundsException- if the offset is invalid.

insert

publicStringBufferinsert(int dstOffset,CharSequences)
Inserts the specifiedCharSequenceinto this sequence.

The characters of theCharSequenceargument are inserted, in order, into this sequence at the indicated offset, moving up any characters originally above that position and increasing the length of this sequence by the length of the argument s.

The result of this method is exactly the same as if it were an invocation of this object's insert(dstOffset, s, 0, s.length()) method.

Ifsisnull, 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

publicStringBufferinsert(int dstOffset,CharSequences, int start, int end)
Inserts a subsequence of the specifiedCharSequenceinto this sequence.

The subsequence of the argumentsspecified bystartandendare inserted, in order, into this sequence at the specified destination offset, moving up any characters originally above that position. The length of this sequence is increased byend - start.

The character at indexkin this sequence becomes equal to:

  • the character at indexkin this sequence, ifkis less thandstOffset
  • the character at indexk+start-dstOffsetin the arguments, ifkis greater than or equal todstOffsetbut is less thandstOffset+end-start
  • the character at indexk-(end-start)in this sequence, ifkis greater than or equal todstOffset+end-start

The dstOffset argument must be greater than or equal to0, and less than or equal to the length of this sequence.

The start argument must be nonnegative, and not greater thanend.

The end argument must be greater than or equal tostart, and less than or equal to the length of s.

Ifsisnull, then this method inserts characters as if the s parameter was a sequence containing the four characters"null".

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

insert

publicStringBufferinsert(int offset, boolean b)
Inserts the string representation of thebooleanargument into this sequence.

The second argument is converted to a string as if by the methodString.valueOf, and the characters of that string are then inserted into this sequence at the indicated offset.

The offset argument must be greater than or equal to0, and less than or equal to the length of this sequence.

Parameters:
offset- the offset.
b- aboolean.
Returns:
a reference to this object.
Throws:
StringIndexOutOfBoundsException- if the offset is invalid.
See Also:
String.valueOf(boolean),insert(int, java.lang.String),length()

insert

publicStringBufferinsert(int offset, char c)
Inserts the string representation of thecharargument into this sequence.

The second argument is inserted into the contents of this sequence at the position indicated byoffset. The length of this sequence increases by one.

The overall effect is exactly as if the argument were converted to a string by the methodString.valueOf(char)and the character in that string were theninsertedinto this character sequence at the position indicated byoffset.

The offset argument must be greater than or equal to0, and less than or equal to the length of this sequence.

Parameters:
offset- the offset.
c- achar.
Returns:
a reference to this object.
Throws:
IndexOutOfBoundsException- if the offset is invalid.
See Also:
length()

insert

publicStringBufferinsert(int offset, int i)
Inserts the string representation of the secondintargument into this sequence.

The second argument is converted to a string as if by the methodString.valueOf, and the characters of that string are then inserted into this sequence at the indicated offset.

The offset argument must be greater than or equal to0, and less than or equal to the length of this sequence.

Parameters:
offset- the offset.
i- anint.
Returns:
a reference to this object.
Throws:
StringIndexOutOfBoundsException- if the offset is invalid.
See Also:
String.valueOf(int),insert(int, java.lang.String),length()

insert

publicStringBufferinsert(int offset, long l)
Inserts the string representation of thelongargument into this sequence.

The second argument is converted to a string as if by the methodString.valueOf, and the characters of that string are then inserted into this sequence at the position indicated byoffset.

The offset argument must be greater than or equal to0, and less than or equal to the length of this sequence.

Parameters:
offset- the offset.
l- along.
Returns:
a reference to this object.
Throws:
StringIndexOutOfBoundsException- if the offset is invalid.
See Also:
String.valueOf(long),insert(int, java.lang.String),length()

insert

publicStringBufferinsert(int offset, float f)
Inserts the string representation of thefloatargument into this sequence.

The second argument is converted to a string as if by the methodString.valueOf, and the characters of that string are then inserted into this sequence at the indicated offset.

The offset argument must be greater than or equal to0, and less than or equal to the length of this sequence.

Parameters:
offset- the offset.
f- afloat.
Returns:
a reference to this object.
Throws:
StringIndexOutOfBoundsException- if the offset is invalid.
See Also:
String.valueOf(float),insert(int, java.lang.String),length()

insert

publicStringBufferinsert(int offset, double d)
Inserts the string representation of thedoubleargument into this sequence.

The second argument is converted to a string as if by the methodString.valueOf, and the characters of that string are then inserted into this sequence at the indicated offset.

The offset argument must be greater than or equal to0, and less than or equal to the length of this sequence.

Parameters:
offset- the offset.
d- adouble.
Returns:
a reference to this object.
Throws:
StringIndexOutOfBoundsException- if the offset is invalid.
See Also:
String.valueOf(double),insert(int, java.lang.String),length()

indexOf

public intindexOf(Stringstr)
Returns the index within this string of the first occurrence of the specified substring. The integer returned is the smallest valueksuch that:
this.toString().startsWith(str,k)
istrue.
Parameters:
str- any string.
Returns:
if the string argument occurs as a substring within this object, then the index of the first character of the first such substring is returned; if it does not occur as a substring,-1is returned.
Throws:
NullPointerException- ifstrisnull.
Since:
1.4

indexOf

public intindexOf(Stringstr, int fromIndex)
Returns the index within this string of the first occurrence of the specified substring, starting at the specified index. The integer returned is the smallest valuekfor which:
k >= Math.min(fromIndex, str.length()) && this.toString().startsWith(str, k)
If no such value ofkexists, then -1 is returned.
Parameters:
str- the substring for which to search.
fromIndex- the index from which to start the search.
Returns:
the index within this string of the first occurrence of the specified substring, starting at the specified index.
Throws:
NullPointerException- ifstrisnull.
Since:
1.4

lastIndexOf

public intlastIndexOf(Stringstr)
Returns the index within this string of the rightmost occurrence of the specified substring. The rightmost empty string "" is considered to occur at the index valuethis.length(). The returned index is the largest valueksuch that
this.toString().startsWith(str, k)
is true.
Parameters:
str- the substring to search for.
Returns:
if the string argument occurs one or more times as a substring within this object, then the index of the first character of the last such substring is returned. If it does not occur as a substring,-1is returned.
Throws:
NullPointerException- ifstrisnull.
Since:
1.4

lastIndexOf

public intlastIndexOf(Stringstr, int fromIndex)
Returns the index within this string of the last occurrence of the specified substring. The integer returned is the largest valueksuch that:
k <= Math.min(fromIndex,="Math.min(fromIndex," str.length())="str.length())" &&="&&" this.toString().startsWith(str,="this.toString().startsWith(str," k)="k)"
If no such value ofkexists, then -1 is returned.
Parameters:
str- the substring to search for.
fromIndex- the index to start the search from.
Returns:
the index within this sequence of the last occurrence of the specified substring.
Throws:
NullPointerException- ifstrisnull.
Since:
1.4

reverse

publicStringBufferreverse()
Causes this character sequence to be replaced by the reverse of the sequence. If there are any surrogate pairs included in the sequence, these are treated as single characters for the reverse operation. Thus, the order of the high-low surrogates is never reversed. Letnbe the character length of this character sequence (not the length incharvalues) just prior to execution of thereversemethod. Then the character at indexkin the new character sequence is equal to the character at indexn-k-1in the old character sequence.

Note that the reverse operation may result in producing surrogate pairs that were unpaired low-surrogates and high-surrogates before the operation. For example, reversing "\uDC00\uD800" produces "\uD800\uDC00" which is a valid surrogate pair.

Returns:
a reference to this object.
Since:
JDK1.0.2

toString

publicStringtoString()
Returns a string representing the data in this sequence. A newStringobject is allocated and initialized to contain the character sequence currently represented by this object. ThisStringis then returned. Subsequent changes to this sequence do not affect the contents of theString.
Specified by:
toStringin interfaceCharSequence
Returns:
a string representation of this sequence of characters.

Overview Package Class Use Tree Deprecated Index Help
Java™ Platform
Standard Ed. 6
PREV CLASSNEXT CLASS FRAMESNO FRAMESAll Classes
SUMMARY: NESTED | FIELD |CONSTR|METHOD DETAIL: FIELD |CONSTR|METHOD

Submit a bug or feature
For further API reference and developer documentation, seeJava SE Developer Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.

Copyright 2006 Sun Microsystems, Inc. All rights reserved. Use is subject tolicense terms. Also see thedocumentation redistribution policy.

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics