📄 stringbuilder.java
字号:
* @throws StringIndexOutOfBoundsException if any index is out of bounds */ public StringBuilder insert(int offset, char[] str, int str_offset, int len) { if (offset < 0 || offset > count || len < 0 || str_offset < 0 || str_offset > str.length - len) throw new StringIndexOutOfBoundsException(); ensureCapacity(count + len); System.arraycopy(value, offset, value, offset + len, count - offset); System.arraycopy(str, str_offset, value, offset, len); count += len; return this; } /** * Insert the <code>String</code> value of the argument into this * <code>StringBuilder</code>. Uses <code>String.valueOf()</code> to convert * to <code>String</code>. * * @param offset the place to insert in this buffer * @param obj the <code>Object</code> to convert and insert * @return this <code>StringBuilder</code> * @exception StringIndexOutOfBoundsException if offset is out of bounds * @see String#valueOf(Object) */ public StringBuilder insert(int offset, Object obj) { return insert(offset, obj == null ? "null" : obj.toString()); } /** * Insert the <code>String</code> argument into this * <code>StringBuilder</code>. If str is null, the String "null" is used * instead. * * @param offset the place to insert in this buffer * @param str the <code>String</code> to insert * @return this <code>StringBuilder</code> * @throws StringIndexOutOfBoundsException if offset is out of bounds */ public StringBuilder insert(int offset, String str) { if (offset < 0 || offset > count) throw new StringIndexOutOfBoundsException(offset); if (str == null) str = "null"; int len = str.count; ensureCapacity(count + len); System.arraycopy(value, offset, value, offset + len, count - offset); str.getChars(0, len, value, offset); count += len; return this; } /** * Insert the <code>CharSequence</code> argument into this * <code>StringBuilder</code>. If the sequence is null, the String * "null" is used instead. * * @param offset the place to insert in this buffer * @param sequence the <code>CharSequence</code> to insert * @return this <code>StringBuilder</code> * @throws IndexOutOfBoundsException if offset is out of bounds */ public synchronized StringBuilder insert(int offset, CharSequence sequence) { if (sequence == null) sequence = "null"; return insert(offset, sequence, 0, sequence.length()); } /** * Insert a subsequence of the <code>CharSequence</code> argument into this * <code>StringBuilder</code>. If the sequence is null, the String * "null" is used instead. * * @param offset the place to insert in this buffer * @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>StringBuilder</code> * @throws IndexOutOfBoundsException if offset, start, * or end are out of bounds */ public synchronized StringBuilder 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(count + len); System.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>StringBuilder</code>. * * @param offset the place to insert in this buffer * @param data the <code>char[]</code> to insert * @return this <code>StringBuilder</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 StringBuilder insert(int offset, char[] data) { return insert(offset, data, 0, data.length); } /** * Insert the <code>String</code> value of the argument into this * <code>StringBuilder</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>StringBuilder</code> * @throws StringIndexOutOfBoundsException if offset is out of bounds * @see String#valueOf(boolean) */ public StringBuilder insert(int offset, boolean bool) { return insert(offset, bool ? "true" : "false"); } /** * Insert the <code>char</code> argument into this <code>StringBuilder</code>. * * @param offset the place to insert in this buffer * @param ch the <code>char</code> to insert * @return this <code>StringBuilder</code> * @throws StringIndexOutOfBoundsException if offset is out of bounds */ public StringBuilder insert(int offset, char ch) { if (offset < 0 || offset > count) throw new StringIndexOutOfBoundsException(offset); ensureCapacity(count + 1); System.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>StringBuilder</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>StringBuilder</code> * @throws StringIndexOutOfBoundsException if offset is out of bounds * @see String#valueOf(int) */ public StringBuilder insert(int offset, int inum) { return insert(offset, String.valueOf(inum)); } /** * Insert the <code>String</code> value of the argument into this * <code>StringBuilder</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>StringBuilder</code> * @throws StringIndexOutOfBoundsException if offset is out of bounds * @see String#valueOf(long) */ public StringBuilder insert(int offset, long lnum) { return insert(offset, Long.toString(lnum, 10)); } /** * Insert the <code>String</code> value of the argument into this * <code>StringBuilder</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>StringBuilder</code> * @throws StringIndexOutOfBoundsException if offset is out of bounds * @see String#valueOf(float) */ public StringBuilder insert(int offset, float fnum) { return insert(offset, Float.toString(fnum)); } /** * Insert the <code>String</code> value of the argument into this * <code>StringBuilder</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>StringBuilder</code> * @throws StringIndexOutOfBoundsException if offset is out of bounds * @see String#valueOf(double) */ public StringBuilder insert(int offset, double dnum) { return insert(offset, Double.toString(dnum)); } /** * Finds the first instance of a substring in this StringBuilder. * * @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) */ public int indexOf(String str) { return indexOf(str, 0); } /** * Finds the first instance of a String in this StringBuilder, 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 */ public 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 StringBuilder. * * @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) */ public int lastIndexOf(String str) { return lastIndexOf(str, count - str.count); } /** * Finds the last instance of a String in this StringBuilder, 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 */ public 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 StringBuilder. The same sequence of * characters exists, but in the reverse index ordering. * * @return this <code>StringBuilder</code> */ public StringBuilder reverse() { // Call ensureCapacity to enforce copy-on-write. ensureCapacity(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>StringBuilder</code> to a <code>String</code>. The * String is composed of the characters currently in this StringBuilder. Note * that the result is a copy, and that future modifications to this buffer * do not affect the String. * * @return the characters in this StringBuilder */ public String toString() { return new String(this); } /** * 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) */ // GCJ LOCAL: Native to access String internals properly. private native boolean regionMatches(int toffset, String other);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -