📄 stringbuffer.java
字号:
* @return this <code>StringBuffer</code> * @see #append(Object) * @since 1.5 */ public synchronized StringBuffer append(CharSequence sequence) { if (sequence == null) sequence = "null"; return append(sequence, 0, sequence.length()); } /** * Append the specified subsequence of the <code>CharSequence</code> * argument to this <code>StringBuffer</code>. * * @param sequence the <code>CharSequence</code> to append * @param start the starting index * @param end one past the ending index * @return this <code>StringBuffer</code> * @see #append(Object) * @since 1.5 */ public synchronized StringBuffer append(CharSequence sequence, int start, int end) { if (sequence == null) sequence = "null"; if (start < 0 || end < 0 || start > end || end > sequence.length()) throw new IndexOutOfBoundsException(); ensureCapacity_unsynchronized(this.count + end - start); for (int i = start; i < end; ++i) value[count++] = sequence.charAt(i); return this; } /** * Append the <code>char</code> array to this <code>StringBuffer</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>StringBuffer</code> * @throws NullPointerException if <code>str</code> is <code>null</code> * @see #append(char[], int, int) */ public StringBuffer append(char[] data) { return append(data, 0, data.length); } /** * Append part of the <code>char</code> array to this * <code>StringBuffer</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>StringBuffer</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 synchronized StringBuffer append(char[] data, int offset, int count) { if (offset < 0 || count < 0 || offset > data.length - count) throw new StringIndexOutOfBoundsException(); ensureCapacity_unsynchronized(this.count + count); VMSystem.arraycopy(data, offset, value, this.count, count); this.count += count; return this; } /** * Append the <code>String</code> value of the argument to this * <code>StringBuffer</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>StringBuffer</code> * @see String#valueOf(boolean) */ public StringBuffer append(boolean bool) { return append(bool ? "true" : "false"); } /** * Append the <code>char</code> to this <code>StringBuffer</code>. * * @param ch the <code>char</code> to append * @return this <code>StringBuffer</code> */ public synchronized StringBuffer append(char ch) { ensureCapacity_unsynchronized(count + 1); value[count++] = ch; return this; } /** * Append the code point to this <code>StringBuffer</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>StringBuffer</code> * @see Character#toChars(int, char[], int) * @since 1.5 */ public synchronized StringBuffer appendCodePoint(int code) { int len = Character.charCount(code); ensureCapacity_unsynchronized(count + len); Character.toChars(code, value, count); count += len; return this; } /** * Append the <code>String</code> value of the argument to this * <code>StringBuffer</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>StringBuffer</code> * @see String#valueOf(int) */ // This is native in libgcj, for efficiency. public StringBuffer append(int inum) { return append(String.valueOf(inum)); } /** * Append the <code>String</code> value of the argument to this * <code>StringBuffer</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>StringBuffer</code> * @see String#valueOf(long) */ public StringBuffer append(long lnum) { return append(Long.toString(lnum, 10)); } /** * Append the <code>String</code> value of the argument to this * <code>StringBuffer</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>StringBuffer</code> * @see String#valueOf(float) */ public StringBuffer append(float fnum) { return append(Float.toString(fnum)); } /** * Append the <code>String</code> value of the argument to this * <code>StringBuffer</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>StringBuffer</code> * @see String#valueOf(double) */ public StringBuffer append(double dnum) { return append(Double.toString(dnum)); } /** * Delete characters from this <code>StringBuffer</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>StringBuffer</code> * @throws StringIndexOutOfBoundsException if start or end are out of bounds * @since 1.2 */ public synchronized StringBuffer 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_unsynchronized(count); if (count - end != 0) VMSystem.arraycopy(value, end, value, start, count - end); count -= end - start; return this; } /** * Delete a character from this <code>StringBuffer</code>. * * @param index the index of the character to delete * @return this <code>StringBuffer</code> * @throws StringIndexOutOfBoundsException if index is out of bounds * @since 1.2 */ public StringBuffer 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 StringBuffer, 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>StringBuffer</code> * @throws StringIndexOutOfBoundsException if start or end are out of bounds * @throws NullPointerException if str is null * @since 1.2 */ public synchronized StringBuffer 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_unsynchronized(count + delta); if (delta != 0 && end < count) VMSystem.arraycopy(value, end, value, end + delta, count - end); str.getChars(0, len, value, start); count += delta; return this; } /** * Creates a substring of this StringBuffer, starting at a specified index * and ending at the end of this StringBuffer. * * @param beginIndex index to start substring (base 0) * @return new String which is a substring of this StringBuffer * @throws StringIndexOutOfBoundsException if beginIndex is out of bounds * @see #substring(int, int) * @since 1.2 */ public String substring(int beginIndex) { return substring(beginIndex, count); } /** * Creates a substring of this StringBuffer, 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 StringBuffer * @throws IndexOutOfBoundsException if beginIndex or endIndex is out of * bounds * @see #substring(int, int) * @since 1.4 */ public CharSequence subSequence(int beginIndex, int endIndex) { return substring(beginIndex, endIndex); } /** * Creates a substring of this StringBuffer, 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 StringBuffer * @throws StringIndexOutOfBoundsException if beginIndex or endIndex is out * of bounds * @since 1.2 */ public synchronized String substring(int beginIndex, int endIndex) { int len = endIndex - beginIndex; if (beginIndex < 0 || endIndex > count || endIndex < beginIndex) throw new StringIndexOutOfBoundsException(); if (len == 0) return ""; // Don't copy unless substring is smaller than 1/4 of the buffer. boolean share_buffer = ((len << 2) >= value.length); if (share_buffer) this.shared = true; // Package constructor avoids an array copy. return new String(value, beginIndex, len, share_buffer); } /** * Insert a subarray of the <code>char[]</code> argument into this * <code>StringBuffer</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>StringBuffer</code> * @throws NullPointerException if <code>str</code> is <code>null</code> * @throws StringIndexOutOfBoundsException if any index is out of bounds * @since 1.2 */ public synchronized StringBuffer insert(int offset, char[] str, int str_offset, int len) { if (offset < 0 || offset > count || len < 0 || str_offset < 0 || str_offset > str.length - len) throw new StringIndexOutOfBoundsException(); ensureCapacity_unsynchronized(count + len); VMSystem.arraycopy(value, offset, value, offset + len, count - offset); VMSystem.arraycopy(str, str_offset, value, offset, len); count += len; return this; } /** * Insert the <code>String</code> value of the argument into this * <code>StringBuffer</code>. Uses <code>String.valueOf()</code> to convert * to <code>String</code>. * * @param offset the place to insert in this buffer * @param obj the <code>Object</code> to convert and insert * @return this <code>StringBuffer</code> * @exception StringIndexOutOfBoundsException if offset is out of bounds * @see String#valueOf(Object) */ public StringBuffer insert(int offset, Object obj) { return insert(offset, obj == null ? "null" : obj.toString()); } /** * Insert the <code>String</code> argument into this * <code>StringBuffer</code>. If str is null, the String "null" is used * instead. * * @param offset the place to insert in this buffer * @param str the <code>String</code> to insert * @return this <code>StringBuffer</code> * @throws StringIndexOutOfBoundsException if offset is out of bounds */ public synchronized StringBuffer insert(int offset, String str) { if (offset < 0 || offset > count) throw new StringIndexOutOfBoundsException(offset); if (str == null) str = "null"; int len = str.count; ensureCapacity_unsynchronized(count + len); VMSystem.arraycopy(value, offset, value, offset + len, count - offset); str.getChars(0, len, value, offset); count += len; return this; } /** * Insert the <code>CharSequence</code> argument into this * <code>StringBuffer</code>. If the sequence is null, the String * "null" is used instead. * * @param offset the place to insert in this buffer * @param sequence the <code>CharSequence</code> to insert * @return this <code>StringBuffer</code> * @throws IndexOutOfBoundsException if offset is out of bounds * @since 1.5 */ public synchronized StringBuffer insert(int offset, CharSequence sequence) { if (sequence == null) sequence = "null"; return insert(offset, sequence, 0, sequence.length()); } /** * Insert a subsequence of the <code>CharSequence</code> argument into this * <code>StringBuffer</code>. If the sequence is null, the String * "null" is used instead. * * @param offset the place to insert in this buffer
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -