|
|||||||||
| Home >> All >> java >> [ lang overview ] | PREV CLASS NEXT CLASS | ||||||||
SUMMARY: JAVADOC | SOURCE | DOWNLOAD | NESTED | FIELD | CONSTR | METHOD |
DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang
Class StringBuilder

java.lang.Objectjava.lang.StringBuilder
- All Implemented Interfaces:
- CharSequence, java.io.Serializable
- public final class StringBuilder
- extends Object
- implements java.io.Serializable, CharSequence
- extends Object
StringBuilder represents a changeable String.
It provides the operations required to modify the
StringBuilder, including insert, replace, delete, append,
and reverse. It like StringBuffer, but is not
synchronized. It is ideal for use when it is known that the
object will only be used from a single thread.
StringBuilders are variable-length in nature, so even if
you initialize them to a certain size, they can still grow larger than
that. Capacity indicates the number of characters the
StringBuilder can have in it before it has to grow (growing
the char array is an expensive operation involving new).
Incidentally, compilers often implement the String operator "+"
by using a StringBuilder operation:
a + b
is the same as
new StringBuilder().append(a).append(b).toString().
Classpath's StringBuilder is capable of sharing memory with Strings for efficiency. This will help when a StringBuilder is converted to a String and the StringBuilder is not changed after that (quite common when performing string concatenation).
- Since:
- 1.5
| Field Summary | |
(package private) int |
count
Index of next available character (and thus the size of the current string contents). |
private static int |
DEFAULT_CAPACITY
The default capacity of a buffer. |
private static long |
serialVersionUID
For compatability with Sun's JDK |
(package private) char[] |
value
The buffer. |
| Constructor Summary | |
StringBuilder()
Create a new StringBuilder with default capacity 16. |
|
StringBuilder(CharSequence seq)
Create a new StringBuilder with the characters in the
specified CharSequence. |
|
StringBuilder(int capacity)
Create an empty StringBuilder with the specified initial
capacity. |
|
StringBuilder(String str)
Create a new StringBuilder with the characters in the
specified String. |
|
| Method Summary | |
StringBuilder |
append(boolean bool)
Append the String value of the argument to this
StringBuilder. |
StringBuilder |
append(char ch)
Append the char to this StringBuilder. |
StringBuilder |
append(char[] data)
Append the char array to this StringBuilder. |
StringBuilder |
append(char[] data,
int offset,
int count)
Append part of the char array to this
StringBuilder. |
StringBuilder |
append(CharSequence seq)
Append the characters in the CharSequence to this
buffer. |
StringBuilder |
append(CharSequence seq,
int start,
int end)
Append some characters from the CharSequence to this
buffer. |
StringBuilder |
append(double dnum)
Append the String value of the argument to this
StringBuilder. |
StringBuilder |
append(float fnum)
Append the String value of the argument to this
StringBuilder. |
StringBuilder |
append(int inum)
Append the String value of the argument to this
StringBuilder. |
StringBuilder |
append(long lnum)
Append the String value of the argument to this
StringBuilder. |
StringBuilder |
append(Object obj)
Append the String value of the argument to this
StringBuilder. |
StringBuilder |
append(String str)
Append the String to this StringBuilder. |
StringBuilder |
append(StringBuffer stringBuffer)
Append the StringBuilder value of the argument to this
StringBuilder. |
StringBuilder |
appendCodePoint(int code)
Append the code point to this StringBuilder. |
int |
capacity()
Get the total number of characters this StringBuilder can
support before it must be grown. |
char |
charAt(int index)
Get the character at the specified index. |
int |
codePointAt(int index)
Get the code point at the specified index. |
int |
codePointBefore(int index)
Get the code point before the specified index. |
int |
codePointCount(int beginIndex,
int endIndex)
Returns the number of Unicode code points in the specified sub sequence. |
StringBuilder |
delete(int start,
int end)
Delete characters from this StringBuilder. |
StringBuilder |
deleteCharAt(int index)
Delete a character from this StringBuilder. |
void |
ensureCapacity(int minimumCapacity)
Increase the capacity of this StringBuilder. |
void |
getChars(int srcOffset,
int srcEnd,
char[] dst,
int dstOffset)
Get the specified array of characters. |
int |
indexOf(String str)
Finds the first instance of a substring in this StringBuilder. |
int |
indexOf(String str,
int fromIndex)
Finds the first instance of a String in this StringBuilder, starting at a given index. |
StringBuilder |
insert(int offset,
boolean bool)
Insert the String value of the argument into this
StringBuilder. |
StringBuilder |
insert(int offset,
char ch)
Insert the char argument into this StringBuilder. |
StringBuilder |
insert(int offset,
char[] data)
Insert the char[] argument into this
StringBuilder. |
StringBuilder |
insert(int offset,
char[] str,
int str_offset,
int len)
Insert a subarray of the char[] argument into this
StringBuilder. |
StringBuilder |
insert(int offset,
CharSequence sequence)
Insert the CharSequence argument into this
StringBuilder. |
StringBuilder |
insert(int offset,
CharSequence sequence,
int start,
int end)
Insert a subsequence of the CharSequence argument into this
StringBuilder. |
StringBuilder |
insert(int offset,
double dnum)
Insert the String value of the argument into this
StringBuilder. |
StringBuilder |
insert(int offset,
float fnum)
Insert the String value of the argument into this
StringBuilder. |
StringBuilder |
insert(int offset,
int inum)
Insert the String value of the argument into this
StringBuilder. |
StringBuilder |
insert(int offset,
long lnum)
Insert the String value of the argument into this
StringBuilder. |
StringBuilder |
insert(int offset,
Object obj)
Insert the String value of the argument into this
StringBuilder. |
StringBuilder |
insert(int offset,
String str)
Insert the String argument into this
StringBuilder. |
int |
lastIndexOf(String str)
Finds the last instance of a substring in this StringBuilder. |
int |
lastIndexOf(String str,
int fromIndex)
Finds the last instance of a String in this StringBuilder, starting at a given index. |
int |
length()
Get the length of the String this StringBuilder
would create. |
private boolean |
regionMatches(int toffset,
String other)
Predicate which determines if a substring of this matches another String starting at a specified offset for each String and continuing for a specified length. |
StringBuilder |
replace(int start,
int end,
String str)
Replace characters between index start (inclusive) and
end (exclusive) with str. |
StringBuilder |
reverse()
Reverse the characters in this StringBuilder. |
void |
setCharAt(int index,
char ch)
Set the character at the specified index. |
void |
setLength(int newLength)
Set the length of this StringBuilder. |
CharSequence |
subSequence(int beginIndex,
int endIndex)
Creates a substring of this StringBuilder, starting at a specified index and ending at one character before a specified index. |
String |
substring(int beginIndex)
Creates a substring of this StringBuilder, starting at a specified index and ending at the end of this StringBuilder. |
String |
substring(int beginIndex,
int endIndex)
Creates a substring of this StringBuilder, starting at a specified index and ending at one character before a specified index. |
String |
toString()
Convert this StringBuilder to a String. |
void |
trimToSize()
|
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
serialVersionUID
private static final long serialVersionUID
- For compatability with Sun's JDK
- See Also:
- Constant Field Values
count
int count
- Index of next available character (and thus the size of the current
string contents). Note that this has permissions set this way so that
String can get the value.
value
char[] value
- The buffer. Note that this has permissions set this way so that String
can get the value.
DEFAULT_CAPACITY
private static final int DEFAULT_CAPACITY
- The default capacity of a buffer.
- See Also:
- Constant Field Values
| Constructor Detail |
StringBuilder
public StringBuilder()
- Create a new StringBuilder with default capacity 16.
StringBuilder
public StringBuilder(int capacity)
- Create an empty
StringBuilderwith the specified initial capacity.
StringBuilder
public StringBuilder(String str)
- Create a new
StringBuilderwith the characters in the specifiedString. Initial capacity will be the size of the String plus 16.
StringBuilder
public StringBuilder(CharSequence seq)
- Create a new
StringBuilderwith the characters in the specifiedCharSequence. Initial capacity will be the length of the sequence plus 16; if the sequence reports a length less than or equal to 0, then the initial capacity will be 16.
| Method Detail |
length
public int length()
- Get the length of the
StringthisStringBuilderwould create. Not to be confused with the capacity of theStringBuilder.- Specified by:
lengthin interfaceCharSequence
capacity
public int capacity()
- Get the total number of characters this
StringBuildercan support before it must be grown. Not to be confused with length.
ensureCapacity
public void ensureCapacity(int minimumCapacity)
- Increase the capacity of this
StringBuilder. This will ensure that an expensive growing operation will not occur untilminimumCapacityis reached. The buffer is grown to the larger ofminimumCapacityandcapacity() * 2 + 2, if it is not already large enough.
setLength
public void setLength(int newLength)
- Set the length of this StringBuilder. If the new length is greater than
the current length, all the new characters are set to '\0'. If the new
length is less than the current length, the first
newLengthcharacters of the old array will be preserved, and the remaining characters are truncated.
charAt
public char charAt(int index)
- Get the character at the specified index.
- Specified by:
charAtin interfaceCharSequence
getChars
public void getChars(int srcOffset,
int srcEnd,
char[] dst,
int dstOffset)
- Get the specified array of characters.
srcOffset - srcEndcharacters will be copied into the array you pass in.
setCharAt
public void setCharAt(int index,
char ch)
- Set the character at the specified index.
append
public StringBuilder append(Object obj)
- Append the
Stringvalue of the argument to thisStringBuilder. UsesString.valueOf()to convert toString.
append
public StringBuilder append(String str)
- Append the
Stringto thisStringBuilder. If str is null, the String "null" is appended.
append
public StringBuilder append(StringBuffer stringBuffer)
- Append the
StringBuildervalue of the argument to thisStringBuilder. This behaves the same asappend((Object) stringBuffer), except it is more efficient.
append
public StringBuilder append(char[] data)
- Append the
chararray to thisStringBuilder. This is similar (but more efficient) thanappend(new String(data)), except in the case of null.
append
public StringBuilder append(char[] data, int offset, int count)
- Append part of the
chararray to thisStringBuilder. This is similar (but more efficient) thanappend(new String(data, offset, count)), except in the case of null.
append
public StringBuilder append(boolean bool)
- Append the
Stringvalue of the argument to thisStringBuilder. UsesString.valueOf()to convert toString.
append
public StringBuilder append(char ch)
- Append the
charto thisStringBuilder.
append
public StringBuilder append(CharSequence seq)
- Append the characters in the
CharSequenceto this buffer.
append
public StringBuilder append(CharSequence seq, int start, int end)
- Append some characters from the
CharSequenceto this buffer. If the argument is null, the four characters "null" are appended.
appendCodePoint
public StringBuilder appendCodePoint(int code)
- Append the code point to this
StringBuilder. This is like #append(char), but will append two characters if a supplementary code point is given.- Since:
- 1.5
append
public StringBuilder append(int inum)
- Append the
Stringvalue of the argument to thisStringBuilder. UsesString.valueOf()to convert toString.
append
public StringBuilder append(long lnum)
- Append the
Stringvalue of the argument to thisStringBuilder. UsesString.valueOf()to convert toString.
append
public StringBuilder append(float fnum)
- Append the
Stringvalue of the argument to thisStringBuilder. UsesString.valueOf()to convert toString.
append
public StringBuilder append(double dnum)
- Append the
Stringvalue of the argument to thisStringBuilder. UsesString.valueOf()to convert toString.
delete
public StringBuilder delete(int start, int end)
- Delete characters from this
StringBuilder.delete(10, 12)will delete 10 and 11, but not 12. It is harmless for end to be larger than length().
deleteCharAt
public StringBuilder deleteCharAt(int index)
- Delete a character from this
StringBuilder.
replace
public StringBuilder replace(int start, int end, String str)
- Replace characters between index
start(inclusive) andend(exclusive) withstr. Ifendis larger than the size of this StringBuilder, all characters afterstartare replaced.
substring
public String substring(int beginIndex)
- Creates a substring of this StringBuilder, starting at a specified index
and ending at the end of this StringBuilder.
subSequence
public CharSequence subSequence(int beginIndex, int endIndex)
- Creates a substring of this StringBuilder, starting at a specified index
and ending at one character before a specified index. This is implemented
the same as
substring(beginIndex, endIndex), to satisfy the CharSequence interface.- Specified by:
subSequencein interfaceCharSequence
substring
public String substring(int beginIndex, int endIndex)
- Creates a substring of this StringBuilder, starting at a specified index
and ending at one character before a specified index.
insert
public StringBuilder insert(int offset, char[] str, int str_offset, int len)
- Insert a subarray of the
char[]argument into thisStringBuilder.
insert
public StringBuilder insert(int offset, Object obj)
- Insert the
Stringvalue of the argument into thisStringBuilder. UsesString.valueOf()to convert toString.
insert
public StringBuilder insert(int offset, String str)
- Insert the
Stringargument into thisStringBuilder. If str is null, the String "null" is used instead.
insert
public StringBuilder insert(int offset, CharSequence sequence)
- Insert the
CharSequenceargument into thisStringBuilder. If the sequence is null, the String "null" is used instead.
insert
public StringBuilder insert(int offset, CharSequence sequence, int start, int end)
- Insert a subsequence of the
CharSequenceargument into thisStringBuilder. If the sequence is null, the String "null" is used instead.
insert
public StringBuilder insert(int offset, char[] data)
- Insert the
char[]argument into thisStringBuilder.
insert
public StringBuilder insert(int offset, boolean bool)
- Insert the
Stringvalue of the argument into thisStringBuilder. UsesString.valueOf()to convert toString.
insert
public StringBuilder insert(int offset, char ch)
- Insert the
charargument into thisStringBuilder.
insert
public StringBuilder insert(int offset, int inum)
- Insert the
Stringvalue of the argument into thisStringBuilder. UsesString.valueOf()to convert toString.
insert
public StringBuilder insert(int offset, long lnum)
- Insert the
Stringvalue of the argument into thisStringBuilder. UsesString.valueOf()to convert toString.
insert
public StringBuilder insert(int offset, float fnum)
- Insert the
Stringvalue of the argument into thisStringBuilder. UsesString.valueOf()to convert toString.
insert
public StringBuilder insert(int offset, double dnum)
- Insert the
Stringvalue of the argument into thisStringBuilder. UsesString.valueOf()to convert toString.
indexOf
public int indexOf(String str)
- Finds the first instance of a substring in this StringBuilder.
indexOf
public int indexOf(String str, int fromIndex)
- Finds the first instance of a String in this StringBuilder, starting at
a given index. If starting index is less than 0, the search starts at
the beginning of this String. If the starting index is greater than the
length of this String, or the substring is not found, -1 is returned.
lastIndexOf
public int lastIndexOf(String str)
- Finds the last instance of a substring in this StringBuilder.
lastIndexOf
public int lastIndexOf(String str, int fromIndex)
- Finds the last instance of a String in this StringBuilder, starting at a
given index. If starting index is greater than the maximum valid index,
then the search begins at the end of this String. If the starting index
is less than zero, or the substring is not found, -1 is returned.
reverse
public StringBuilder reverse()
- Reverse the characters in this StringBuilder. The same sequence of
characters exists, but in the reverse index ordering.
toString
public String toString()
- Convert this
StringBuilderto aString. The String is composed of the characters currently in this StringBuilder. Note that the result is a copy, and that future modifications to this buffer do not affect the String.- Specified by:
toStringin interfaceCharSequence- Overrides:
toStringin classObject
regionMatches
private boolean regionMatches(int toffset,
String other)
- Predicate which determines if a substring of this matches another String
starting at a specified offset for each String and continuing for a
specified length. This is more efficient than creating a String to call
indexOf on.
codePointAt
public int codePointAt(int index)
- Get the code point at the specified index. This is like #charAt(int),
but if the character is the start of a surrogate pair, and the
following character completes the pair, then the corresponding
supplementary code point is returned.
- Since:
- 1.5
codePointBefore
public int codePointBefore(int index)
- Get the code point before the specified index. This is like
#codePointAt(int), but checks the characters at
index-1andindex-2to see if they form a supplementary code point.- Since:
- 1.5
codePointCount
public int codePointCount(int beginIndex,
int endIndex)
- Returns the number of Unicode code points in the specified sub sequence.
Surrogate pairs count as one code point.
trimToSize
public void trimToSize()
|
|||||||||
| Home >> All >> java >> [ lang overview ] | PREV CLASS NEXT CLASS | ||||||||
SUMMARY: JAVADOC | SOURCE | DOWNLOAD | NESTED | FIELD | CONSTR | METHOD |
DETAIL: FIELD | CONSTR | METHOD | ||||||||
JAVADOC
java.lang.StringBuilder