📄 stringbuffer.java
字号:
} /** 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. * @param inum the <code>int</code> to convert and insert. * @return this <code>StringBuffer</code>. * @exception IndexOutOfBoundsException if <code>offset</code> is out * of range for this <code>StringBuffer</code>. * @see java.lang.String#valueOf(int) */ public StringBuffer insert (int offset, int inum) { return insert (offset, String.valueOf(inum)); } /** 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. * @param lnum the <code>long</code> to convert and insert. * @return this <code>StringBuffer</code>. * @exception IndexOutOfBoundsException if <code>offset</code> is out * of range for this <code>StringBuffer</code>. * @see java.lang.String#valueOf(long) */ public StringBuffer insert (int offset, long lnum) { return insert (offset, String.valueOf(lnum)); } /** 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. * @param fnum the <code>float</code> to convert and insert. * @return this <code>StringBuffer</code>. * @exception IndexOutOfBoundsException if <code>offset</code> is out * of range for this <code>StringBuffer</code>. * @see java.lang.String#valueOf(float) */ public StringBuffer insert (int offset, float fnum) { return insert (offset, String.valueOf(fnum)); } /** 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. * @param dnum the <code>double</code> to convert and insert. * @return this <code>StringBuffer</code>. * @exception IndexOutOfBoundsException if <code>offset</code> is out * of range for this <code>StringBuffer</code>. * @see java.lang.String#valueOf(double) */ public StringBuffer insert (int offset, double dnum) { return insert (offset, String.valueOf(dnum)); } /** 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. * @param obj the <code>Object</code> to convert and insert. * @return this <code>StringBuffer</code>. * @exception IndexOutOfBoundsException if <code>offset</code> is out * of range for this <code>StringBuffer</code>. * @see java.lang.String#valueOf(java.lang.Object) */ public StringBuffer insert (int offset, Object obj) { return insert (offset, String.valueOf(obj)); } /** Insert the <code>String</code> argument into this <code>StringBuffer</code>. * @param offset the place to insert. * @param str the <code>String</code> to insert. * @return this <code>StringBuffer</code>. * @exception IndexOutOfBoundsException if <code>offset</code> is out * of range for this <code>StringBuffer</code>. */ public synchronized StringBuffer insert (int offset, String str) { if (offset < 0 || offset > count) throw new StringIndexOutOfBoundsException (offset); // Note that using `null' is from JDK 1.2. if (str == null) str = "null"; int len = str.length(); ensureCapacity_unsynchronized (count+len); System.arraycopy(value, offset, value, offset+len, count-offset); str.getChars(0, len, value, offset); count += len; return this; } /** Insert the <code>char[]</code> argument into this * <code>StringBuffer</code>. * @param offset the place to insert. * @param data the <code>char[]</code> to insert. * @return this <code>StringBuffer</code>. * @exception NullPointerException if <code>data</code> is * <code>null</code>. * @exception IndexOutOfBoundsException if <code>offset</code> is out * of range for this <code>StringBuffer</code>. */ public StringBuffer insert (int offset, char[] data) { // One could check if offset is invalid here instead of making sure that // data isn't null before dereferencing, but this works just as well. return insert (offset, data, 0, data == null ? 0 : data.length); } /** Insert the <code>char[]</code> argument into this * <code>StringBuffer</code>. * @param offset the place to insert. * @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>. * @exception NullPointerException if <code>str</code> is <code>null</code>. * @exception IndexOutOfBoundsException if <code>offset</code> is out * of range, for this <code>StringBuffer</code>, or if * <code>str_offset</code> or <code>str_offset+len</code> * are out of range for <code>str</code>. */ public synchronized StringBuffer insert(int offset, char[] str, int str_offset, int len) { if (offset < 0 || offset > count) throw new StringIndexOutOfBoundsException (offset); if (len < 0) throw new StringIndexOutOfBoundsException (len); if (str_offset < 0 || str_offset + len > str.length) throw new StringIndexOutOfBoundsException (str_offset); ensureCapacity_unsynchronized (count + len); System.arraycopy(value, offset, value, offset + len, count - offset); System.arraycopy(str, str_offset, value, offset, len); count += len; return this; } /** Get the length of the <code>String</code> this * <code>StringBuffer</code> would create. Not to be confused with the * <em>capacity</em> of the <code>StringBuffer</code>. * @return the length of this <code>StringBuffer</code>. * @see #capacity() * @see #setLength(int) */ public int length () { return count; } /** 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>. */ public synchronized StringBuffer replace (int start, int end, String str) { if (start < 0 || start > count || start > end) throw new StringIndexOutOfBoundsException (start); int len = str.length(); // 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) System.arraycopy(value, end, value, end + delta, count - end); str.getChars (0, len, value, start); count += delta; return this; } /** Reverse the characters in this StringBuffer. * @return this <code>StringBuffer</code>. */ public synchronized StringBuffer reverse () { // Call ensureCapacity to enforce copy-on-write. ensureCapacity_unsynchronized (count); for (int i = 0; i < count / 2; ++i) { char c = value[i]; value[i] = value[count - i - 1]; value[count - i - 1] = c; } return this; } /** Set the character at the specified index. * @param index the index of the character to set starting at 0. * @param ch the value to set that character to. * @exception IndexOutOfBoundsException if the specified character * index is not between 0 and length() - 1 (inclusive). */ public synchronized void setCharAt (int index, char ch) { if (index < 0 || index >= count) throw new StringIndexOutOfBoundsException (index); // Call ensureCapacity to enforce copy-on-write. ensureCapacity_unsynchronized (count); value[index] = ch; } /** Set the length of this StringBuffer. * <P> * If the new length is greater than the current length, all the new * characters are set to '\0'. * <P> * If the new length is less than the current length, the first * <code>newLength</code> characters of the old array will be * @param newLength the new length * @exception IndexOutOfBoundsException if the new length is * negative. * @see #length() */ public synchronized void setLength (int newLength) { if (newLength < 0) throw new StringIndexOutOfBoundsException (newLength); ensureCapacity_unsynchronized (newLength); for (int i = count; i < newLength; ++i) value[i] = '\0'; count = newLength; } /** Create a new StringBuffer with default capacity 16. * @see JLS 20.13.1 */ public StringBuffer () { this (DEFAULT_CAPACITY); } /** Create an empty <code>StringBuffer</code> with the specified initial capacity. * @param capacity the initial capacity. */ public StringBuffer (int capacity) { count = 0; value = new char[capacity]; shared = false; } /** Create a new <code>StringBuffer</code> with the characters in the specified <code>String</code>. * Initial capacity will be the size of the String plus 16. * @param str the <code>String</code> to make a <code>StringBuffer</code> out of. * @XXX optimize for sharing. */ public StringBuffer (String str) { // The documentation is not clear, but experimentation with // other implementations indicates that StringBuffer(null) // should throw a NullPointerException. count = str.length(); // JLS: The initial capacity of the string buffer is 16 plus the // length of the argument string. value = new char[count + DEFAULT_CAPACITY]; str.getChars(0, count, value, 0); shared = false; } /** * 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 * * @exception StringIndexOutOfBoundsException * if (beginIndex < 0 || beginIndex > this.length()) */ 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. * * @param beginIndex index to start substring (base 0) * @param endIndex index after the last character to be * copied into the substring * * @return new String which is a substring of this StringBuffer * * @exception StringIndexOutOfBoundsException * if (beginIndex < 0 || endIndex > this.length() || beginIndex > endIndex) */ public synchronized String substring (int beginIndex, int endIndex) { if (beginIndex < 0 || endIndex > count || beginIndex > endIndex) throw new StringIndexOutOfBoundsException (); // FIXME: for libgcj it would be possible, and more efficient, to // enable sharing here. return new String (value, beginIndex, endIndex - beginIndex); } /** * Creates a substring of this StringBuffer, starting at a specified index * and ending at one character before a specified index. * <p> * To implement <code>CharSequence</code>. * Calls <code>substring(beginIndex, endIndex)</code>. * * @param beginIndex index to start substring (base 0) * @param endIndex index after the last character to be * copied into the substring * * @return new String which is a substring of this StringBuffer * * @exception StringIndexOutOfBoundsException * if (beginIndex < 0 || endIndex > this.length() || beginIndex > endIndex) */ public CharSequence subSequence (int beginIndex, int endIndex) { return substring(beginIndex, endIndex); } /** Convert this <code>StringBuffer</code> to a <code>String</code>. * @return the characters in this StringBuffer */ public String toString () { // Note: in libgcj this causes the StringBuffer to be shared. In // Classpath it does not. return new String (this); } // Index of next available character. Note that this has // permissions set this way so that String can get the value. int count; // The buffer. Note that this has permissions set this way so that // String can get the value. char[] value; // True if we need to copy the buffer before writing to it again. // FIXME: JDK 1.2 doesn't specify this. The new buffer-growing // semantics make this less useful in that case, too. Note that // this has permissions set this way so that String can get the // value. boolean shared; static final long serialVersionUID = 3388685877147921107L; private final static int DEFAULT_CAPACITY = 16; // JLS 20.13.1}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -