📄 string.java
字号:
{ return lastIndexOf(str, count - str.count); } /** * Finds the last instance of a String 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 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, 0, str.count)) return fromIndex; return -1; } /** * Creates a substring of this String, starting at a specified index * and ending at the end of this String. * * @param begin index to start substring (base 0) * @return new String which is a substring of this String * @throws IndexOutOfBoundsException if begin < 0 || begin > length() * (while unspecified, this is a StringIndexOutOfBoundsException) */ public String substring(int begin) { return substring(begin, count); } /** * Creates a substring of this String, starting at a specified index * and ending at one character before a specified index. * * @param beginIndex index to start substring (inclusive, base 0) * @param endIndex index to end at (exclusive) * @return new String which is a substring of this String * @throws IndexOutOfBoundsException if begin < 0 || end > length() * || begin > end (while unspecified, this is a * StringIndexOutOfBoundsException) */ public String substring(int beginIndex, int endIndex) { if (beginIndex < 0 || endIndex > count || beginIndex > endIndex) throw new StringIndexOutOfBoundsException(); if (beginIndex == 0 && endIndex == count) return this; int len = endIndex - beginIndex; // Package constructor avoids an array copy. return new String(value, beginIndex + offset, len, (len << 2) >= value.length); } /** * Creates a substring of this String, starting at a specified index * and ending at one character before a specified index. This behaves like * <code>substring(begin, end)</code>. * * @param begin index to start substring (inclusive, base 0) * @param end index to end at (exclusive) * @return new String which is a substring of this String * @throws IndexOutOfBoundsException if begin < 0 || end > length() * || begin > end * @since 1.4 */ public CharSequence subSequence(int begin, int end) { return substring(begin, end); } /** * Concatenates a String to this String. This results in a new string unless * one of the two originals is "". * * @param str String to append to this String * @return newly concatenated String * @throws NullPointerException if str is null */ public String concat(String str) { if (str.count == 0) return this; if (count == 0) return str; char[] newStr = new char[count + str.count]; VMSystem.arraycopy(value, offset, newStr, 0, count); VMSystem.arraycopy(str.value, str.offset, newStr, count, str.count); // Package constructor avoids an array copy. return new String(newStr, 0, newStr.length, true); } /** * Replaces every instance of a character in this String with a new * character. If no replacements occur, this is returned. * * @param oldChar the old character to replace * @param newChar the new character * @return new String with all instances of oldChar replaced with newChar */ public String replace(char oldChar, char newChar) { if (oldChar == newChar) return this; int i = count; int x = offset - 1; while (--i >= 0) if (value[++x] == oldChar) break; if (i < 0) return this; char[] newStr = (char[]) value.clone(); newStr[x] = newChar; while (--i >= 0) if (value[++x] == oldChar) newStr[x] = newChar; // Package constructor avoids an array copy. return new String(newStr, offset, count, true); } /** * Test if this String matches a regular expression. This is shorthand for * <code>{@link Pattern}.matches(regex, this)</code>. * * @param regex the pattern to match * @return true if the pattern matches * @throws NullPointerException if regex is null * @throws PatternSyntaxException if regex is invalid * @see Pattern#matches(String, CharSequence) * @since 1.4 */ public boolean matches(String regex) { return Pattern.matches(regex, this); } /** * Replaces the first substring match of the regular expression with a * given replacement. This is shorthand for <code>{@link Pattern} * .compile(regex).matcher(this).replaceFirst(replacement)</code>. * * @param regex the pattern to match * @param replacement the replacement string * @return the modified string * @throws NullPointerException if regex or replacement is null * @throws PatternSyntaxException if regex is invalid * @see #replaceAll(String, String) * @see Pattern#compile(String) * @see Pattern#matcher(CharSequence) * @see Matcher#replaceFirst(String) * @since 1.4 */ public String replaceFirst(String regex, String replacement) { return Pattern.compile(regex).matcher(this).replaceFirst(replacement); } /** * Replaces all matching substrings of the regular expression with a * given replacement. This is shorthand for <code>{@link Pattern} * .compile(regex).matcher(this).replaceAll(replacement)</code>. * * @param regex the pattern to match * @param replacement the replacement string * @return the modified string * @throws NullPointerException if regex or replacement is null * @throws PatternSyntaxException if regex is invalid * @see #replaceFirst(String, String) * @see Pattern#compile(String) * @see Pattern#matcher(CharSequence) * @see Matcher#replaceAll(String) * @since 1.4 */ public String replaceAll(String regex, String replacement) { return Pattern.compile(regex).matcher(this).replaceAll(replacement); } /** * Split this string around the matches of a regular expression. Each * element of the returned array is the largest block of characters not * terminated by the regular expression, in the order the matches are found. * * <p>The limit affects the length of the array. If it is positive, the * array will contain at most n elements (n - 1 pattern matches). If * negative, the array length is unlimited, but there can be trailing empty * entries. if 0, the array length is unlimited, and trailing empty entries * are discarded. * * <p>For example, splitting "boo:and:foo" yields:<br> * <table border=0> * <th><td>Regex</td> <td>Limit</td> <td>Result</td></th> * <tr><td>":"</td> <td>2</td> <td>{ "boo", "and:foo" }</td></tr> * <tr><td>":"</td> <td>t</td> <td>{ "boo", "and", "foo" }</td></tr> * <tr><td>":"</td> <td>-2</td> <td>{ "boo", "and", "foo" }</td></tr> * <tr><td>"o"</td> <td>5</td> <td>{ "b", "", ":and:f", "", "" }</td></tr> * <tr><td>"o"</td> <td>-2</td> <td>{ "b", "", ":and:f", "", "" }</td></tr> * <tr><td>"o"</td> <td>0</td> <td>{ "b", "", ":and:f" }</td></tr> * </table> * * <p>This is shorthand for * <code>{@link Pattern}.compile(regex).split(this, limit)</code>. * * @param regex the pattern to match * @param limit the limit threshold * @return the array of split strings * @throws NullPointerException if regex or replacement is null * @throws PatternSyntaxException if regex is invalid * @see Pattern#compile(String) * @see Pattern#split(CharSequence, int) * @since 1.4 */ public String[] split(String regex, int limit) { return Pattern.compile(regex).split(this, limit); } /** * Split this string around the matches of a regular expression. Each * element of the returned array is the largest block of characters not * terminated by the regular expression, in the order the matches are found. * The array length is unlimited, and trailing empty entries are discarded, * as though calling <code>split(regex, 0)</code>. * * @param regex the pattern to match * @return the array of split strings * @throws NullPointerException if regex or replacement is null * @throws PatternSyntaxException if regex is invalid * @see #split(String, int) * @see Pattern#compile(String) * @see Pattern#split(CharSequence, int) * @since 1.4 */ public String[] split(String regex) { return Pattern.compile(regex).split(this, 0); } /** * Lowercases this String according to a particular locale. This uses * Unicode's special case mappings, as applied to the given Locale, so the * resulting string may be a different length. * * @param loc locale to use * @return new lowercased String, or this if no characters were lowercased * @throws NullPointerException if loc is null * @see #toUpperCase(Locale) * @since 1.1 */ public String toLowerCase(Locale loc) { // First, see if the current string is already lower case. boolean turkish = "tr".equals(loc.getLanguage()); int i = count; int x = offset - 1; while (--i >= 0) { char ch = value[++x]; if ((turkish && ch == '\u0049') || ch != Character.toLowerCase(ch)) break; } if (i < 0) return this; // Now we perform the conversion. Fortunately, there are no multi-character // lowercase expansions in Unicode 3.0.0. char[] newStr = (char[]) value.clone(); do { char ch = value[x]; // Hardcoded special case. newStr[x++] = (turkish && ch == '\u0049') ? '\u0131' : Character.toLowerCase(ch); } while (--i >= 0); // Package constructor avoids an array copy. return new String(newStr, offset, count, true); } /** * Lowercases this String. This uses Unicode's special case mappings, as * applied to the platform's default Locale, so the resulting string may * be a different length. * * @return new lowercased String, or this if no characters were lowercased * @see #toLowerCase(Locale) * @see #toUpperCase() */ public String toLowerCase() { return toLowerCase(Locale.getDefault()); } /** * Uppercases this String according to a particular locale. This uses * Unicode's special case mappings, as applied to the given Locale, so the * resulting string may be a different length. * * @param loc locale to use * @return new uppercased String, or this if no characters were uppercased * @throws NullPointerException if loc is null * @see #toLowerCase(Locale) * @since 1.1 */ public String toUpperCase(Locale loc) { // First, see how many characters we have to grow by, as well as if the // current string is already upper case. boolean turkish = "tr".equals(loc.getLanguage()); int expand = 0; boolean unchanged = true; int i = count; int x = i + offset; while (--i >= 0) { char ch = value[--x]; expand += upperCaseExpansion(ch); unchanged = (unchanged && expand == 0 && ! (turkish && ch == '\u0069') && ch == Character.toUpperCase(ch)); } if (unchanged) return this; // Now we perform the conversion. i = count; if (expand == 0) { char[] newStr = (char[]) value.clone(); while (--i >= 0) { char ch = value[x]; // Hardcoded special case. newStr[x++] = (turkish && ch == '\u0069') ? '\u0130' : Character.toUpperCase(ch); } // Package constructor avoids an array copy. return new String(newStr, offset, count, true); } // Expansion is necessary. char[] newStr = new char[count + expand]; int j = 0; while (--i >= 0) { char ch = value[x++]; // Hardcoded special case. if (turkish && ch == '\u0069') { newStr[j++] = '\u0130'; continue; } expand = upperCaseExpansion(ch); if (expand > 0) { int index = upperCaseIndex(ch); while (expand-- >= 0) newStr[j++] = upperExpand[index++]; } else newStr[j++] = Character.toUpperCase(ch); } // Package constructor avoids an array copy. return new String(newStr, 0, newStr.length, true); } /** * Uppercases this String. This uses Unicode's special case mappings, as * applied to the platform's default Locale, so the resulting string may * be a different length. * * @return new uppercased String, or this if no characters were uppercased * @see #toUpperCase(Locale) * @see #toLowerCase() */ public String toUpperCase()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -