📄 stringbuffer.java
字号:
* @param sequence the <code>CharSequence</code> to insert * @param start the starting index of the subsequence * @param end one past the ending index of the subsequence * @return this <code>StringBuffer</code> * @throws IndexOutOfBoundsException if offset, start, * or end are out of bounds * @since 1.5 */ public synchronized StringBuffer insert(int offset, CharSequence sequence, int start, int end) { if (sequence == null) sequence = "null"; if (start < 0 || end < 0 || start > end || end > sequence.length()) throw new IndexOutOfBoundsException(); int len = end - start; ensureCapacity_unsynchronized(count + len); VMSystem.arraycopy(value, offset, value, offset + len, count - offset); for (int i = start; i < end; ++i) value[offset++] = sequence.charAt(i); count += len; return this; } /** * Insert the <code>char[]</code> argument into this * <code>StringBuffer</code>. * * @param offset the place to insert in this buffer * @param data the <code>char[]</code> to insert * @return this <code>StringBuffer</code> * @throws NullPointerException if <code>data</code> is <code>null</code> * @throws StringIndexOutOfBoundsException if offset is out of bounds * @see #insert(int, char[], int, int) */ public StringBuffer insert(int offset, char[] data) { return insert(offset, data, 0, data.length); } /** * 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 in this buffer * @param bool the <code>boolean</code> to convert and insert * @return this <code>StringBuffer</code> * @throws StringIndexOutOfBoundsException if offset is out of bounds * @see String#valueOf(boolean) */ public StringBuffer insert(int offset, boolean bool) { return insert(offset, bool ? "true" : "false"); } /** * Insert the <code>char</code> argument into this <code>StringBuffer</code>. * * @param offset the place to insert in this buffer * @param ch the <code>char</code> to insert * @return this <code>StringBuffer</code> * @throws StringIndexOutOfBoundsException if offset is out of bounds */ public synchronized StringBuffer insert(int offset, char ch) { if (offset < 0 || offset > count) throw new StringIndexOutOfBoundsException(offset); ensureCapacity_unsynchronized(count + 1); VMSystem.arraycopy(value, offset, value, offset + 1, count - offset); value[offset] = ch; count++; return this; } /** * 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 in this buffer * @param inum the <code>int</code> to convert and insert * @return this <code>StringBuffer</code> * @throws StringIndexOutOfBoundsException if offset is out of bounds * @see 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 in this buffer * @param lnum the <code>long</code> to convert and insert * @return this <code>StringBuffer</code> * @throws StringIndexOutOfBoundsException if offset is out of bounds * @see String#valueOf(long) */ public StringBuffer insert(int offset, long lnum) { return insert(offset, Long.toString(lnum, 10)); } /** * 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 in this buffer * @param fnum the <code>float</code> to convert and insert * @return this <code>StringBuffer</code> * @throws StringIndexOutOfBoundsException if offset is out of bounds * @see String#valueOf(float) */ public StringBuffer insert(int offset, float fnum) { return insert(offset, Float.toString(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 in this buffer * @param dnum the <code>double</code> to convert and insert * @return this <code>StringBuffer</code> * @throws StringIndexOutOfBoundsException if offset is out of bounds * @see String#valueOf(double) */ public StringBuffer insert(int offset, double dnum) { return insert(offset, Double.toString(dnum)); } /** * Finds the first instance of a substring in this StringBuffer. * * @param str String to find * @return location (base 0) of the String, or -1 if not found * @throws NullPointerException if str is null * @see #indexOf(String, int) * @since 1.4 */ public int indexOf(String str) { return indexOf(str, 0); } /** * Finds the first instance of a String in this StringBuffer, starting at * a given index. If starting index is less than 0, the search starts at * the beginning of this String. If the starting index is greater than the * length of this String, or the substring is not found, -1 is returned. * * @param str String to find * @param fromIndex index to start the search * @return location (base 0) of the String, or -1 if not found * @throws NullPointerException if str is null * @since 1.4 */ public synchronized int indexOf(String str, int fromIndex) { if (fromIndex < 0) fromIndex = 0; int limit = count - str.count; for ( ; fromIndex <= limit; fromIndex++) if (regionMatches(fromIndex, str)) return fromIndex; return -1; } /** * Finds the last instance of a substring in this StringBuffer. * * @param str String to find * @return location (base 0) of the String, or -1 if not found * @throws NullPointerException if str is null * @see #lastIndexOf(String, int) * @since 1.4 */ public int lastIndexOf(String str) { return lastIndexOf(str, count - str.count); } /** * Finds the last instance of a String in this StringBuffer, starting at a * given index. If starting index is greater than the maximum valid index, * then the search begins at the end of this String. If the starting index * is less than zero, or the substring is not found, -1 is returned. * * @param str String to find * @param fromIndex index to start the search * @return location (base 0) of the String, or -1 if not found * @throws NullPointerException if str is null * @since 1.4 */ public synchronized int lastIndexOf(String str, int fromIndex) { fromIndex = Math.min(fromIndex, count - str.count); for ( ; fromIndex >= 0; fromIndex--) if (regionMatches(fromIndex, str)) return fromIndex; return -1; } /** * Reverse the characters in this StringBuffer. The same sequence of * characters exists, but in the reverse index ordering. * * @return this <code>StringBuffer</code> */ public synchronized StringBuffer reverse() { // Call ensureCapacity to enforce copy-on-write. ensureCapacity_unsynchronized(count); for (int i = count >> 1, j = count - i; --i >= 0; ++j) { char c = value[i]; value[i] = value[j]; value[j] = c; } return this; } /** * Convert this <code>StringBuffer</code> to a <code>String</code>. The * String is composed of the characters currently in this StringBuffer. Note * that the result is a copy, and that future modifications to this buffer * do not affect the String. * * @return the characters in this StringBuffer */ public String toString() { // The string will set this.shared = true. return new String(this); } /** * This may reduce the amount of memory used by the StringBuffer, * by resizing the internal array to remove unused space. However, * this method is not required to resize, so this behavior cannot * be relied upon. * @since 1.5 */ public synchronized void trimToSize() { int wouldSave = value.length - count; // Some random heuristics: if we save less than 20 characters, who // cares. if (wouldSave < 20) return; // If we save more than 200 characters, shrink. // If we save more than 1/4 of the buffer, shrink. if (wouldSave > 200 || wouldSave * 4 > value.length) { char[] newValue = new char[count]; VMSystem.arraycopy(value, 0, newValue, 0, count); value = newValue; } } /** * Return the number of code points between two indices in the * <code>StringBuffer</code>. An unpaired surrogate counts as a * code point for this purpose. Characters outside the indicated * range are not examined, even if the range ends in the middle of a * surrogate pair. * * @param start the starting index * @param end one past the ending index * @return the number of code points * @since 1.5 */ public synchronized int codePointCount(int start, int end) { if (start < 0 || end >= count || start > end) throw new StringIndexOutOfBoundsException(); int count = 0; while (start < end) { char base = value[start]; if (base < Character.MIN_HIGH_SURROGATE || base > Character.MAX_HIGH_SURROGATE || start == end || start == count || value[start + 1] < Character.MIN_LOW_SURROGATE || value[start + 1] > Character.MAX_LOW_SURROGATE) { // Nothing. } else { // Surrogate pair. ++start; } ++start; ++count; } return count; } /** * Starting at the given index, this counts forward by the indicated * number of code points, and then returns the resulting index. An * unpaired surrogate counts as a single code point for this * purpose. * * @param start the starting index * @param codePoints the number of code points * @return the resulting index * @since 1.5 */ public synchronized int offsetByCodePoints(int start, int codePoints) { while (codePoints > 0) { char base = value[start]; if (base < Character.MIN_HIGH_SURROGATE || base > Character.MAX_HIGH_SURROGATE || start == count || value[start + 1] < Character.MIN_LOW_SURROGATE || value[start + 1] > Character.MAX_LOW_SURROGATE) { // Nothing. } else { // Surrogate pair. ++start; } ++start; --codePoints; } return start; } /** * An unsynchronized version of ensureCapacity, used internally to avoid * the cost of a second lock on the same object. This also has the side * effect of duplicating the array, if it was shared (to form copy-on-write * semantics). * * @param minimumCapacity the minimum capacity * @see #ensureCapacity(int) */ private void ensureCapacity_unsynchronized(int minimumCapacity) { if (shared || minimumCapacity > value.length) { // We don't want to make a larger vector when `shared' is // set. If we do, then setLength becomes very inefficient // when repeatedly reusing a StringBuffer in a loop. int max = (minimumCapacity > value.length ? value.length * 2 + 2 : value.length); minimumCapacity = (minimumCapacity < max ? max : minimumCapacity); char[] nb = new char[minimumCapacity]; VMSystem.arraycopy(value, 0, nb, 0, count); value = nb; shared = false; } } /** * Predicate which determines if a substring of this matches another String * starting at a specified offset for each String and continuing for a * specified length. This is more efficient than creating a String to call * indexOf on. * * @param toffset index to start comparison at for this String * @param other non-null String to compare to region of this * @return true if regions match, false otherwise * @see #indexOf(String, int) * @see #lastIndexOf(String, int) * @see String#regionMatches(boolean, int, String, int, int) */ private boolean regionMatches(int toffset, String other) { int len = other.count; int index = other.offset; while (--len >= 0) if (value[toffset++] != other.value[index++]) return false; return true; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -