📄 string.java
字号:
/** * 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 oofset 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 native boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len); /** * 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 native boolean startsWith(String prefix, int toffset); /** * 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 startsWith (prefix, 0); } /** * 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 (this.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 native int 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 native int indexOf(int ch, int fromIndex); /** * 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 native int lastIndexOf(int ch, int fromIndex); /** * 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 native int indexOf(String str, int fromIndex); /** * 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) { 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) { if (fromIndex >= count) fromIndex = count - str.count; for (;; --fromIndex) { if (fromIndex < 0) return -1; if (startsWith(str, fromIndex)) return fromIndex; } } /** * 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 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 (while unspecified, this is a * StringIndexOutOfBoundsException) */ public native String substring(int begin, int end); /** * 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 native String concat(String str); /** * 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 native String replace(char oldChar, char newChar); /** * 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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -