/** * Appends the pad character to the builder the specified number of times. * * @param length the length to append, negative means no append * @param padChar the character to append * @return this, to enable chaining */ 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 a boolean value to the string builder. * * @param value the value to append * @return this, to enable chaining */ 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 char value to the string builder. * * @param ch the value to append * @return this, to enable chaining */ public StrBuilder append(char ch) { int len = length(); ensureCapacity(len + 1); buffer[size++] = ch; return this; }
/** * Internal method to delete a range without validation. * * @param startIndex the start index, must be valid * @param endIndex the end index (exclusive), must be valid * @param removeLen the length to remove (endIndex - startIndex), must be valid * @param insertStr the string to replace with, null means delete range * @param insertLen the length of the insert string, must be valid * @throws IndexOutOfBoundsException if any index is invalid */ private void replaceImpl(int startIndex, int endIndex, int removeLen, String insertStr, int insertLen) { int newSize = size - removeLen + insertLen; if (insertLen != removeLen) { ensureCapacity(newSize); System.arraycopy(buffer, endIndex, buffer, startIndex + insertLen, size - endIndex); size = newSize; } if (insertLen > 0) { insertStr.getChars(0, insertLen, buffer, startIndex); } }
/** * Inserts the value into this builder. * * @param index the index to add at, must be valid * @param value the value to insert * @return this, to enable chaining * @throws IndexOutOfBoundsException if the index is invalid */ 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. * * @param index the index to add at, must be valid * @param value the value to insert * @return this, to enable chaining * @throws IndexOutOfBoundsException if the index is invalid */ 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 string into this builder. * Inserting null will use the stored null text value. * * @param index the index to add at, must be valid * @param str the string to insert * @return this, to enable chaining * @throws IndexOutOfBoundsException if the index is invalid */ 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; }
/** * Updates the length of the builder by either dropping the last characters * or adding filler of unicode zero. * * @param length the length to set to, must be zero or positive * @return this, to enable chaining * @throws IndexOutOfBoundsException if the length is negative */ 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; }
ensureCapacity(size + width); String str = (obj == null ? getNullText() : obj.toString()); if (str == null) {
ensureCapacity(size + width); String str = (obj == null ? getNullText() : obj.toString()); if (str == null) {
/** * Appends a string buffer to this string builder. * Appending null will call {@link #appendNull()}. * * @param str the string buffer to append * @return this, to enable chaining */ 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 to this string builder. * Appending null will call {@link #appendNull()}. * * @param str the string to append * @return this, to enable chaining */ 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; }
/** * Inserts the character array into this builder. * Inserting null will use the stored null text value. * * @param index the index to add at, must be valid * @param chars the char array to insert * @return this, to enable chaining * @throws IndexOutOfBoundsException if the index is invalid */ 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; }
/** * Appends a char array to the string builder. * Appending null will call {@link #appendNull()}. * * @param chars the char array to append * @return this, to enable chaining */ 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 another string builder to this string builder. * Appending null will call {@link #appendNull()}. * * @param str the string builder to append * @return this, to enable chaining */ 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 part of a string to this string builder. * Appending null will call {@link #appendNull()}. * * @param str the string to append * @param startIndex the start index, inclusive, must be valid * @param length the length to append, must be valid * @return this, to enable chaining */ 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 buffer to this string builder. * Appending null will call {@link #appendNull()}. * * @param str the string to append * @param startIndex the start index, inclusive, must be valid * @param length the length to append, must be valid * @return this, to enable chaining */ 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; }
/** * Inserts part of the character array into this builder. * Inserting null will use the stored null text value. * * @param index the index to add at, must be valid * @param chars the char array to insert * @param offset the offset into the character array to start at, must be valid * @param length the length of the character array part to copy, must be positive * @return this, to enable chaining * @throws IndexOutOfBoundsException if any index is invalid */ 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; }
/** * Appends a char array to the string builder. * Appending null will call {@link #appendNull()}. * * @param chars the char array to append * @param startIndex the start index, inclusive, must be valid * @param length the length to append, must be valid * @return this, to enable chaining */ 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 part of a string builder to this string builder. * Appending null will call {@link #appendNull()}. * * @param str the string to append * @param startIndex the start index, inclusive, must be valid * @param length the length to append, must be valid * @return this, to enable chaining */ 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; }