📄 stringbuffer.java
字号:
* @return a reference to this <code>StringBuffer</code> object. * @see java.lang.String#valueOf(float) * @see java.lang.StringBuffer#append(java.lang.String) */ public synchronized StringBuffer append(float f) { new FloatingDecimal(f).appendTo(this); return this; } /** * Appends the string representation of the <code>double</code> * argument to this string buffer. * <p> * The argument is converted to a string as if by the method * <code>String.valueOf</code>, and the characters of that * string are then appended to this string buffer. * * @param d a <code>double</code>. * @return a reference to this <code>StringBuffer</code> object. * @see java.lang.String#valueOf(double) * @see java.lang.StringBuffer#append(java.lang.String) */ public synchronized StringBuffer append(double d) { new FloatingDecimal(d).appendTo(this); return this; } /** * Removes the characters in a substring of this <code>StringBuffer</code>. * The substring begins at the specified <code>start</code> and extends to * the character at index <code>end - 1</code> or to the end of the * <code>StringBuffer</code> if no such character exists. If * <code>start</code> is equal to <code>end</code>, no changes are made. * * @param start The beginning index, inclusive. * @param end The ending index, exclusive. * @return This string buffer. * @exception StringIndexOutOfBoundsException if <code>start</code> * is negative, greater than <code>length()</code>, or * greater than <code>end</code>. * @since 1.2 */ public synchronized StringBuffer delete(int start, int end) { if (start < 0) throw new StringIndexOutOfBoundsException(start); if (end > count) end = count; if (start > end) throw new StringIndexOutOfBoundsException(); int len = end - start; if (len > 0) { if (shared) copy(); if(count != end) { /* IAI - 17 */ CVM.copyCharArray(value, start+len, value, start, count-end); } count -= len; } return this; } /** * Removes the character at the specified position in this * <code>StringBuffer</code> (shortening the <code>StringBuffer</code> * by one character). * * @param index Index of character to remove * @return This string buffer. * @exception StringIndexOutOfBoundsException if the <code>index</code> * is negative or greater than or equal to * <code>length()</code>. * @since 1.2 */ public synchronized StringBuffer deleteCharAt(int index) { if ((index < 0) || (index >= count)) throw new StringIndexOutOfBoundsException(); if (shared) copy(); if(count - index - 1 != 0) { /* IAI - 17 */ CVM.copyCharArray(value, index+1, value, index, count-index-1); } count--; return this; } /** * Replaces the characters in a substring of this <code>StringBuffer</code> * with characters in the specified <code>String</code>. The substring * begins at the specified <code>start</code> and extends to the character * at index <code>end - 1</code> or to the end of the * <code>StringBuffer</code> if no such character exists. First the * characters in the substring are removed and then the specified * <code>String</code> is inserted at <code>start</code>. (The * <code>StringBuffer</code> will be lengthened to accommodate the * specified String if necessary.) * * @param start The beginning index, inclusive. * @param end The ending index, exclusive. * @param str String that will replace previous contents. * @return This string buffer. * @exception StringIndexOutOfBoundsException if <code>start</code> * is negative, greater than <code>length()</code>, or * greater than <code>end</code>. * @since 1.2 */ public synchronized StringBuffer replace(int start, int end, String str) { if (start < 0) throw new StringIndexOutOfBoundsException(start); if (end > count) end = count; if (start > end) throw new StringIndexOutOfBoundsException(); int len = str.length(); int newCount = count + len - (end - start); if (newCount > value.length) expandCapacity(newCount); else if (shared) copy(); if(count != end) { /* IAI - 17 */ CVM.copyCharArray(value, end, value, start + len, count - end); } str.getChars(0, len, value, start); count = newCount; return this; } /** * Returns a new <code>String</code> that contains a subsequence of * characters currently contained in this <code>StringBuffer</code>.The * substring begins at the specified index and extends to the end of the * <code>StringBuffer</code>. * * @param start The beginning index, inclusive. * @return The new string. * @exception StringIndexOutOfBoundsException if <code>start</code> is * less than zero, or greater than the length of this * <code>StringBuffer</code>. * @since 1.2 */ public synchronized String substring(int start) { return substring(start, count); } /** * Returns a new character sequence that is a subsequence of this sequence. * * <p> An invocation of this method of the form * * <blockquote><pre> * sb.subSequence(begin, end)</pre></blockquote> * * behaves in exactly the same way as the invocation * * <blockquote><pre> * sb.substring(begin, end)</pre></blockquote> * * This method is provided so that the <tt>StringBuffer</tt> class can * implement the {@link CharSequence} interface. </p> * * @param start the start index, inclusive. * @param end the end index, exclusive. * @return the specified subsequence. * * @throws IndexOutOfBoundsException * if <tt>start</tt> or <tt>end</tt> are negative, * if <tt>end</tt> is greater than <tt>length()</tt>, * or if <tt>start</tt> is greater than <tt>end</tt> * * @since 1.4 * @spec JSR-51 */ public CharSequence subSequence(int start, int end) { return this.substring(start, end); } /** * Returns a new <code>String</code> that contains a subsequence of * characters currently contained in this <code>StringBuffer</code>. The * substring begins at the specified <code>start</code> and * extends to the character at index <code>end - 1</code>. An * exception is thrown if * * @param start The beginning index, inclusive. * @param end The ending index, exclusive. * @return The new string. * @exception StringIndexOutOfBoundsException if <code>start</code> * or <code>end</code> are negative or greater than * <code>length()</code>, or <code>start</code> is * greater than <code>end</code>. * @since 1.2 */ public synchronized String substring(int start, int end) { if (start < 0) throw new StringIndexOutOfBoundsException(start); if (end > count) throw new StringIndexOutOfBoundsException(end); if (start > end) throw new StringIndexOutOfBoundsException(end - start); return new String(value, start, end - start); } /** * Inserts the string representation of a subarray of the <code>str</code> * array argument into this string buffer. The subarray begins at the * specified <code>offset</code> and extends <code>len</code> characters. * The characters of the subarray are inserted into this string buffer at * the position indicated by <code>index</code>. The length of this * <code>StringBuffer</code> increases by <code>len</code> characters. * * @param index position at which to insert subarray. * @param str A character array. * @param offset the index of the first character in subarray to * to be inserted. * @param len the number of characters in the subarray to * to be inserted. * @return This string buffer. * @exception StringIndexOutOfBoundsException if <code>index</code> * is negative or greater than <code>length()</code>, or * <code>offset</code> or <code>len</code> are negative, or * <code>(offset+len)</code> is greater than * <code>str.length</code>. * @since 1.2 */ public synchronized StringBuffer insert(int index, char str[], int offset, int len) { if ((index < 0) || (index > count)) throw new StringIndexOutOfBoundsException(); if ((offset < 0) || (offset + len < 0) || (offset + len > str.length)) throw new StringIndexOutOfBoundsException(offset); if (len < 0) throw new StringIndexOutOfBoundsException(len); int newCount = count + len; if (newCount > value.length) expandCapacity(newCount); else if (shared) copy(); if(count != index) { /* IAI - 17 */ CVM.copyCharArray(value, index, value, index + len, count - index); } if(len != 0) { /* IAI - 17 */ CVM.copyCharArray(str, offset, value, index, len); } count = newCount; return this; } /** * Inserts the string representation of the <code>Object</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 obj an <code>Object</code>. * @return a reference to this <code>StringBuffer</code> object. * @exception StringIndexOutOfBoundsException if the offset is invalid. * @see java.lang.String#valueOf(java.lang.Object) * @see java.lang.StringBuffer#insert(int, java.lang.String) * @see java.lang.StringBuffer#length() */ public synchronized StringBuffer insert(int offset, Object obj) { return insert(offset, String.valueOf(obj)); } /** * Inserts the string into this string buffer. * <p> * The characters of the <code>String</code> argument are inserted, in * order, into this string buffer at the indicated offset, moving up any * characters originally above that position and increasing the length * of this string buffer by the length of the argument. If * <code>str</code> is <code>null</code>, then the four characters * <code>"null"</code> are inserted into this string buffer. * <p> * The character at index <i>k</i> in the new character sequence is * equal to: * <ul> * <li>the character at index <i>k</i> in the old character sequence, if * <i>k</i> is less than <code>offset</code> * <li>the character at index <i>k</i><code>-offset</code> in the * argument <code>str</code>, if <i>k</i> is not less than * <code>offset</code> but is less than <code>offset+str.length()</code> * <li>the character at index <i>k</i><code>-str.length()</code> in the * old character sequence, if <i>k</i> is not less than * <code>offset+str.length()</code> * </ul><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 str a string. * @return a reference to this <code>StringBuffer</code> object. * @exception StringIndexOutOfBoundsException if the offset is invalid. * @see java.lang.StringBuffer#length() */ public synchronized StringBuffer insert(int offset, String str) { if ((offset < 0) || (offset > count)) { throw new StringIndexOutOfBoundsException(); } if (str == null) { str = String.valueOf(str); } 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); } str.getChars(0, len, value, offset); count = newcount; return this; } /** * Inserts the string representation of the <code>char</code> array * argument into this string buffer. * <p> * The characters of the array argument are inserted into the * contents of this string buffer at the position indicated by * <code>offset</code>. The length of this string buffer increases by * the length of the argument. * <p> * The overall effect is exactly as if the argument were converted to * a string by the method {@link String#valueOf(char[])} and the * characters of that string were then * {@link #insert(int,String) inserted} into this * <code>StringBuffer</code> object at the position indicated by * <code>offset</code>. * * @param offset the offset. * @param str a character array. * @return a reference to this <code>StringBuffer</code> object. * @exception StringIndexOutOfBoundsException if the offset is invalid.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -