Class CStringBuilder
This is generally the Apache String builder class refactored here so we can
use it regardless of vm limitations/differences for increased performance.
A modifiable sequence of characters for use in creating
and modifying Strings. This class is intended as a direct replacement of
StringBuffer for non-concurrent use; unlike StringBuffer this
class is not synchronized for thread safety.
The majority of the modification methods on this class return StringBuilder, so that, like StringBuffers, they can be used in
chaining method calls together. For example, new StringBuilder("One should ").append("always strive ").append("to achieve Harmony").
Since
1.5
Deprecated
we will be moving to the proper string builder very soon
See also
-
CharSequence
-
Appendable
-
StringBuffer
-
String
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs an instance with an initial capacity of16.CStringBuilder(int capacity) Constructs an instance with the specified capacity.CStringBuilder(String str) Constructs an instance that's initialized with the contents of the specifiedString. -
Method Summary
Modifier and TypeMethodDescriptionappend(boolean b) Appends the string representation of the specifiedbooleanvalue.append(char c) Appends the string representation of the specifiedcharvalue.append(char[] ch) Appends the string representation of the specifiedchar[].append(char[] str, int offset, int len) Appends the string representation of the specified subset of thechar[].append(double d) Appends the string representation of the specifieddoublevalue.append(float f) Appends the string representation of the specifiedfloatvalue.append(int i) Appends the string representation of the specifiedintvalue.append(long lng) Appends the string representation of the specifiedlongvalue.Appends the string representation of the specifiedObject.Appends the contents of the specified string.appendCodePoint(int codePoint) Appends the encoded Unicode code point.intcapacity()Returns the number of characters that can be held without growing.charcharAt(int index) Retrieves the character at theindex.intcodePointAt(int index) Retrieves the Unicode code point value at theindex.intcodePointBefore(int index) Retrieves the Unicode code point value that precedes theindex.intcodePointCount(int beginIndex, int endIndex) Calculates the number of Unicode code points betweenbeginIndexandendIndex.delete(int start, int end) Deletes a sequence of characters specified bystartandend.deleteCharAt(int index) Deletes the character at the specified index.voidensureCapacity(int min) Ensures that this object has a minimum capacity available before requiring the internal buffer to be enlarged.voidgetChars(int start, int end, char[] dest, int destStart) Copies the requested sequence of characters to thechar[]passed starting atdestStart.intSearches for the first index of the specified character.intSearches for the index of the specified character.insert(int offset, boolean b) Inserts the string representation of the specifiedbooleanvalue at the specifiedoffset.insert(int offset, char c) Inserts the string representation of the specifiedcharvalue at the specifiedoffset.insert(int offset, char[] ch) Inserts the string representation of the specifiedchar[]at the specifiedoffset.insert(int offset, char[] str, int strOffset, int strLen) Inserts the string representation of the specified subsequence of thechar[]at the specifiedoffset.insert(int offset, double d) Inserts the string representation of the specifieddoublevalue at the specifiedoffset.insert(int offset, float f) Inserts the string representation of the specifiedfloatvalue at the specifiedoffset.insert(int offset, int i) Inserts the string representation of the specifiedintvalue at the specifiedoffset.insert(int offset, long l) Inserts the string representation of the specifiedlongvalue at the specifiedoffset.Inserts the string representation of the specifiedObjectat the specifiedoffset.Inserts the specified string at the specifiedoffset.intlastIndexOf(String string) Searches for the last index of the specified character.intlastIndexOf(String subString, int start) Searches for the index of the specified character.intlength()The current length.intoffsetByCodePoints(int index, int codePointOffset) Returns the index that is offsetcodePointOffsetcode points fromindex.Replaces the specified subsequence in this builder with the specified string.reverse()Reverses the order of characters in this builder.voidsetCharAt(int index, char ch) Sets the character at theindex.voidsetLength(int length) Sets the current length to a new value.substring(int start) Returns the String value of the subsequence from thestartindex to the current end.substring(int start, int end) Returns the String value of the subsequence from thestartindex to theendindex.toString()Returns the contents of this builder.voidTrims off any extra capacity beyond the current length.
-
Constructor Details
-
CStringBuilder
public CStringBuilder()Constructs an instance with an initial capacity of
16.See also
- #capacity()
-
CStringBuilder
public CStringBuilder(int capacity) Constructs an instance with the specified capacity.
Parameters
capacity: the initial capacity to use.
Throws
NegativeArraySizeException: if the specifiedcapacityis negative.
See also
- #capacity()
-
CStringBuilder
Constructs an instance that's initialized with the contents of the specified
String. The capacity of the new builder will be the length of theStringplus 16.Parameters
str: theStringto copy into the builder.
Throws
NullPointerException: ifstrisnull.
-
-
Method Details
-
append
Appends the string representation of the specified
booleanvalue. Thebooleanvalue is converted to a String according to the rule defined byString#valueOf(boolean).Parameters
b: thebooleanvalue to append.
Returns
this builder.
See also
- String#valueOf(boolean)
-
append
Appends the string representation of the specified
charvalue. Thecharvalue is converted to a string according to the rule defined byString#valueOf(char).Parameters
c: thecharvalue to append.
Returns
this builder.
See also
- String#valueOf(char)
-
append
Appends the string representation of the specified
intvalue. Theintvalue is converted to a string according to the rule defined byString#valueOf(int).Parameters
i: theintvalue to append.
Returns
this builder.
See also
- String#valueOf(int)
-
append
Appends the string representation of the specified
longvalue. Thelongvalue is converted to a string according to the rule defined byString#valueOf(long).Parameters
lng: thelongvalue.
Returns
this builder.
See also
- String#valueOf(long)
-
append
Appends the string representation of the specified
floatvalue. Thefloatvalue is converted to a string according to the rule defined byString#valueOf(float).Parameters
f: thefloatvalue to append.
Returns
this builder.
See also
- String#valueOf(float)
-
append
Appends the string representation of the specified
doublevalue. Thedoublevalue is converted to a string according to the rule defined byString#valueOf(double).Parameters
d: thedoublevalue to append.
Returns
this builder.
See also
- String#valueOf(double)
-
append
Appends the string representation of the specified
Object. TheObjectvalue is converted to a string according to the rule defined byString#valueOf(Object).Parameters
obj: theObjectto append.
Returns
this builder.
See also
- String#valueOf(Object)
-
append
Appends the contents of the specified string. If the string is
null, then the string"null"is appended.Parameters
str: the string to append.
Returns
this builder.
-
append
Appends the string representation of the specified
char[]. Thechar[]is converted to a string according to the rule defined byString#valueOf(char[]).Parameters
ch: thechar[]to append..
Returns
this builder.
See also
- String#valueOf(char[])
-
append
Appends the string representation of the specified subset of the
char[]. Thechar[]value is converted to a String according to the rule defined byint, int).Parameters
-
str: thechar[]to append. -
offset: the inclusive offset index. -
len: the number of characters.
Returns
this builder.
Throws
ArrayIndexOutOfBoundsException: @throws ArrayIndexOutOfBoundsException ifoffsetandlendo not specify a valid subsequence.
See also
- String#valueOf(char[], int, int)
-
-
appendCodePoint
Appends the encoded Unicode code point. The code point is converted to a
char[]as defined byCharacter#toChars(int).Parameters
codePoint: the Unicode code point to encode and append.
Returns
this builder.
See also
- Character#toChars(int)
-
delete
Deletes a sequence of characters specified by
startandend. Shifts any remaining characters to the left.Parameters
-
start: the inclusive start index. -
end: the exclusive end index.
Returns
this builder.
Throws
StringIndexOutOfBoundsException: @throws StringIndexOutOfBoundsException ifstartis less than zero, greater than the current length or greater thanend.
-
-
deleteCharAt
Deletes the character at the specified index. shifts any remaining characters to the left.
Parameters
index: the index of the character to delete.
Returns
this builder.
Throws
StringIndexOutOfBoundsException: @throws StringIndexOutOfBoundsException ifindexis less than zero or is greater than or equal to the current length.
-
insert
Inserts the string representation of the specified
booleanvalue at the specifiedoffset. Thebooleanvalue is converted to a string according to the rule defined byString#valueOf(boolean).Parameters
-
offset: the index to insert at. -
b: thebooleanvalue to insert.
Returns
this builder.
Throws
StringIndexOutOfBoundsException: @throws StringIndexOutOfBoundsException ifoffsetis negative or greater than the currentlength.
See also
- String#valueOf(boolean)
-
-
insert
Inserts the string representation of the specified
charvalue at the specifiedoffset. Thecharvalue is converted to a string according to the rule defined byString#valueOf(char).Parameters
-
offset: the index to insert at. -
c: thecharvalue to insert.
Returns
this builder.
Throws
IndexOutOfBoundsException: @throws IndexOutOfBoundsException ifoffsetis negative or greater than the currentlength().
See also
- String#valueOf(char)
-
-
insert
Inserts the string representation of the specified
intvalue at the specifiedoffset. Theintvalue is converted to a String according to the rule defined byString#valueOf(int).Parameters
-
offset: the index to insert at. -
i: theintvalue to insert.
Returns
this builder.
Throws
StringIndexOutOfBoundsException: @throws StringIndexOutOfBoundsException ifoffsetis negative or greater than the currentlength().
See also
- String#valueOf(int)
-
-
insert
Inserts the string representation of the specified
longvalue at the specifiedoffset. Thelongvalue is converted to a String according to the rule defined byString#valueOf(long).Parameters
-
offset: the index to insert at. -
l: thelongvalue to insert.
Returns
this builder.
Throws
StringIndexOutOfBoundsException: @throws StringIndexOutOfBoundsException ifoffsetis negative or greater than the current {code length()}.
See also
- String#valueOf(long)
-
-
insert
Inserts the string representation of the specified
floatvalue at the specifiedoffset. Thefloatvalue is converted to a string according to the rule defined byString#valueOf(float).Parameters
-
offset: the index to insert at. -
f: thefloatvalue to insert.
Returns
this builder.
Throws
StringIndexOutOfBoundsException: @throws StringIndexOutOfBoundsException ifoffsetis negative or greater than the currentlength().
See also
- String#valueOf(float)
-
-
insert
Inserts the string representation of the specified
doublevalue at the specifiedoffset. Thedoublevalue is converted to a String according to the rule defined byString#valueOf(double).Parameters
-
offset: the index to insert at. -
d: thedoublevalue to insert.
Returns
this builder.
Throws
StringIndexOutOfBoundsException: @throws StringIndexOutOfBoundsException ifoffsetis negative or greater than the currentlength().
See also
- String#valueOf(double)
-
-
insert
Inserts the string representation of the specified
Objectat the specifiedoffset. TheObjectvalue is converted to a String according to the rule defined byString#valueOf(Object).Parameters
-
offset: the index to insert at. -
obj: theObjectto insert.
Returns
this builder.
Throws
StringIndexOutOfBoundsException: @throws StringIndexOutOfBoundsException ifoffsetis negative or greater than the currentlength().
See also
- String#valueOf(Object)
-
-
insert
Inserts the specified string at the specified
offset. If the specified string is null, then the String"null"is inserted.Parameters
-
offset: the index to insert at. -
str: theStringto insert.
Returns
this builder.
Throws
StringIndexOutOfBoundsException: @throws StringIndexOutOfBoundsException ifoffsetis negative or greater than the currentlength().
-
-
insert
Inserts the string representation of the specified
char[]at the specifiedoffset. Thechar[]value is converted to a String according to the rule defined byString#valueOf(char[]).Parameters
-
offset: the index to insert at. -
ch: thechar[]to insert.
Returns
this builder.
Throws
StringIndexOutOfBoundsException: @throws StringIndexOutOfBoundsException ifoffsetis negative or greater than the currentlength().
See also
- String#valueOf(char[])
-
-
insert
Inserts the string representation of the specified subsequence of the
char[]at the specifiedoffset. Thechar[]value is converted to a String according to the rule defined byint, int).Parameters
-
offset: the index to insert at. -
str: thechar[]to insert. -
strOffset: the inclusive index. -
strLen: the number of characters.
Returns
this builder.
Throws
StringIndexOutOfBoundsException: @throws StringIndexOutOfBoundsException ifoffsetis negative or greater than the currentlength(), orstrOffsetandstrLendo not specify a valid subsequence.
See also
- String#valueOf(char[], int, int)
-
-
replace
Replaces the specified subsequence in this builder with the specified string.
Parameters
-
start: the inclusive begin index. -
end: the exclusive end index. -
str: the replacement string.
Returns
this builder.
Throws
-
StringIndexOutOfBoundsException: @throws StringIndexOutOfBoundsException ifstartis negative, greater than the currentlength()or greater thanend. -
NullPointerException: ifstrisnull.
-
-
reverse
Reverses the order of characters in this builder.
Returns
this buffer.
-
toString
Returns the contents of this builder.
Returns
the string representation of the data in this builder.
-
capacity
public int capacity()Returns the number of characters that can be held without growing.
Returns
the capacity
See also
-
#ensureCapacity
-
#length
-
-
charAt
public char charAt(int index) Retrieves the character at the
index.Parameters
index: the index of the character to retrieve.
Returns
the char value.
Throws
IndexOutOfBoundsException: @throws IndexOutOfBoundsException ifindexis negative or greater than or equal to the current#length().
-
ensureCapacity
public void ensureCapacity(int min) Ensures that this object has a minimum capacity available before requiring the internal buffer to be enlarged. The general policy of this method is that if the
minimumCapacityis larger than the current#capacity(), then the capacity will be increased to the largest value of either theminimumCapacityor the current capacity multiplied by two plus two. Although this is the general policy, there is no guarantee that the capacity will change.Parameters
min: the new minimum capacity to set.
-
getChars
public void getChars(int start, int end, char[] dest, int destStart) Copies the requested sequence of characters to the
char[]passed starting atdestStart.Parameters
-
start: the inclusive start index of the characters to copy. -
end: the exclusive end index of the characters to copy. -
dest: thechar[]to copy the characters to. -
destStart: the inclusive start index ofdestto begin copying to.
Throws
IndexOutOfBoundsException: @throws IndexOutOfBoundsException if thestartis negative, thedestStartis negative, thestartis greater thanend, theendis greater than the current#length()ordestStart + end - beginis greater thandest.length.
-
-
length
public int length()The current length.
Returns
the number of characters contained in this instance.
-
setCharAt
public void setCharAt(int index, char ch) Sets the character at the
index.Parameters
-
index: the zero-based index of the character to replace. -
ch: the character to set.
Throws
IndexOutOfBoundsException: @throws IndexOutOfBoundsException ifindexis negative or greater than or equal to the current#length().
-
-
setLength
public void setLength(int length) Sets the current length to a new value. If the new length is larger than the current length, then the new characters at the end of this object will contain the
charvalue of�.Parameters
length: the new length of this StringBuffer.
Throws
IndexOutOfBoundsException: iflength < 0.
See also
- #length
-
substring
Returns the String value of the subsequence from the
startindex to the current end.Parameters
start: the inclusive start index to begin the subsequence.
Returns
a String containing the subsequence.
Throws
StringIndexOutOfBoundsException: @throws StringIndexOutOfBoundsException ifstartis negative or greater than the current#length().
-
substring
Returns the String value of the subsequence from the
startindex to theendindex.Parameters
-
start: the inclusive start index to begin the subsequence. -
end: the exclusive end index to end the subsequence.
Returns
a String containing the subsequence.
Throws
StringIndexOutOfBoundsException: @throws StringIndexOutOfBoundsException ifstartis negative, greater thanendor ifendis greater than the current#length().
-
-
indexOf
Searches for the first index of the specified character. The search for the character starts at the beginning and moves towards the end.
Parameters
string: the string to find.
Returns
- Returns:
the index of the specified character, -1 if the character isn't found.
Since
1.4
See also
- #lastIndexOf(String)
-
indexOf
Searches for the index of the specified character. The search for the character starts at the specified offset and moves towards the end.
Parameters
-
subString: the string to find. -
start: the starting offset.
Returns
- Returns:
the index of the specified character, -1 if the character isn't found
Since
1.4
See also
- #lastIndexOf(String, int)
-
-
lastIndexOf
Searches for the last index of the specified character. The search for the character starts at the end and moves towards the beginning.
Parameters
string: the string to find.
Returns
- Returns:
the index of the specified character, -1 if the character isn't found.
Throws
NullPointerException: ifstringisnull.
Since
1.4
See also
- String#lastIndexOf(java.lang.String)
-
lastIndexOf
Searches for the index of the specified character. The search for the character starts at the specified offset and moves towards the beginning.
Parameters
-
subString: the string to find. -
start: the starting offset.
Returns
- Returns:
the index of the specified character, -1 if the character isn't found.
Throws
NullPointerException: ifsubStringisnull.
Since
1.4
See also
- String#lastIndexOf(String, int)
-
-
trimToSize
public void trimToSize()Trims off any extra capacity beyond the current length. Note, this method is NOT guaranteed to change the capacity of this object.
Since
1.5
-
codePointAt
public int codePointAt(int index) Retrieves the Unicode code point value at the
index.Parameters
index: the index to thecharcode unit.
Returns
the Unicode code point value.
Throws
IndexOutOfBoundsException: @throws IndexOutOfBoundsException ifindexis negative or greater than or equal to#length().
Since
1.5
See also
-
Character
-
Character#codePointAt(char[], int, int)
-
codePointBefore
public int codePointBefore(int index) Retrieves the Unicode code point value that precedes the
index.Parameters
index: the index to thecharcode unit within this object.
Returns
the Unicode code point value.
Throws
IndexOutOfBoundsException: @throws IndexOutOfBoundsException ifindexis less than 1 or greater than#length().
Since
1.5
See also
-
Character
-
Character#codePointBefore(char[], int, int)
-
codePointCount
public int codePointCount(int beginIndex, int endIndex) Calculates the number of Unicode code points between
beginIndexandendIndex.Parameters
-
beginIndex: the inclusive beginning index of the subsequence. -
endIndex: the exclusive end index of the subsequence.
Returns
the number of Unicode code points in the subsequence.
Throws
IndexOutOfBoundsException: @throws IndexOutOfBoundsException ifbeginIndexis negative or greater thanendIndexorendIndexis greater than#length().
Since
1.5
See also
-
Character
-
Character#codePointCount(char[], int, int)
-
-
offsetByCodePoints
public int offsetByCodePoints(int index, int codePointOffset) Returns the index that is offset
codePointOffsetcode points fromindex.Parameters
-
index: the index to calculate the offset from. -
codePointOffset: the number of code points to count.
Returns
- Returns:
the index that is
codePointOffsetcode points away from index.Throws
IndexOutOfBoundsException: @throws IndexOutOfBoundsException ifindexis negative or greater than#length()or if there aren't enough code points before or afterindexto matchcodePointOffset.
Since
1.5
See also
-
Character
-
Character#offsetByCodePoints(char[], int, int, int, int)
-
-