Method from org.apache.commons.lang.text.StrBuilder Detail: |
public StrBuilder append(Object obj) {
if (obj == null) {
return appendNull();
}
return append(obj.toString());
}
Appends an object to this string builder.
Appending null will call #appendNull() . |
public StrBuilder append(String str) {
if (str == null) {
return appendNull();
}
int strLen = str.length();
if (strLen > 0) {
int len = length();
ensureCapacity(len + strLen);
str.getChars(0, strLen, buffer, len);
size += strLen;
}
return this;
}
Appends a string to this string builder.
Appending null will call #appendNull() . |
public StrBuilder append(StringBuffer str) {
if (str == null) {
return appendNull();
}
int strLen = str.length();
if (strLen > 0) {
int len = length();
ensureCapacity(len + strLen);
str.getChars(0, strLen, buffer, len);
size += strLen;
}
return this;
}
Appends a string buffer to this string builder.
Appending null will call #appendNull() . |
public StrBuilder append(StrBuilder str) {
if (str == null) {
return appendNull();
}
int strLen = str.length();
if (strLen > 0) {
int len = length();
ensureCapacity(len + strLen);
System.arraycopy(str.buffer, 0, buffer, len, strLen);
size += strLen;
}
return this;
}
Appends another string builder to this string builder.
Appending null will call #appendNull() . |
public StrBuilder append(char[] chars) {
if (chars == null) {
return appendNull();
}
int strLen = chars.length;
if (strLen > 0) {
int len = length();
ensureCapacity(len + strLen);
System.arraycopy(chars, 0, buffer, len, strLen);
size += strLen;
}
return this;
}
Appends a char array to the string builder.
Appending null will call #appendNull() . |
public StrBuilder append(boolean value) {
if (value) {
ensureCapacity(size + 4);
buffer[size++] = 't';
buffer[size++] = 'r';
buffer[size++] = 'u';
buffer[size++] = 'e';
} else {
ensureCapacity(size + 5);
buffer[size++] = 'f';
buffer[size++] = 'a';
buffer[size++] = 'l';
buffer[size++] = 's';
buffer[size++] = 'e';
}
return this;
}
Appends a boolean value to the string builder. |
public StrBuilder append(char ch) {
int len = length();
ensureCapacity(len + 1);
buffer[size++] = ch;
return this;
}
Appends a char value to the string builder. |
public StrBuilder append(int value) {
return append(String.valueOf(value));
}
Appends an int value to the string builder using String.valueOf . |
public StrBuilder append(long value) {
return append(String.valueOf(value));
}
Appends a long value to the string builder using String.valueOf . |
public StrBuilder append(float value) {
return append(String.valueOf(value));
}
Appends a float value to the string builder using String.valueOf . |
public StrBuilder append(double value) {
return append(String.valueOf(value));
}
Appends a double value to the string builder using String.valueOf . |
public StrBuilder append(String str,
int startIndex,
int length) {
if (str == null) {
return appendNull();
}
if (startIndex < 0 || startIndex > str.length()) {
throw new StringIndexOutOfBoundsException("startIndex must be valid");
}
if (length < 0 || (startIndex + length) > str.length()) {
throw new StringIndexOutOfBoundsException("length must be valid");
}
if (length > 0) {
int len = length();
ensureCapacity(len + length);
str.getChars(startIndex, startIndex + length, buffer, len);
size += length;
}
return this;
}
Appends part of a string to this string builder.
Appending null will call #appendNull() . |
public StrBuilder append(StringBuffer str,
int startIndex,
int length) {
if (str == null) {
return appendNull();
}
if (startIndex < 0 || startIndex > str.length()) {
throw new StringIndexOutOfBoundsException("startIndex must be valid");
}
if (length < 0 || (startIndex + length) > str.length()) {
throw new StringIndexOutOfBoundsException("length must be valid");
}
if (length > 0) {
int len = length();
ensureCapacity(len + length);
str.getChars(startIndex, startIndex + length, buffer, len);
size += length;
}
return this;
}
Appends part of a string buffer to this string builder.
Appending null will call #appendNull() . |
public StrBuilder append(StrBuilder str,
int startIndex,
int length) {
if (str == null) {
return appendNull();
}
if (startIndex < 0 || startIndex > str.length()) {
throw new StringIndexOutOfBoundsException("startIndex must be valid");
}
if (length < 0 || (startIndex + length) > str.length()) {
throw new StringIndexOutOfBoundsException("length must be valid");
}
if (length > 0) {
int len = length();
ensureCapacity(len + length);
str.getChars(startIndex, startIndex + length, buffer, len);
size += length;
}
return this;
}
Appends part of a string builder to this string builder.
Appending null will call #appendNull() . |
public StrBuilder append(char[] chars,
int startIndex,
int length) {
if (chars == null) {
return appendNull();
}
if (startIndex < 0 || startIndex > chars.length) {
throw new StringIndexOutOfBoundsException("Invalid startIndex: " + length);
}
if (length < 0 || (startIndex + length) > chars.length) {
throw new StringIndexOutOfBoundsException("Invalid length: " + length);
}
if (length > 0) {
int len = length();
ensureCapacity(len + length);
System.arraycopy(chars, startIndex, buffer, len, length);
size += length;
}
return this;
}
Appends a char array to the string builder.
Appending null will call #appendNull() . |
public StrBuilder appendAll(Object[] array) {
if (array != null && array.length > 0) {
for (int i = 0; i < array.length; i++) {
append(array[i]);
}
}
return this;
}
Appends each item in an array to the builder without any separators.
Appending a null array will have no effect.
Each object is appended using #append(Object) . |
public StrBuilder appendAll(Collection coll) {
if (coll != null && coll.size() > 0) {
Iterator it = coll.iterator();
while (it.hasNext()) {
append(it.next());
}
}
return this;
}
Appends each item in a collection to the builder without any separators.
Appending a null collection will have no effect.
Each object is appended using #append(Object) . |
public StrBuilder appendAll(Iterator it) {
if (it != null) {
while (it.hasNext()) {
append(it.next());
}
}
return this;
}
Appends each item in an iterator to the builder without any separators.
Appending a null iterator will have no effect.
Each object is appended using #append(Object) . |
public StrBuilder appendFixedWidthPadLeft(Object obj,
int width,
char padChar) {
if (width > 0) {
ensureCapacity(size + width);
String str = (obj == null ? getNullText() : obj.toString());
if (str == null) {
str = "";
}
int strLen = str.length();
if (strLen >= width) {
str.getChars(strLen - width, strLen, buffer, size);
} else {
int padLen = width - strLen;
for (int i = 0; i < padLen; i++) {
buffer[size + i] = padChar;
}
str.getChars(0, strLen, buffer, size + padLen);
}
size += width;
}
return this;
}
Appends an object to the builder padding on the left to a fixed width.
The toString of the object is used.
If the object is larger than the length, the left hand side is lost.
If the object is null, the null text value is used. |
public StrBuilder appendFixedWidthPadLeft(int value,
int width,
char padChar) {
return appendFixedWidthPadLeft(String.valueOf(value), width, padChar);
}
Appends an object to the builder padding on the left to a fixed width.
The String.valueOf of the int value is used.
If the formatted value is larger than the length, the left hand side is lost. |
public StrBuilder appendFixedWidthPadRight(Object obj,
int width,
char padChar) {
if (width > 0) {
ensureCapacity(size + width);
String str = (obj == null ? getNullText() : obj.toString());
if (str == null) {
str = "";
}
int strLen = str.length();
if (strLen >= width) {
str.getChars(0, width, buffer, size);
} else {
int padLen = width - strLen;
str.getChars(0, strLen, buffer, size);
for (int i = 0; i < padLen; i++) {
buffer[size + strLen + i] = padChar;
}
}
size += width;
}
return this;
}
Appends an object to the builder padding on the right to a fixed length.
The toString of the object is used.
If the object is larger than the length, the right hand side is lost.
If the object is null, null text value is used. |
public StrBuilder appendFixedWidthPadRight(int value,
int width,
char padChar) {
return appendFixedWidthPadRight(String.valueOf(value), width, padChar);
}
Appends an object to the builder padding on the right to a fixed length.
The String.valueOf of the int value is used.
If the object is larger than the length, the right hand side is lost. |
public StrBuilder appendNewLine() {
if (newLine == null) {
append(SystemUtils.LINE_SEPARATOR);
return this;
}
return append(newLine);
}
Appends the new line string to this string builder.
The new line string can be altered using #setNewLineText(String) .
This might be used to force the output to always use Unix line endings
even when on Windows. |
public StrBuilder appendNull() {
if (nullText == null) {
return this;
}
return append(nullText);
}
Appends the text representing null to this string builder. |
public StrBuilder appendPadding(int length,
char padChar) {
if (length >= 0) {
ensureCapacity(size + length);
for (int i = 0; i < length; i++) {
buffer[size++] = padChar;
}
}
return this;
}
Appends the pad character to the builder the specified number of times. |
public StrBuilder appendSeparator(String separator) {
return appendSeparator(separator, null);
}
for (Iterator it = list.iterator(); it.hasNext(); ) {
appendSeparator(",");
append(it.next());
}
Note that for this simple example, you should use
#appendWithSeparators(Collection, String) . |
public StrBuilder appendSeparator(char separator) {
if (size() > 0) {
append(separator);
}
return this;
}
for (Iterator it = list.iterator(); it.hasNext(); ) {
appendSeparator(',');
append(it.next());
}
Note that for this simple example, you should use
#appendWithSeparators(Collection, String) . |
public StrBuilder appendSeparator(String standard,
String defaultIfEmpty) {
String str = isEmpty() ? defaultIfEmpty : standard;
if (str != null) {
append(str);
}
return this;
}
StrBuilder whereClause = new StrBuilder();
if(searchCommand.getPriority() != null) {
whereClause.appendSeparator(" and", " where");
whereClause.append(" priority = ?")
}
if(searchCommand.getComponent() != null) {
whereClause.appendSeparator(" and", " where");
whereClause.append(" component = ?")
}
selectClause.append(whereClause)
|
public StrBuilder appendSeparator(char standard,
char defaultIfEmpty) {
if (size() > 0) {
append(standard);
}
else {
append(defaultIfEmpty);
}
return this;
}
Append one of both separators to the builder
If the builder is currently empty it will append the defaultIfEmpty-separator
Otherwise it will append the standard-separator
The separator is appended using #append(char) . |
public StrBuilder appendSeparator(String separator,
int loopIndex) {
if (separator != null && loopIndex > 0) {
append(separator);
}
return this;
}
for (int i = 0; i < list.size(); i++) {
appendSeparator(",", i);
append(list.get(i));
}
Note that for this simple example, you should use
#appendWithSeparators(Collection, String) . |
public StrBuilder appendSeparator(char separator,
int loopIndex) {
if (loopIndex > 0) {
append(separator);
}
return this;
}
for (int i = 0; i < list.size(); i++) {
appendSeparator(",", i);
append(list.get(i));
}
Note that for this simple example, you should use
#appendWithSeparators(Collection, String) . |
public StrBuilder appendWithSeparators(Object[] array,
String separator) {
if (array != null && array.length > 0) {
separator = (separator == null ? "" : separator);
append(array[0]);
for (int i = 1; i < array.length; i++) {
append(separator);
append(array[i]);
}
}
return this;
}
Appends an array placing separators between each value, but
not before the first or after the last.
Appending a null array will have no effect.
Each object is appended using #append(Object) . |
public StrBuilder appendWithSeparators(Collection coll,
String separator) {
if (coll != null && coll.size() > 0) {
separator = (separator == null ? "" : separator);
Iterator it = coll.iterator();
while (it.hasNext()) {
append(it.next());
if (it.hasNext()) {
append(separator);
}
}
}
return this;
}
Appends a collection placing separators between each value, but
not before the first or after the last.
Appending a null collection will have no effect.
Each object is appended using #append(Object) . |
public StrBuilder appendWithSeparators(Iterator it,
String separator) {
if (it != null) {
separator = (separator == null ? "" : separator);
while (it.hasNext()) {
append(it.next());
if (it.hasNext()) {
append(separator);
}
}
}
return this;
}
Appends an iterator placing separators between each value, but
not before the first or after the last.
Appending a null iterator will have no effect.
Each object is appended using #append(Object) . |
public StrBuilder appendln(Object obj) {
return append(obj).appendNewLine();
}
Appends an object followed by a new line to this string builder.
Appending null will call #appendNull() . |
public StrBuilder appendln(String str) {
return append(str).appendNewLine();
}
Appends a string followed by a new line to this string builder.
Appending null will call #appendNull() . |
public StrBuilder appendln(StringBuffer str) {
return append(str).appendNewLine();
}
Appends a string buffer followed by a new line to this string builder.
Appending null will call #appendNull() . |
public StrBuilder appendln(StrBuilder str) {
return append(str).appendNewLine();
}
Appends another string builder followed by a new line to this string builder.
Appending null will call #appendNull() . |
public StrBuilder appendln(char[] chars) {
return append(chars).appendNewLine();
}
Appends a char array followed by a new line to the string builder.
Appending null will call #appendNull() . |
public StrBuilder appendln(boolean value) {
return append(value).appendNewLine();
}
Appends a boolean value followed by a new line to the string builder. |
public StrBuilder appendln(char ch) {
return append(ch).appendNewLine();
}
Appends a char value followed by a new line to the string builder. |
public StrBuilder appendln(int value) {
return append(value).appendNewLine();
}
Appends an int value followed by a new line to the string builder using String.valueOf . |
public StrBuilder appendln(long value) {
return append(value).appendNewLine();
}
Appends a long value followed by a new line to the string builder using String.valueOf . |
public StrBuilder appendln(float value) {
return append(value).appendNewLine();
}
Appends a float value followed by a new line to the string builder using String.valueOf . |
public StrBuilder appendln(double value) {
return append(value).appendNewLine();
}
Appends a double value followed by a new line to the string builder using String.valueOf . |
public StrBuilder appendln(String str,
int startIndex,
int length) {
return append(str, startIndex, length).appendNewLine();
}
Appends part of a string followed by a new line to this string builder.
Appending null will call #appendNull() . |
public StrBuilder appendln(StringBuffer str,
int startIndex,
int length) {
return append(str, startIndex, length).appendNewLine();
}
Appends part of a string buffer followed by a new line to this string builder.
Appending null will call #appendNull() . |
public StrBuilder appendln(StrBuilder str,
int startIndex,
int length) {
return append(str, startIndex, length).appendNewLine();
}
Appends part of a string builder followed by a new line to this string builder.
Appending null will call #appendNull() . |
public StrBuilder appendln(char[] chars,
int startIndex,
int length) {
return append(chars, startIndex, length).appendNewLine();
}
Appends a char array followed by a new line to the string builder.
Appending null will call #appendNull() . |
public Reader asReader() {
return new StrBuilderReader();
}
Gets the contents of this builder as a Reader.
This method allows the contents of the builder to be read
using any standard method that expects a Reader.
To use, simply create a StrBuilder , populate it with
data, call asReader , and then read away.
The internal character array is shared between the builder and the reader.
This allows you to append to the builder after creating the reader,
and the changes will be picked up.
Note however, that no synchronization occurs, so you must perform
all operations with the builder and the reader in one thread.
The returned reader supports marking, and ignores the flush method. |
public StrTokenizer asTokenizer() {
return new StrBuilderTokenizer();
}
Creates a tokenizer that can tokenize the contents of this builder.
This method allows the contents of this builder to be tokenized.
The tokenizer will be setup by default to tokenize on space, tab,
newline and formfeed (as per StringTokenizer). These values can be
changed on the tokenizer class, before retrieving the tokens.
The returned tokenizer is linked to this builder. You may intermix
calls to the buider and tokenizer within certain limits, however
there is no synchronization. Once the tokenizer has been used once,
it must be reset to pickup the latest
changes in the builder. For example:
StrBuilder b = new StrBuilder();
b.append("a b ");
StrTokenizer t = b.asTokenizer();
String[] tokens1 = t.getTokenArray(); // returns a,b
b.append("c d ");
String[] tokens2 = t.getTokenArray(); // returns a,b (c and d ignored)
t.reset(); // reset causes builder changes to be picked up
String[] tokens3 = t.getTokenArray(); // returns a,b,c,d
In addition to simply intermixing appends and tokenization, you can also
call the set methods on the tokenizer to alter how it tokenizes. Just
remember to call reset when you want to pickup builder changes.
Calling StrTokenizer#reset(String) or StrTokenizer#reset(char[])
with a non-null value will break the link with the builder. |
public Writer asWriter() {
return new StrBuilderWriter();
}
Gets this builder as a Writer that can be written to.
This method allows you to populate the contents of the builder
using any standard method that takes a Writer.
To use, simply create a StrBuilder ,
call asWriter , and populate away. The data is available
at any time using the methods of the StrBuilder .
The internal character array is shared between the builder and the writer.
This allows you to intermix calls that append to the builder and
write using the writer and the changes will be occur correctly.
Note however, that no synchronization occurs, so you must perform
all operations with the builder and the writer in one thread.
The returned writer ignores the close and flush methods. |
public int capacity() {
return buffer.length;
}
Gets the current size of the internal character array buffer. |
public char charAt(int index) {
if (index < 0 || index >= length()) {
throw new StringIndexOutOfBoundsException(index);
}
return buffer[index];
}
Gets the character at the specified index. |
public StrBuilder clear() {
size = 0;
return this;
}
Clears the string builder (convenience Collections API style method).
This method does not reduce the size of the internal character buffer.
To do that, call clear() followed by #minimizeCapacity() .
This method is the same as #setLength(int) called with zero
and is provided to match the API of Collections. |
public boolean contains(char ch) {
char[] thisBuf = buffer;
for (int i = 0; i < this.size; i++) {
if (thisBuf[i] == ch) {
return true;
}
}
return false;
}
Checks if the string builder contains the specified char. |
public boolean contains(String str) {
return indexOf(str, 0) >= 0;
}
Checks if the string builder contains the specified string. |
public boolean contains(StrMatcher matcher) {
return indexOf(matcher, 0) >= 0;
}
Checks if the string builder contains a string matched using the
specified matcher.
Matchers can be used to perform advanced searching behaviour.
For example you could write a matcher to search for the character
'a' followed by a number. |
public StrBuilder delete(int startIndex,
int endIndex) {
endIndex = validateRange(startIndex, endIndex);
int len = endIndex - startIndex;
if (len > 0) {
deleteImpl(startIndex, endIndex, len);
}
return this;
}
Deletes the characters between the two specified indices. |
public StrBuilder deleteAll(char ch) {
for (int i = 0; i < size; i++) {
if (buffer[i] == ch) {
int start = i;
while (++i < size) {
if (buffer[i] != ch) {
break;
}
}
int len = i - start;
deleteImpl(start, i, len);
i -= len;
}
}
return this;
}
Deletes the character wherever it occurs in the builder. |
public StrBuilder deleteAll(String str) {
int len = (str == null ? 0 : str.length());
if (len > 0) {
int index = indexOf(str, 0);
while (index >= 0) {
deleteImpl(index, index + len, len);
index = indexOf(str, index);
}
}
return this;
}
Deletes the string wherever it occurs in the builder. |
public StrBuilder deleteAll(StrMatcher matcher) {
return replace(matcher, null, 0, size, -1);
}
Deletes all parts of the builder that the matcher matches.
Matchers can be used to perform advanced deletion behaviour.
For example you could write a matcher to delete all occurances
where the character 'a' is followed by a number. |
public StrBuilder deleteCharAt(int index) {
if (index < 0 || index >= size) {
throw new StringIndexOutOfBoundsException(index);
}
deleteImpl(index, index + 1, 1);
return this;
}
Deletes the character at the specified index. |
public StrBuilder deleteFirst(char ch) {
for (int i = 0; i < size; i++) {
if (buffer[i] == ch) {
deleteImpl(i, i + 1, 1);
break;
}
}
return this;
}
Deletes the character wherever it occurs in the builder. |
public StrBuilder deleteFirst(String str) {
int len = (str == null ? 0 : str.length());
if (len > 0) {
int index = indexOf(str, 0);
if (index >= 0) {
deleteImpl(index, index + len, len);
}
}
return this;
}
Deletes the string wherever it occurs in the builder. |
public StrBuilder deleteFirst(StrMatcher matcher) {
return replace(matcher, null, 0, size, 1);
}
Deletes the first match within the builder using the specified matcher.
Matchers can be used to perform advanced deletion behaviour.
For example you could write a matcher to delete
where the character 'a' is followed by a number. |
public boolean endsWith(String str) {
if (str == null) {
return false;
}
int len = str.length();
if (len == 0) {
return true;
}
if (len > size) {
return false;
}
int pos = size - len;
for (int i = 0; i < len; i++,pos++) {
if (buffer[pos] != str.charAt(i)) {
return false;
}
}
return true;
}
Checks whether this builder ends with the specified string.
Note that this method handles null input quietly, unlike String. |
public StrBuilder ensureCapacity(int capacity) {
if (capacity > buffer.length) {
char[] old = buffer;
buffer = new char[capacity * 2];
System.arraycopy(old, 0, buffer, 0, size);
}
return this;
}
Checks the capacity and ensures that it is at least the size specified. |
public boolean equals(StrBuilder other) {
if (this == other) {
return true;
}
if (this.size != other.size) {
return false;
}
char thisBuf[] = this.buffer;
char otherBuf[] = other.buffer;
for (int i = size - 1; i >= 0; i--) {
if (thisBuf[i] != otherBuf[i]) {
return false;
}
}
return true;
}
Checks the contents of this builder against another to see if they
contain the same character content. |
public boolean equals(Object obj) {
if (obj instanceof StrBuilder) {
return equals((StrBuilder) obj);
}
return false;
}
Checks the contents of this builder against another to see if they
contain the same character content. |
public boolean equalsIgnoreCase(StrBuilder other) {
if (this == other) {
return true;
}
if (this.size != other.size) {
return false;
}
char thisBuf[] = this.buffer;
char otherBuf[] = other.buffer;
for (int i = size - 1; i >= 0; i--) {
char c1 = thisBuf[i];
char c2 = otherBuf[i];
if (c1 != c2 && Character.toUpperCase(c1) != Character.toUpperCase(c2)) {
return false;
}
}
return true;
}
Checks the contents of this builder against another to see if they
contain the same character content ignoring case. |
public char[] getChars(char[] destination) {
int len = length();
if (destination == null || destination.length < len) {
destination = new char[len];
}
System.arraycopy(buffer, 0, destination, 0, len);
return destination;
}
Copies the character array into the specified array. |
public void getChars(int startIndex,
int endIndex,
char[] destination,
int destinationIndex) {
if (startIndex < 0) {
throw new StringIndexOutOfBoundsException(startIndex);
}
if (endIndex < 0 || endIndex > length()) {
throw new StringIndexOutOfBoundsException(endIndex);
}
if (startIndex > endIndex) {
throw new StringIndexOutOfBoundsException("end < start");
}
System.arraycopy(buffer, startIndex, destination, destinationIndex, endIndex - startIndex);
}
Copies the character array into the specified array. |
public String getNewLineText() {
return newLine;
}
Gets the text to be appended when a new line is added. |
public String getNullText() {
return nullText;
}
Gets the text to be appended when null is added. |
public int hashCode() {
char buf[] = buffer;
int hash = 0;
for (int i = size - 1; i >= 0; i--) {
hash = 31 * hash + buf[i];
}
return hash;
}
Gets a suitable hash code for this builder. |
public int indexOf(char ch) {
return indexOf(ch, 0);
}
Searches the string builder to find the first reference to the specified char. |
public int indexOf(String str) {
return indexOf(str, 0);
}
Searches the string builder to find the first reference to the specified string.
Note that a null input string will return -1, whereas the JDK throws an exception. |
public int indexOf(StrMatcher matcher) {
return indexOf(matcher, 0);
}
Searches the string builder using the matcher to find the first match.
Matchers can be used to perform advanced searching behaviour.
For example you could write a matcher to find the character 'a'
followed by a number. |
public int indexOf(char ch,
int startIndex) {
startIndex = (startIndex < 0 ? 0 : startIndex);
if (startIndex >= size) {
return -1;
}
char[] thisBuf = buffer;
for (int i = startIndex; i < size; i++) {
if (thisBuf[i] == ch) {
return i;
}
}
return -1;
}
Searches the string builder to find the first reference to the specified char. |
public int indexOf(String str,
int startIndex) {
startIndex = (startIndex < 0 ? 0 : startIndex);
if (str == null || startIndex >= size) {
return -1;
}
int strLen = str.length();
if (strLen == 1) {
return indexOf(str.charAt(0), startIndex);
}
if (strLen == 0) {
return startIndex;
}
if (strLen > size) {
return -1;
}
char[] thisBuf = buffer;
int len = size - strLen + 1;
outer:
for (int i = startIndex; i < len; i++) {
for (int j = 0; j < strLen; j++) {
if (str.charAt(j) != thisBuf[i + j]) {
continue outer;
}
}
return i;
}
return -1;
}
Searches the string builder to find the first reference to the specified
string starting searching from the given index.
Note that a null input string will return -1, whereas the JDK throws an exception. |
public int indexOf(StrMatcher matcher,
int startIndex) {
startIndex = (startIndex < 0 ? 0 : startIndex);
if (matcher == null || startIndex >= size) {
return -1;
}
int len = size;
char[] buf = buffer;
for (int i = startIndex; i < len; i++) {
if (matcher.isMatch(buf, i, startIndex, len) > 0) {
return i;
}
}
return -1;
}
Searches the string builder using the matcher to find the first
match searching from the given index.
Matchers can be used to perform advanced searching behaviour.
For example you could write a matcher to find the character 'a'
followed by a number. |
public StrBuilder insert(int index,
Object obj) {
if (obj == null) {
return insert(index, nullText);
}
return insert(index, obj.toString());
}
Inserts the string representation of an object into this builder.
Inserting null will use the stored null text value. |
public StrBuilder insert(int index,
String str) {
validateIndex(index);
if (str == null) {
str = nullText;
}
int strLen = (str == null ? 0 : str.length());
if (strLen > 0) {
int newSize = size + strLen;
ensureCapacity(newSize);
System.arraycopy(buffer, index, buffer, index + strLen, size - index);
size = newSize;
str.getChars(0, strLen, buffer, index); // str cannot be null here
}
return this;
}
Inserts the string into this builder.
Inserting null will use the stored null text value. |
public StrBuilder insert(int index,
char[] chars) {
validateIndex(index);
if (chars == null) {
return insert(index, nullText);
}
int len = chars.length;
if (len > 0) {
ensureCapacity(size + len);
System.arraycopy(buffer, index, buffer, index + len, size - index);
System.arraycopy(chars, 0, buffer, index, len);
size += len;
}
return this;
}
Inserts the character array into this builder.
Inserting null will use the stored null text value. |
public StrBuilder insert(int index,
boolean value) {
validateIndex(index);
if (value) {
ensureCapacity(size + 4);
System.arraycopy(buffer, index, buffer, index + 4, size - index);
buffer[index++] = 't';
buffer[index++] = 'r';
buffer[index++] = 'u';
buffer[index] = 'e';
size += 4;
} else {
ensureCapacity(size + 5);
System.arraycopy(buffer, index, buffer, index + 5, size - index);
buffer[index++] = 'f';
buffer[index++] = 'a';
buffer[index++] = 'l';
buffer[index++] = 's';
buffer[index] = 'e';
size += 5;
}
return this;
}
Inserts the value into this builder. |
public StrBuilder insert(int index,
char value) {
validateIndex(index);
ensureCapacity(size + 1);
System.arraycopy(buffer, index, buffer, index + 1, size - index);
buffer[index] = value;
size++;
return this;
}
Inserts the value into this builder. |
public StrBuilder insert(int index,
int value) {
return insert(index, String.valueOf(value));
}
Inserts the value into this builder. |
public StrBuilder insert(int index,
long value) {
return insert(index, String.valueOf(value));
}
Inserts the value into this builder. |
public StrBuilder insert(int index,
float value) {
return insert(index, String.valueOf(value));
}
Inserts the value into this builder. |
public StrBuilder insert(int index,
double value) {
return insert(index, String.valueOf(value));
}
Inserts the value into this builder. |
public StrBuilder insert(int index,
char[] chars,
int offset,
int length) {
validateIndex(index);
if (chars == null) {
return insert(index, nullText);
}
if (offset < 0 || offset > chars.length) {
throw new StringIndexOutOfBoundsException("Invalid offset: " + offset);
}
if (length < 0 || offset + length > chars.length) {
throw new StringIndexOutOfBoundsException("Invalid length: " + length);
}
if (length > 0) {
ensureCapacity(size + length);
System.arraycopy(buffer, index, buffer, index + length, size - index);
System.arraycopy(chars, offset, buffer, index, length);
size += length;
}
return this;
}
Inserts part of the character array into this builder.
Inserting null will use the stored null text value. |
public boolean isEmpty() {
return size == 0;
}
|
public int lastIndexOf(char ch) {
return lastIndexOf(ch, size - 1);
}
Searches the string builder to find the last reference to the specified char. |
public int lastIndexOf(String str) {
return lastIndexOf(str, size - 1);
}
Searches the string builder to find the last reference to the specified string.
Note that a null input string will return -1, whereas the JDK throws an exception. |
public int lastIndexOf(StrMatcher matcher) {
return lastIndexOf(matcher, size);
}
Searches the string builder using the matcher to find the last match.
Matchers can be used to perform advanced searching behaviour.
For example you could write a matcher to find the character 'a'
followed by a number. |
public int lastIndexOf(char ch,
int startIndex) {
startIndex = (startIndex >= size ? size - 1 : startIndex);
if (startIndex < 0) {
return -1;
}
for (int i = startIndex; i >= 0; i--) {
if (buffer[i] == ch) {
return i;
}
}
return -1;
}
Searches the string builder to find the last reference to the specified char. |
public int lastIndexOf(String str,
int startIndex) {
startIndex = (startIndex >= size ? size - 1 : startIndex);
if (str == null || startIndex < 0) {
return -1;
}
int strLen = str.length();
if (strLen > 0 && strLen < = size) {
if (strLen == 1) {
return lastIndexOf(str.charAt(0), startIndex);
}
outer:
for (int i = startIndex - strLen + 1; i >= 0; i--) {
for (int j = 0; j < strLen; j++) {
if (str.charAt(j) != buffer[i + j]) {
continue outer;
}
}
return i;
}
} else if (strLen == 0) {
return startIndex;
}
return -1;
}
Searches the string builder to find the last reference to the specified
string starting searching from the given index.
Note that a null input string will return -1, whereas the JDK throws an exception. |
public int lastIndexOf(StrMatcher matcher,
int startIndex) {
startIndex = (startIndex >= size ? size - 1 : startIndex);
if (matcher == null || startIndex < 0) {
return -1;
}
char[] buf = buffer;
int endIndex = startIndex + 1;
for (int i = startIndex; i >= 0; i--) {
if (matcher.isMatch(buf, i, 0, endIndex) > 0) {
return i;
}
}
return -1;
}
Searches the string builder using the matcher to find the last
match searching from the given index.
Matchers can be used to perform advanced searching behaviour.
For example you could write a matcher to find the character 'a'
followed by a number. |
public String leftString(int length) {
if (length < = 0) {
return "";
} else if (length >= size) {
return new String(buffer, 0, size);
} else {
return new String(buffer, 0, length);
}
}
Extracts the leftmost characters from the string builder without
throwing an exception.
This method extracts the left length characters from
the builder. If this many characters are not available, the whole
builder is returned. Thus the returned string may be shorter than the
length requested. |
public int length() {
return size;
}
Gets the length of the string builder. |
public String midString(int index,
int length) {
if (index < 0) {
index = 0;
}
if (length < = 0 || index >= size) {
return "";
}
if (size < = index + length) {
return new String(buffer, index, size - index);
} else {
return new String(buffer, index, length);
}
}
Extracts some characters from the middle of the string builder without
throwing an exception.
This method extracts length characters from the builder
at the specified index.
If the index is negative it is treated as zero.
If the index is greater than the builder size, it is treated as the builder size.
If the length is negative, the empty string is returned.
If insufficient characters are available in the builder, as much as possible is returned.
Thus the returned string may be shorter than the length requested. |
public StrBuilder minimizeCapacity() {
if (buffer.length > length()) {
char[] old = buffer;
buffer = new char[length()];
System.arraycopy(old, 0, buffer, 0, size);
}
return this;
}
Minimizes the capacity to the actual length of the string. |
public StrBuilder replace(int startIndex,
int endIndex,
String replaceStr) {
endIndex = validateRange(startIndex, endIndex);
int insertLen = (replaceStr == null ? 0 : replaceStr.length());
replaceImpl(startIndex, endIndex, endIndex - startIndex, replaceStr, insertLen);
return this;
}
Replaces a portion of the string builder with another string.
The length of the inserted string does not have to match the removed length. |
public StrBuilder replace(StrMatcher matcher,
String replaceStr,
int startIndex,
int endIndex,
int replaceCount) {
endIndex = validateRange(startIndex, endIndex);
return replaceImpl(matcher, replaceStr, startIndex, endIndex, replaceCount);
}
Advanced search and replaces within the builder using a matcher.
Matchers can be used to perform advanced behaviour.
For example you could write a matcher to delete all occurances
where the character 'a' is followed by a number. |
public StrBuilder replaceAll(char search,
char replace) {
if (search != replace) {
for (int i = 0; i < size; i++) {
if (buffer[i] == search) {
buffer[i] = replace;
}
}
}
return this;
}
Replaces the search character with the replace character
throughout the builder. |
public StrBuilder replaceAll(String searchStr,
String replaceStr) {
int searchLen = (searchStr == null ? 0 : searchStr.length());
if (searchLen > 0) {
int replaceLen = (replaceStr == null ? 0 : replaceStr.length());
int index = indexOf(searchStr, 0);
while (index >= 0) {
replaceImpl(index, index + searchLen, searchLen, replaceStr, replaceLen);
index = indexOf(searchStr, index + replaceLen);
}
}
return this;
}
Replaces the search string with the replace string throughout the builder. |
public StrBuilder replaceAll(StrMatcher matcher,
String replaceStr) {
return replace(matcher, replaceStr, 0, size, -1);
}
Replaces all matches within the builder with the replace string.
Matchers can be used to perform advanced replace behaviour.
For example you could write a matcher to replace all occurances
where the character 'a' is followed by a number. |
public StrBuilder replaceFirst(char search,
char replace) {
if (search != replace) {
for (int i = 0; i < size; i++) {
if (buffer[i] == search) {
buffer[i] = replace;
break;
}
}
}
return this;
}
Replaces the first instance of the search character with the
replace character in the builder. |
public StrBuilder replaceFirst(String searchStr,
String replaceStr) {
int searchLen = (searchStr == null ? 0 : searchStr.length());
if (searchLen > 0) {
int index = indexOf(searchStr, 0);
if (index >= 0) {
int replaceLen = (replaceStr == null ? 0 : replaceStr.length());
replaceImpl(index, index + searchLen, searchLen, replaceStr, replaceLen);
}
}
return this;
}
Replaces the first instance of the search string with the replace string. |
public StrBuilder replaceFirst(StrMatcher matcher,
String replaceStr) {
return replace(matcher, replaceStr, 0, size, 1);
}
Replaces the first match within the builder with the replace string.
Matchers can be used to perform advanced replace behaviour.
For example you could write a matcher to replace
where the character 'a' is followed by a number. |
public StrBuilder reverse() {
if (size == 0) {
return this;
}
int half = size / 2;
char[] buf = buffer;
for (int leftIdx = 0, rightIdx = size - 1; leftIdx < half; leftIdx++,rightIdx--) {
char swap = buf[leftIdx];
buf[leftIdx] = buf[rightIdx];
buf[rightIdx] = swap;
}
return this;
}
Reverses the string builder placing each character in the opposite index. |
public String rightString(int length) {
if (length < = 0) {
return "";
} else if (length >= size) {
return new String(buffer, 0, size);
} else {
return new String(buffer, size - length, length);
}
}
Extracts the rightmost characters from the string builder without
throwing an exception.
This method extracts the right length characters from
the builder. If this many characters are not available, the whole
builder is returned. Thus the returned string may be shorter than the
length requested. |
public StrBuilder setCharAt(int index,
char ch) {
if (index < 0 || index >= length()) {
throw new StringIndexOutOfBoundsException(index);
}
buffer[index] = ch;
return this;
}
Sets the character at the specified index. |
public StrBuilder setLength(int length) {
if (length < 0) {
throw new StringIndexOutOfBoundsException(length);
}
if (length < size) {
size = length;
} else if (length > size) {
ensureCapacity(length);
int oldEnd = size;
int newEnd = length;
size = length;
for (int i = oldEnd; i < newEnd; i++) {
buffer[i] = '\0';
}
}
return this;
}
Updates the length of the builder by either dropping the last characters
or adding filler of unicode zero. |
public StrBuilder setNewLineText(String newLine) {
this.newLine = newLine;
return this;
}
Sets the text to be appended when a new line is added. |
public StrBuilder setNullText(String nullText) {
if (nullText != null && nullText.length() == 0) {
nullText = null;
}
this.nullText = nullText;
return this;
}
Sets the text to be appended when null is added. |
public int size() {
return size;
}
|
public boolean startsWith(String str) {
if (str == null) {
return false;
}
int len = str.length();
if (len == 0) {
return true;
}
if (len > size) {
return false;
}
for (int i = 0; i < len; i++) {
if (buffer[i] != str.charAt(i)) {
return false;
}
}
return true;
}
Checks whether this builder starts with the specified string.
Note that this method handles null input quietly, unlike String. |
public String substring(int start) {
return substring(start, size);
}
Extracts a portion of this string builder as a string. |
public String substring(int startIndex,
int endIndex) {
endIndex = validateRange(startIndex, endIndex);
return new String(buffer, startIndex, endIndex - startIndex);
}
Extracts a portion of this string builder as a string.
Note: This method treats an endIndex greater than the length of the
builder as equal to the length of the builder, and continues
without error, unlike StringBuffer or String. |
public char[] toCharArray() {
if (size == 0) {
return ArrayUtils.EMPTY_CHAR_ARRAY;
}
char chars[] = new char[size];
System.arraycopy(buffer, 0, chars, 0, size);
return chars;
}
Copies the builder's character array into a new character array. |
public char[] toCharArray(int startIndex,
int endIndex) {
endIndex = validateRange(startIndex, endIndex);
int len = endIndex - startIndex;
if (len == 0) {
return ArrayUtils.EMPTY_CHAR_ARRAY;
}
char chars[] = new char[len];
System.arraycopy(buffer, startIndex, chars, 0, len);
return chars;
}
Copies part of the builder's character array into a new character array. |
public String toString() {
return new String(buffer, 0, size);
}
Gets a String version of the string builder, creating a new instance
each time the method is called.
Note that unlike StringBuffer, the string version returned is
independent of the string builder. |
public StringBuffer toStringBuffer() {
return new StringBuffer(size).append(buffer, 0, size);
}
Gets a StringBuffer version of the string builder, creating a
new instance each time the method is called. |
public StrBuilder trim() {
if (size == 0) {
return this;
}
int len = size;
char[] buf = buffer;
int pos = 0;
while (pos < len && buf[pos] < = ' ') {
pos++;
}
while (pos < len && buf[len - 1] < = ' ') {
len--;
}
if (len < size) {
delete(len, size);
}
if (pos > 0) {
delete(0, pos);
}
return this;
}
Trims the builder by removing characters less than or equal to a space
from the beginning and end. |
protected void validateIndex(int index) {
if (index < 0 || index > size) {
throw new StringIndexOutOfBoundsException(index);
}
}
Validates parameters defining a single index in the builder. |
protected int validateRange(int startIndex,
int endIndex) {
if (startIndex < 0) {
throw new StringIndexOutOfBoundsException(startIndex);
}
if (endIndex > size) {
endIndex = size;
}
if (startIndex > endIndex) {
throw new StringIndexOutOfBoundsException("end < start");
}
return endIndex;
}
Validates parameters defining a range of the builder. |