📄 stringbuffer.java
字号:
*/ public synchronized StringBuffer insert(int offset, char str[]) { if ((offset < 0) || (offset > count)) { throw new StringIndexOutOfBoundsException(); } int len = str.length; int newcount = count + len; if (newcount > value.length) expandCapacity(newcount); else if (shared) copy(); if(count != offset) { /* IAI - 17 */ CVM.copyCharArray(value, offset, value, offset + len, count - offset); } if(len != 0) { /* IAI - 17 */ CVM.copyCharArray(str, 0, value, offset, len); } count = newcount; return this; } /** * Inserts the string representation of the <code>boolean</code> * argument into this string buffer. * <p> * The second argument is converted to a string as if by the method * <code>String.valueOf</code>, and the characters of that * string are then inserted into this string buffer at the indicated * offset. * <p> * The offset argument must be greater than or equal to * <code>0</code>, and less than or equal to the length of this * string buffer. * * @param offset the offset. * @param b a <code>boolean</code>. * @return a reference to this <code>StringBuffer</code> object. * @exception StringIndexOutOfBoundsException if the offset is invalid. * @see java.lang.String#valueOf(boolean) * @see java.lang.StringBuffer#insert(int, java.lang.String) * @see java.lang.StringBuffer#length() */ public StringBuffer insert(int offset, boolean b) { return insert(offset, String.valueOf(b)); } /** * Inserts the string representation of the <code>char</code> * argument into this string buffer. * <p> * The second argument is inserted into the contents of this string * buffer at the position indicated by <code>offset</code>. The length * of this string buffer increases by one. * <p> * The overall effect is exactly as if the argument were converted to * a string by the method {@link String#valueOf(char)} and the character * in that string were then {@link #insert(int, String) inserted} into * this <code>StringBuffer</code> object at the position indicated by * <code>offset</code>. * <p> * The offset argument must be greater than or equal to * <code>0</code>, and less than or equal to the length of this * string buffer. * * @param offset the offset. * @param c a <code>char</code>. * @return a reference to this <code>StringBuffer</code> object. * @exception IndexOutOfBoundsException if the offset is invalid. * @see java.lang.StringBuffer#length() */ public synchronized StringBuffer insert(int offset, char c) { int newcount = count + 1; if (newcount > value.length) expandCapacity(newcount); else if (shared) copy();/* IAI - 15 */ if ((offset < 0) || (offset > count)) { throw new StringIndexOutOfBoundsException(offset); } if(count != offset) { /* IAI - 17 */ CVM.copyCharArray(value, offset, value, offset + 1, count - offset); }/* IAI - 15 */ value[offset] = c; count = newcount; return this; } /** * Inserts the string representation of the second <code>int</code> * argument into this string buffer. * <p> * The second argument is converted to a string as if by the method * <code>String.valueOf</code>, and the characters of that * string are then inserted into this string buffer at the indicated * offset. * <p> * The offset argument must be greater than or equal to * <code>0</code>, and less than or equal to the length of this * string buffer. * * @param offset the offset. * @param i an <code>int</code>. * @return a reference to this <code>StringBuffer</code> object. * @exception StringIndexOutOfBoundsException if the offset is invalid. * @see java.lang.String#valueOf(int) * @see java.lang.StringBuffer#insert(int, java.lang.String) * @see java.lang.StringBuffer#length() */ public StringBuffer insert(int offset, int i) { return insert(offset, String.valueOf(i)); } /** * Inserts the string representation of the <code>long</code> * argument into this string buffer. * <p> * The second argument is converted to a string as if by the method * <code>String.valueOf</code>, and the characters of that * string are then inserted into this string buffer at the position * indicated by <code>offset</code>. * <p> * The offset argument must be greater than or equal to * <code>0</code>, and less than or equal to the length of this * string buffer. * * @param offset the offset. * @param l a <code>long</code>. * @return a reference to this <code>StringBuffer</code> object. * @exception StringIndexOutOfBoundsException if the offset is invalid. * @see java.lang.String#valueOf(long) * @see java.lang.StringBuffer#insert(int, java.lang.String) * @see java.lang.StringBuffer#length() */ public StringBuffer insert(int offset, long l) { return insert(offset, String.valueOf(l)); } /** * Inserts the string representation of the <code>float</code> * argument into this string buffer. * <p> * The second argument is converted to a string as if by the method * <code>String.valueOf</code>, and the characters of that * string are then inserted into this string buffer at the indicated * offset. * <p> * The offset argument must be greater than or equal to * <code>0</code>, and less than or equal to the length of this * string buffer. * * @param offset the offset. * @param f a <code>float</code>. * @return a reference to this <code>StringBuffer</code> object. * @exception StringIndexOutOfBoundsException if the offset is invalid. * @see java.lang.String#valueOf(float) * @see java.lang.StringBuffer#insert(int, java.lang.String) * @see java.lang.StringBuffer#length() */ public StringBuffer insert(int offset, float f) { return insert(offset, String.valueOf(f)); } /** * Inserts the string representation of the <code>double</code> * argument into this string buffer. * <p> * The second argument is converted to a string as if by the method * <code>String.valueOf</code>, and the characters of that * string are then inserted into this string buffer at the indicated * offset. * <p> * The offset argument must be greater than or equal to * <code>0</code>, and less than or equal to the length of this * string buffer. * * @param offset the offset. * @param d a <code>double</code>. * @return a reference to this <code>StringBuffer</code> object. * @exception StringIndexOutOfBoundsException if the offset is invalid. * @see java.lang.String#valueOf(double) * @see java.lang.StringBuffer#insert(int, java.lang.String) * @see java.lang.StringBuffer#length() */ public StringBuffer insert(int offset, double d) { return insert(offset, String.valueOf(d)); } /** * Returns the index within this string of the first occurrence of the * specified substring. The integer returned is the smallest value * <i>k</i> such that: * <blockquote><pre> * this.toString().startsWith(str, <i>k</i>) * </pre></blockquote> * is <code>true</code>. * * @param str any string. * @return if the string argument occurs as a substring within this * object, then the index of the first character of the first * such substring is returned; if it does not occur as a * substring, <code>-1</code> is returned. * @exception java.lang.NullPointerException if <code>str</code> is * <code>null</code>. * @since 1.4 */ public int indexOf(String str) { return indexOf(str, 0); } /** * Returns the index within this string of the first occurrence of the * specified substring, starting at the specified index. The integer * returned is the smallest value <tt>k</tt> for which: * <blockquote><pre> * k >= Math.min(fromIndex, str.length()) && * this.toString().startsWith(str, k) * </pre></blockquote> * If no such value of <i>k</i> exists, then -1 is returned. * * @param str the substring for which to search. * @param fromIndex the index from which to start the search. * @return the index within this string of the first occurrence of the * specified substring, starting at the specified index. * @exception java.lang.NullPointerException if <code>str</code> is * <code>null</code>. * @since 1.4 */ public synchronized int indexOf(String str, int fromIndex) { return String.indexOf(value, 0, count, str.toCharArray(), 0, str.length(), fromIndex); } /** * Returns the index within this string of the rightmost occurrence * of the specified substring. The rightmost empty string "" is * considered to occur at the index value <code>this.length()</code>. * The returned index is the largest value <i>k</i> such that * <blockquote><pre> * this.toString().startsWith(str, k) * </pre></blockquote> * is true. * * @param str the substring to search for. * @return if the string argument occurs one or more times as a substring * within this object, then the index of the first character of * the last such substring is returned. If it does not occur as * a substring, <code>-1</code> is returned. * @exception java.lang.NullPointerException if <code>str</code> is * <code>null</code>. * @since 1.4 */ public synchronized int lastIndexOf(String str) { return lastIndexOf(str, count); } /** * Returns the index within this string of the last occurrence of the * specified substring. The integer returned is the largest value <i>k</i> * such that: * <blockquote><pre> * k <= Math.min(fromIndex, str.length()) && * this.toString().startsWith(str, k) * </pre></blockquote> * If no such value of <i>k</i> exists, then -1 is returned. * * @param str the substring to search for. * @param fromIndex the index to start the search from. * @return the index within this string of the last occurrence of the * specified substring. * @exception java.lang.NullPointerException if <code>str</code> is * <code>null</code>. * @since 1.4 */ public synchronized int lastIndexOf(String str, int fromIndex) { return String.lastIndexOf(value, 0, count, str.toCharArray(), 0, str.length(), fromIndex); } /** * The character sequence contained in this string buffer is * replaced by the reverse of the sequence. * <p> * Let <i>n</i> be the length of the old character sequence, the one * contained in the string buffer just prior to execution of the * <code>reverse</code> method. Then the character at index <i>k</i> in * the new character sequence is equal to the character at index * <i>n-k-1</i> in the old character sequence. * * @return a reference to this <code>StringBuffer</code> object. * @since JDK1.0.2 */ public synchronized StringBuffer reverse() { if (shared) copy(); int n = count - 1; for (int j = (n-1) >> 1; j >= 0; --j) { char temp = value[j]; value[j] = value[n - j]; value[n - j] = temp; } return this; } /** * Converts to a string representing the data in this string buffer. * A new <code>String</code> object is allocated and initialized to * contain the character sequence currently represented by this * string buffer. This <code>String</code> is then returned. Subsequent * changes to the string buffer do not affect the contents of the * <code>String</code>. * <p> * Implementation advice: This method can be coded so as to create a new * <code>String</code> object without allocating new memory to hold a * copy of the character sequence. Instead, the string can share the * memory used by the string buffer. Any subsequent operation that alters * the content or capacity of the string buffer must then make a copy of * the internal buffer at that time. This strategy is effective for * reducing the amount of memory allocated by a string concatenation * operation when it is implemented using a string buffer. * * @return a string representation of the string buffer. */ public String toString() { return new String(this); } // // The following two methods are needed by String to efficiently // convert a StringBuffer into a String. They are not public. // They shouldn't be called by anyone but String. final void setShared() { shared = true; } final char[] getValue() { return value; } /** * readObject is called to restore the state of the StringBuffer from * a stream. */ private synchronized void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException { s.defaultReadObject(); value = (char[]) value.clone(); shared = false; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -