📄 appendingstringbuffer.java
字号:
* equal to the length of this string buffer. * * @param offset * the offset. * @param d * a <code>double</code>. * @return a reference to this <code>AppendingStringBuffer</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 AppendingStringBuffer 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 int indexOf(String str, int fromIndex) { return indexOf(value, 0, count, str.toCharArray(), 0, str.length(), fromIndex); } static int indexOf(char[] source, int sourceOffset, int sourceCount, char[] target, int targetOffset, int targetCount, int fromIndex) { if (fromIndex >= sourceCount) { return (targetCount == 0 ? sourceCount : -1); } if (fromIndex < 0) { fromIndex = 0; } if (targetCount == 0) { return fromIndex; } char first = target[targetOffset]; int i = sourceOffset + fromIndex; int max = sourceOffset + (sourceCount - targetCount); startSearchForFirstChar : while (true) { /* Look for first character. */ while (i <= max && source[i] != first) { i++; } if (i > max) { return -1; } /* Found first character, now look at the rest of v2 */ int j = i + 1; int end = j + targetCount - 1; int k = targetOffset + 1; while (j < end) { if (source[j++] != target[k++]) { i++; /* Look for str's first char again. */ continue startSearchForFirstChar; } } return i - sourceOffset; /* Found whole string. */ } } /** * 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 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 int lastIndexOf(String str, int fromIndex) { return lastIndexOf(value, 0, count, str.toCharArray(), 0, str.length(), fromIndex); } static int lastIndexOf(char[] source, int sourceOffset, int sourceCount, char[] target, int targetOffset, int targetCount, int fromIndex) { /* * Check arguments; return immediately where possible. For consistency, don't check for null * str. */ int rightIndex = sourceCount - targetCount; if (fromIndex < 0) { return -1; } if (fromIndex > rightIndex) { fromIndex = rightIndex; } /* Empty string always matches. */ if (targetCount == 0) { return fromIndex; } int strLastIndex = targetOffset + targetCount - 1; char strLastChar = target[strLastIndex]; int min = sourceOffset + targetCount - 1; int i = min + fromIndex; startSearchForLastChar : while (true) { while (i >= min && source[i] != strLastChar) { i--; } if (i < min) { return -1; } int j = i - 1; int start = j - (targetCount - 1); int k = strLastIndex - 1; while (j > start) { if (source[j--] != target[k--]) { i--; continue startSearchForLastChar; } } return start - sourceOffset + 1; } } /** * Tests if this AppendingStringBuffer starts with the specified prefix beginning a specified * index. * * @param prefix * the prefix. * @param toffset * where to begin looking in the string. * @return <code>true</code> if the character sequence represented by the argument is a prefix * of the substring of this object starting at index <code>toffset</code>; * <code>false</code> otherwise. The result is <code>false</code> if * <code>toffset</code> is negative or greater than the length of this * <code>String</code> object; otherwise the result is the same as the result of the * expression * * <pre> * this.subString(toffset).startsWith(prefix) * </pre> */ public boolean startsWith(CharSequence prefix, int toffset) { char ta[] = value; int to = toffset; int po = 0; int pc = prefix.length(); // Note: toffset might be near -1>>>1. if ((toffset < 0) || (toffset > count - pc)) { return false; } while (--pc >= 0) { if (ta[to++] != prefix.charAt(po++)) { return false; } } return true; } /** * Tests if this AppendingStringBuffer starts with the specified prefix. * * @param prefix * the prefix. * @return <code>true</code> if the character sequence represented by the argument is a prefix * of the character sequence represented by this AppendingStringBuffer; * <code>false</code> otherwise. Note also that <code>true</code> will be returned * if the argument is an empty string or is equal to this * <code>AppendingStringBuffer</code> object as determined by the * {@link #equals(Object)} method. * @since 1. 0 */ public boolean startsWith(CharSequence prefix) { return startsWith(prefix, 0); } /** * Tests if this AppendingStringBuffer ends with the specified suffix. * * @param suffix * the suffix. * @return <code>true</code> if the character sequence represented by the argument is a suffix * of the character sequence represented by this AppendingStringBuffer; * <code>false</code> otherwise. Note that the result will be <code>true</code> if * the argument is the empty string or is equal to this * <code>AppendingStringBuffer</code> object as determined by the * {@link #equals(Object)} method. */ public boolean endsWith(CharSequence suffix) { return startsWith(suffix, count - suffix.length()); } /** * Converts to a string representing the data in this AppendingStringBuffer. 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.value, 0, count); } /** * This method returns the internal char array. So it is not * * @return The internal char array */ public final char[] getValue() { return value; } /** * readObject is called to restore the state of the AppendingStringBuffer from a stream. * * @param s * @throws ClassNotFoundException * @throws IOException */ private void readObject(java.io.ObjectInputStream s) throws IOException, ClassNotFoundException { s.defaultReadObject(); value = (char[])value.clone(); } /** * Compares this AppendingStringBuffer to the specified object. The result is <code>true</code> * if and only if the argument is not <code>null</code> and is a * <code>AppendingStringBuffer</code> object or another charsequence object! that represents * the same sequence of characters as this object. * * @param anObject * the object to compare this <code>AppendingStringBuffer</code> against. * @return <code>true</code> if the <code>AppendingStringBuffer</code>are equal; * <code>false</code> otherwise. */ public boolean equals(Object anObject) { if (this == anObject) { return true; } if (anObject instanceof AppendingStringBuffer) { AppendingStringBuffer anotherString = (AppendingStringBuffer)anObject; int n = count; if (n == anotherString.count) { char v1[] = value; char v2[] = anotherString.value; int i = 0; while (n-- != 0) { if (v1[i] != v2[i++]) { return false; } } return true; } } else if (anObject instanceof CharSequence) { CharSequence sequence = (CharSequence)anObject; int n = count; if (sequence.length() == count) { char v1[] = value; int i = 0; while (n-- != 0) { if (v1[i] != sequence.charAt(i++)) { return false; } } return true; } } return false; } /** * Returns a hash code for this AppendingStringBuffer. The hash code for a * <code>AppendingStringBuffer</code> object is computed as <blockquote> * * <pre> * s[0]*31ˆ(n-1) + s[1]*31ˆ(n-2) + ... + s[n-1] * </pre> * * </blockquote> using <code>int</code> arithmetic, where <code>s[i]</code> is the <i>i</i>th * character of the AppendingStringBuffer, <code>n</code> is the length of the * AppendingStringBuffer, and <code>^</code> indicates exponentiation. (The hash value of the * empty AppendingStringBuffer is zero.) * * @return a hash code value for this object. */ public int hashCode() { int h = 0; if (h == 0) { int off = 0; char val[] = value; int len = count; for (int i = 0; i < len; i++) { h = 31 * h + val[off++]; } } return h; } /** * Clears the buffer contents, but leaves the allocated size intact */ public void clear() { count = 0; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -