📄 appendingstringbuffer.java
字号:
* Then the character at index <i>k</i> in the new character sequence is equal to the character * at index <i>k</i> in the old character sequence, if <i>k</i> is less than <i>n</i>; * otherwise, it is equal to the character at index <i>k-n</i> in the argument <code>sb</code>. * <p> * The method <tt>ensureCapacity</tt> is first called on this <tt>AppendingStringBuffer</tt> * with the new buffer length as its argument. (This ensures that the storage of this * <tt>AppendingStringBuffer</tt> is adequate to contain the additional characters being * appended.) * * @param sb * the <tt>AppendingStringBuffer</tt> to append. * @return a reference to this <tt>AppendingStringBuffer</tt>. * @since 1.4 */ public AppendingStringBuffer append(StringBuffer sb) { if (sb == null) { sb = SB_NULL; } int len = sb.length(); int newcount = count + len; if (newcount > value.length) { expandCapacity(newcount); } sb.getChars(0, len, value, count); count = newcount; return this; } /** * Appends the specified <tt>AppendingStringBuffer</tt> to this <tt>AppendingStringBuffer</tt>. * <p> * The characters of the <tt>AppendingStringBuffer</tt> argument are appended, in order, to * the contents of this <tt>AppendingStringBuffer</tt>, increasing the length of this * <tt>AppendingStringBuffer</tt> by the length of the argument. If <tt>sb</tt> is * <tt>null</tt>, then the four characters <tt>"null"</tt> are appended to this * <tt>AppendingStringBuffer</tt>. * <p> * Let <i>n</i> be the length of the old character sequence, the one contained in the * <tt>AppendingStringBuffer</tt> just prior to execution of the <tt>append</tt> method. * Then the character at index <i>k</i> in the new character sequence is equal to the character * at index <i>k</i> in the old character sequence, if <i>k</i> is less than <i>n</i>; * otherwise, it is equal to the character at index <i>k-n</i> in the argument <code>sb</code>. * <p> * The method <tt>ensureCapacity</tt> is first called on this <tt>AppendingStringBuffer</tt> * with the new buffer length as its argument. (This ensures that the storage of this * <tt>AppendingStringBuffer</tt> is adequate to contain the additional characters being * appended.) * * @param sb * the <tt>AppendingStringBuffer</tt> to append. * @param from * The index where it must start from * @param length * The length that must be copied * @return a reference to this <tt>AppendingStringBuffer</tt>. */ public AppendingStringBuffer append(StringBuffer sb, int from, int length) { if (sb == null) { sb = SB_NULL; } int newcount = count + length; if (newcount > value.length) { expandCapacity(newcount); } sb.getChars(from, length, value, count); count = newcount; return this; } /** * Appends the string representation of the <code>char</code> array argument to this string * buffer. * <p> * The characters of the array argument are appended, in order, to the contents of this string * buffer. 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 #append(String) appended} to this <code>AppendingStringBuffer</code> object. * * @param str * the characters to be appended. * @return a reference to this <code>AppendingStringBuffer</code> object. */ public AppendingStringBuffer append(char str[]) { int len = str.length; int newcount = count + len; if (newcount > value.length) { expandCapacity(newcount); } System.arraycopy(str, 0, value, count, len); count = newcount; return this; } /** * Appends the string representation of a subarray of the <code>char</code> array argument to * this string buffer. * <p> * Characters of the character array <code>str</code>, starting at index <code>offset</code>, * are appended, in order, to the contents of this string buffer. The length of this string * buffer increases by the value of <code>len</code>. * <p> * The overall effect is exactly as if the arguments were converted to a string by the method * {@link String#valueOf(char[],int,int)} and the characters of that string were then * {@link #append(String) appended} to this <code>AppendingStringBuffer</code> object. * * @param str * the characters to be appended. * @param offset * the index of the first character to append. * @param len * the number of characters to append. * @return a reference to this <code>AppendingStringBuffer</code> object. */ public AppendingStringBuffer append(char str[], int offset, int len) { int newcount = count + len; if (newcount > value.length) { expandCapacity(newcount); } System.arraycopy(str, offset, value, count, len); count = newcount; return this; } /** * Appends the string representation of the <code>boolean</code> argument to the 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 b * a <code>boolean</code>. * @return a reference to this <code>AppendingStringBuffer</code>. * @see java.lang.String#valueOf(boolean) * @see java.lang.StringBuffer#append(java.lang.String) */ public AppendingStringBuffer append(boolean b) { if (b) { int newcount = count + 4; if (newcount > value.length) { expandCapacity(newcount); } value[count++] = 't'; value[count++] = 'r'; value[count++] = 'u'; value[count++] = 'e'; } else { int newcount = count + 5; if (newcount > value.length) { expandCapacity(newcount); } value[count++] = 'f'; value[count++] = 'a'; value[count++] = 'l'; value[count++] = 's'; value[count++] = 'e'; } return this; } /** * Appends the string representation of the <code>char</code> argument to this string buffer. * <p> * The argument is appended to the contents of this string buffer. The length of this string * buffer increases by <code>1</code>. * <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 #append(String) appended} to this <code>AppendingStringBuffer</code> object. * * @param c * a <code>char</code>. * @return a reference to this <code>AppendingStringBuffer</code> object. */ public AppendingStringBuffer append(char c) { int newcount = count + 1; if (newcount > value.length) { expandCapacity(newcount); } value[count++] = c; return this; } /** * Appends the string representation of the <code>int</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 i * an <code>int</code>. * @return a reference to this <code>AppendingStringBuffer</code> object. * @see java.lang.String#valueOf(int) * @see java.lang.StringBuffer#append(java.lang.String) */ public AppendingStringBuffer append(int i) { return append(String.valueOf(i)); } /** * Appends the string representation of the <code>long</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 l * a <code>long</code>. * @return a reference to this <code>AppendingStringBuffer</code> object. * @see java.lang.String#valueOf(long) * @see java.lang.StringBuffer#append(java.lang.String) */ public AppendingStringBuffer append(long l) { return append(String.valueOf(l)); } /** * Appends the string representation of the <code>float</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 f * a <code>float</code>. * @return a reference to this <code>AppendingStringBuffer</code> object. * @see java.lang.String#valueOf(float) * @see java.lang.StringBuffer#append(java.lang.String) */ public AppendingStringBuffer append(float f) { return append(String.valueOf(f)); } /** * 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>AppendingStringBuffer</code> object. * @see java.lang.String#valueOf(double) * @see java.lang.StringBuffer#append(java.lang.String) */ public AppendingStringBuffer append(double d) { return append(String.valueOf(d)); } /** * Removes the characters in a substring of this <code>AppendingStringBuffer</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>AppendingStringBuffer</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 AppendingStringBuffer 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) { System.arraycopy(value, start + len, value, start, count - end); count -= len; } return this; } /** * Removes the character at the specified position in this <code>AppendingStringBuffer</code> * (shortening the <code>AppendingStringBuffer</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 AppendingStringBuffer deleteCharAt(int index) { if ((index < 0) || (index >= count)) { throw new StringIndexOutOfBoundsException(); } System.arraycopy(value, index + 1, value, index, count - index - 1); count--; return this; } /** * Replaces the characters in a substring of this <code>AppendingStringBuffer</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>AppendingStringBuffer</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>AppendingStringBuffer</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 AppendingStringBuffer 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); } System.arraycopy(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>AppendingStringBuffer</code>.The substring begins at the specified * index and extends to the end of the <code>AppendingStringBuffer</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>AppendingStringBuffer</code>. * @since 1.2 */ public 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>AppendingStringBuffer</tt> class can implement the * {@link CharSequence} interface. * </p> * * @param start * the start index, inclusive.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -