📄 string.java
字号:
* @throws NullPointerException if the given StringBuffer is null * @since 1.4 */ public boolean contentEquals(StringBuffer buffer) { synchronized (buffer) { if (count != buffer.count) return false; if (value == buffer.value) return true; // Possible if shared. int i = count; int x = offset + count; while (--i >= 0) if (value[--x] != buffer.value[i]) return false; return true; } } /** * Compares the given CharSequence to this String. This is true if * the CharSequence has the same content as this String at this * moment. * * @param seq the CharSequence to compare to * @return true if CharSequence has the same character sequence * @throws NullPointerException if the given CharSequence is null * @since 1.5 */ public boolean contentEquals(CharSequence seq) { if (seq.length() != count) return false; for (int i = 0; i < count; ++i) if (value[offset + i] != seq.charAt(i)) return false; return true; } /** * Compares a String to this String, ignoring case. This does not handle * multi-character capitalization exceptions; instead the comparison is * made on a character-by-character basis, and is true if:<br><ul> * <li><code>c1 == c2</code></li> * <li><code>Character.toUpperCase(c1) * == Character.toUpperCase(c2)</code></li> * <li><code>Character.toLowerCase(c1) * == Character.toLowerCase(c2)</code></li> * </ul> * * @param anotherString String to compare to this String * @return true if anotherString is equal, ignoring case * @see #equals(Object) * @see Character#toUpperCase(char) * @see Character#toLowerCase(char) */ public boolean equalsIgnoreCase(String anotherString) { if (anotherString == null || count != anotherString.count) return false; int i = count; int x = offset; int y = anotherString.offset; while (--i >= 0) { char c1 = value[x++]; char c2 = anotherString.value[y++]; // Note that checking c1 != c2 is redundant, but avoids method calls. if (c1 != c2 && Character.toUpperCase(c1) != Character.toUpperCase(c2) && Character.toLowerCase(c1) != Character.toLowerCase(c2)) return false; } return true; } /** * Compares this String and another String (case sensitive, * lexicographically). The result is less than 0 if this string sorts * before the other, 0 if they are equal, and greater than 0 otherwise. * After any common starting sequence is skipped, the result is * <code>this.charAt(k) - anotherString.charAt(k)</code> if both strings * have characters remaining, or * <code>this.length() - anotherString.length()</code> if one string is * a subsequence of the other. * * @param anotherString the String to compare against * @return the comparison * @throws NullPointerException if anotherString is null */ public int compareTo(String anotherString) { int i = Math.min(count, anotherString.count); int x = offset; int y = anotherString.offset; while (--i >= 0) { int result = value[x++] - anotherString.value[y++]; if (result != 0) return result; } return count - anotherString.count; } /** * Behaves like <code>compareTo(java.lang.String)</code> unless the Object * is not a <code>String</code>. Then it throws a * <code>ClassCastException</code>. * * @param o the object to compare against * @return the comparison * @throws NullPointerException if o is null * @throws ClassCastException if o is not a <code>String</code> * @since 1.2 */ public int compareTo(Object o) { return compareTo((String) o); } /** * Compares this String and another String (case insensitive). This * comparison is <em>similar</em> to equalsIgnoreCase, in that it ignores * locale and multi-characater capitalization, and compares characters * after performing * <code>Character.toLowerCase(Character.toUpperCase(c))</code> on each * character of the string. This is unsatisfactory for locale-based * comparison, in which case you should use {@link java.text.Collator}. * * @param str the string to compare against * @return the comparison * @see Collator#compare(String, String) * @since 1.2 */ public int compareToIgnoreCase(String str) { int i = Math.min(count, str.count); int x = offset; int y = str.offset; while (--i >= 0) { int result = Character.toLowerCase(Character.toUpperCase(value[x++])) - Character.toLowerCase(Character.toUpperCase(str.value[y++])); if (result != 0) return result; } return count - str.count; } /** * Predicate which determines if this String matches another String * starting at a specified offset for each String and continuing * for a specified length. Indices out of bounds are harmless, and give * a false result. * * @param toffset index to start comparison at for this String * @param other String to compare region to this String * @param ooffset index to start comparison at for other * @param len number of characters to compare * @return true if regions match (case sensitive) * @throws NullPointerException if other is null */ public boolean regionMatches(int toffset, String other, int ooffset, int len) { return regionMatches(false, toffset, other, ooffset, len); } /** * Predicate which determines if this String matches another String * starting at a specified offset for each String and continuing * for a specified length, optionally ignoring case. Indices out of bounds * are harmless, and give a false result. Case comparisons are based on * <code>Character.toLowerCase()</code> and * <code>Character.toUpperCase()</code>, not on multi-character * capitalization expansions. * * @param ignoreCase true if case should be ignored in comparision * @param toffset index to start comparison at for this String * @param other String to compare region to this String * @param ooffset index to start comparison at for other * @param len number of characters to compare * @return true if regions match, false otherwise * @throws NullPointerException if other is null */ public boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len) { if (toffset < 0 || ooffset < 0 || toffset + len > count || ooffset + len > other.count) return false; toffset += offset; ooffset += other.offset; while (--len >= 0) { char c1 = value[toffset++]; char c2 = other.value[ooffset++]; // Note that checking c1 != c2 is redundant when ignoreCase is true, // but it avoids method calls. if (c1 != c2 && (! ignoreCase || (Character.toLowerCase(c1) != Character.toLowerCase(c2) && (Character.toUpperCase(c1) != Character.toUpperCase(c2))))) return false; } return true; } /** * Predicate which determines if this String contains the given prefix, * beginning comparison at toffset. The result is false if toffset is * negative or greater than this.length(), otherwise it is the same as * <code>this.substring(toffset).startsWith(prefix)</code>. * * @param prefix String to compare * @param toffset offset for this String where comparison starts * @return true if this String starts with prefix * @throws NullPointerException if prefix is null * @see #regionMatches(boolean, int, String, int, int) */ public boolean startsWith(String prefix, int toffset) { return regionMatches(false, toffset, prefix, 0, prefix.count); } /** * Predicate which determines if this String starts with a given prefix. * If the prefix is an empty String, true is returned. * * @param prefix String to compare * @return true if this String starts with the prefix * @throws NullPointerException if prefix is null * @see #startsWith(String, int) */ public boolean startsWith(String prefix) { return regionMatches(false, 0, prefix, 0, prefix.count); } /** * Predicate which determines if this String ends with a given suffix. * If the suffix is an empty String, true is returned. * * @param suffix String to compare * @return true if this String ends with the suffix * @throws NullPointerException if suffix is null * @see #regionMatches(boolean, int, String, int, int) */ public boolean endsWith(String suffix) { return regionMatches(false, count - suffix.count, suffix, 0, suffix.count); } /** * Computes the hashcode for this String. This is done with int arithmetic, * where ** represents exponentiation, by this formula:<br> * <code>s[0]*31**(n-1) + s[1]*31**(n-2) + ... + s[n-1]</code>. * * @return hashcode value of this String */ public int hashCode() { if (cachedHashCode != 0) return cachedHashCode; // Compute the hash code using a local variable to be reentrant. int hashCode = 0; int limit = count + offset; for (int i = offset; i < limit; i++) hashCode = hashCode * 31 + value[i]; return cachedHashCode = hashCode; } /** * Finds the first instance of a character in this String. * * @param ch character to find * @return location (base 0) of the character, or -1 if not found */ public int indexOf(int ch) { return indexOf(ch, 0); } /** * Finds the first instance of a character in this String, 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, -1 is returned. * * @param ch character to find * @param fromIndex index to start the search * @return location (base 0) of the character, or -1 if not found */ public int indexOf(int ch, int fromIndex) { if ((char) ch != ch) return -1; if (fromIndex < 0) fromIndex = 0; int i = fromIndex + offset; for ( ; fromIndex < count; fromIndex++) if (value[i++] == ch) return fromIndex; return -1; } /** * Finds the last instance of a character in this String. * * @param ch character to find * @return location (base 0) of the character, or -1 if not found */ public int lastIndexOf(int ch) { return lastIndexOf(ch, count - 1); } /** * Finds the last instance of a character in this String, 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, -1 is returned. * * @param ch character to find * @param fromIndex index to start the search * @return location (base 0) of the character, or -1 if not found */ public int lastIndexOf(int ch, int fromIndex) { if ((char) ch != ch) return -1; if (fromIndex >= count) fromIndex = count - 1; int i = fromIndex + offset; for ( ; fromIndex >= 0; fromIndex--) if (value[i--] == ch) return fromIndex; return -1; } /** * Finds the first instance of a String in this String. * * @param str String to find * @return location (base 0) of the String, or -1 if not found * @throws NullPointerException if str is null */ public int indexOf(String str) { return indexOf(str, 0); } /** * Finds the first instance of a String in this String, 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, -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, 0, str.count)) return fromIndex; return -1; } /** * Finds the last instance of a String in this String. * * @param str String to find * @return location (base 0) of the String, or -1 if not found * @throws NullPointerException if str is null */ public int lastIndexOf(String str)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -