📄 stringbuilder.java
字号:
str.getChars(0, len, value, count); count += len; return this; } /** * Append the <code>StringBuilder</code> value of the argument to this * <code>StringBuilder</code>. This behaves the same as * <code>append((Object) stringBuffer)</code>, except it is more efficient. * * @param stringBuffer the <code>StringBuilder</code> to convert and append * @return this <code>StringBuilder</code> * @see #append(Object) */ public StringBuilder append(StringBuffer stringBuffer) { if (stringBuffer == null) return append("null"); synchronized (stringBuffer) { int len = stringBuffer.count; ensureCapacity(count + len); System.arraycopy(stringBuffer.value, 0, value, count, len); count += len; } return this; } /** * Append the <code>char</code> array to this <code>StringBuilder</code>. * This is similar (but more efficient) than * <code>append(new String(data))</code>, except in the case of null. * * @param data the <code>char[]</code> to append * @return this <code>StringBuilder</code> * @throws NullPointerException if <code>str</code> is <code>null</code> * @see #append(char[], int, int) */ public StringBuilder append(char[] data) { return append(data, 0, data.length); } /** * Append part of the <code>char</code> array to this * <code>StringBuilder</code>. This is similar (but more efficient) than * <code>append(new String(data, offset, count))</code>, except in the case * of null. * * @param data the <code>char[]</code> to append * @param offset the start location in <code>str</code> * @param count the number of characters to get from <code>str</code> * @return this <code>StringBuilder</code> * @throws NullPointerException if <code>str</code> is <code>null</code> * @throws IndexOutOfBoundsException if offset or count is out of range * (while unspecified, this is a StringIndexOutOfBoundsException) */ public StringBuilder append(char[] data, int offset, int count) { if (offset < 0 || count < 0 || offset > data.length - count) throw new StringIndexOutOfBoundsException(); ensureCapacity(this.count + count); System.arraycopy(data, offset, value, this.count, count); this.count += count; return this; } /** * Append the <code>String</code> value of the argument to this * <code>StringBuilder</code>. Uses <code>String.valueOf()</code> to convert * to <code>String</code>. * * @param bool the <code>boolean</code> to convert and append * @return this <code>StringBuilder</code> * @see String#valueOf(boolean) */ public StringBuilder append(boolean bool) { return append(bool ? "true" : "false"); } /** * Append the <code>char</code> to this <code>StringBuilder</code>. * * @param ch the <code>char</code> to append * @return this <code>StringBuilder</code> */ public StringBuilder append(char ch) { ensureCapacity(count + 1); value[count++] = ch; return this; } /** * Append the characters in the <code>CharSequence</code> to this * buffer. * * @param seq the <code>CharSequence</code> providing the characters * @return this <code>StringBuilder</code> */ public StringBuilder append(CharSequence seq) { return append(seq, 0, seq.length()); } /** * Append some characters from the <code>CharSequence</code> to this * buffer. If the argument is null, the four characters "null" are * appended. * * @param seq the <code>CharSequence</code> providing the characters * @param start the starting index * @param end one past the final index * @return this <code>StringBuilder</code> */ public StringBuilder append(CharSequence seq, int start, int end) { if (seq == null) return append("null"); if (end - start > 0) { ensureCapacity(count + end - start); for (; start < end; ++start) value[count++] = seq.charAt(start); } return this; } /** * Append the code point to this <code>StringBuilder</code>. * This is like #append(char), but will append two characters * if a supplementary code point is given. * * @param code the code point to append * @return this <code>StringBuilder</code> * @see Character#toChars(int, char[], int) * @since 1.5 */ public synchronized StringBuilder appendCodePoint(int code) { int len = Character.charCount(code); ensureCapacity(count + len); Character.toChars(code, value, count); count += len; return this; } /** * Append the <code>String</code> value of the argument to this * <code>StringBuilder</code>. Uses <code>String.valueOf()</code> to convert * to <code>String</code>. * * @param inum the <code>int</code> to convert and append * @return this <code>StringBuilder</code> * @see String#valueOf(int) */ // FIXME: this is native in libgcj in StringBuffer. public StringBuilder append(int inum) { return append(String.valueOf(inum)); } /** * Append the <code>String</code> value of the argument to this * <code>StringBuilder</code>. Uses <code>String.valueOf()</code> to convert * to <code>String</code>. * * @param lnum the <code>long</code> to convert and append * @return this <code>StringBuilder</code> * @see String#valueOf(long) */ public StringBuilder append(long lnum) { return append(Long.toString(lnum, 10)); } /** * Append the <code>String</code> value of the argument to this * <code>StringBuilder</code>. Uses <code>String.valueOf()</code> to convert * to <code>String</code>. * * @param fnum the <code>float</code> to convert and append * @return this <code>StringBuilder</code> * @see String#valueOf(float) */ public StringBuilder append(float fnum) { return append(Float.toString(fnum)); } /** * Append the <code>String</code> value of the argument to this * <code>StringBuilder</code>. Uses <code>String.valueOf()</code> to convert * to <code>String</code>. * * @param dnum the <code>double</code> to convert and append * @return this <code>StringBuilder</code> * @see String#valueOf(double) */ public StringBuilder append(double dnum) { return append(Double.toString(dnum)); } /** * Delete characters from this <code>StringBuilder</code>. * <code>delete(10, 12)</code> will delete 10 and 11, but not 12. It is * harmless for end to be larger than length(). * * @param start the first character to delete * @param end the index after the last character to delete * @return this <code>StringBuilder</code> * @throws StringIndexOutOfBoundsException if start or end are out of bounds */ public StringBuilder delete(int start, int end) { if (start < 0 || start > count || start > end) throw new StringIndexOutOfBoundsException(start); if (end > count) end = count; // This will unshare if required. ensureCapacity(count); if (count - end != 0) System.arraycopy(value, end, value, start, count - end); count -= end - start; return this; } /** * Delete a character from this <code>StringBuilder</code>. * * @param index the index of the character to delete * @return this <code>StringBuilder</code> * @throws StringIndexOutOfBoundsException if index is out of bounds */ public StringBuilder deleteCharAt(int index) { return delete(index, index + 1); } /** * Replace characters between index <code>start</code> (inclusive) and * <code>end</code> (exclusive) with <code>str</code>. If <code>end</code> * is larger than the size of this StringBuilder, all characters after * <code>start</code> are replaced. * * @param start the beginning index of characters to delete (inclusive) * @param end the ending index of characters to delete (exclusive) * @param str the new <code>String</code> to insert * @return this <code>StringBuilder</code> * @throws StringIndexOutOfBoundsException if start or end are out of bounds * @throws NullPointerException if str is null */ public StringBuilder replace(int start, int end, String str) { if (start < 0 || start > count || start > end) throw new StringIndexOutOfBoundsException(start); int len = str.count; // Calculate the difference in 'count' after the replace. int delta = len - (end > count ? count : end) + start; ensureCapacity(count + delta); if (delta != 0 && end < count) System.arraycopy(value, end, value, end + delta, count - end); str.getChars(0, len, value, start); count += delta; return this; } /** * Creates a substring of this StringBuilder, starting at a specified index * and ending at the end of this StringBuilder. * * @param beginIndex index to start substring (base 0) * @return new String which is a substring of this StringBuilder * @throws StringIndexOutOfBoundsException if beginIndex is out of bounds * @see #substring(int, int) */ public String substring(int beginIndex) { return substring(beginIndex, count); } /** * 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 <code>substring(beginIndex, endIndex)</code>, to satisfy * the CharSequence interface. * * @param beginIndex index to start at (inclusive, base 0) * @param endIndex index to end at (exclusive) * @return new String which is a substring of this StringBuilder * @throws IndexOutOfBoundsException if beginIndex or endIndex is out of * bounds * @see #substring(int, int) */ public CharSequence subSequence(int beginIndex, int endIndex) { return substring(beginIndex, endIndex); } /** * Creates a substring of this StringBuilder, starting at a specified index * and ending at one character before a specified index. * * @param beginIndex index to start at (inclusive, base 0) * @param endIndex index to end at (exclusive) * @return new String which is a substring of this StringBuilder * @throws StringIndexOutOfBoundsException if beginIndex or endIndex is out * of bounds */ public String substring(int beginIndex, int endIndex) { int len = endIndex - beginIndex; if (beginIndex < 0 || endIndex > count || endIndex < beginIndex) throw new StringIndexOutOfBoundsException(); if (len == 0) return ""; return new String(value, beginIndex, len); } /** * Insert a subarray of the <code>char[]</code> argument into this * <code>StringBuilder</code>. * * @param offset the place to insert in this buffer * @param str the <code>char[]</code> to insert * @param str_offset the index in <code>str</code> to start inserting from * @param len the number of characters to insert * @return this <code>StringBuilder</code> * @throws NullPointerException if <code>str</code> is <code>null</code>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -