string.java
来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 1,585 行 · 第 1/4 页
JAVA
1,585 行
* @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) {
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 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 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(beginIndex, endIndex)</code>.
*
* @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
* @since 1.4
*/
public CharSequence subSequence(int beginIndex, int endIndex) {
return substring(beginIndex, endIndex);
}
/**
* 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];
System.arraycopy(value, offset, newStr, 0, count);
System.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;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?