string.java

来自「Mac OS X 10.4.9 for x86 Source Code gcc」· Java 代码 · 共 1,258 行 · 第 1/3 页

JAVA
1,258
字号
    // only used once.    init (buffer);  }  /**   * Returns the number of characters contained in this String.   *   * @return the length of this String   */  public int length()  {    return count;  }  /**   * Returns the character located at the specified index within this String.   *   * @param index position of character to return (base 0)   * @return character located at position index   * @throws IndexOutOfBoundsException if index &lt; 0 || index &gt;= length()   *         (while unspecified, this is a StringIndexOutOfBoundsException)   */  public native char charAt(int index);  /**   * Copies characters from this String starting at a specified start index,   * ending at a specified stop index, to a character array starting at   * a specified destination begin index.   *   * @param srcBegin index to begin copying characters from this String   * @param srcEnd index after the last character to be copied from this String   * @param dst character array which this String is copied into   * @param dstBegin index to start writing characters into dst   * @throws NullPointerException if dst is null   * @throws IndexOutOfBoundsException if any indices are out of bounds   *         (while unspecified, source problems cause a   *         StringIndexOutOfBoundsException, and dst problems cause an   *         ArrayIndexOutOfBoundsException)   */  public native void getChars(int srcBegin, int srcEnd,			      char[] dst, int dstBegin);  /**   * Copies the low byte of each character from this String starting at a   * specified start index, ending at a specified stop index, to a byte array   * starting at a specified destination begin index.   *   * @param srcBegin index to being copying characters from this String   * @param srcEnd index after the last character to be copied from this String   * @param dst byte array which each low byte of this String is copied into   * @param dstBegin index to start writing characters into dst   * @throws NullPointerException if dst is null and copy length is non-zero   * @throws IndexOutOfBoundsException if any indices are out of bounds   *         (while unspecified, source problems cause a   *         StringIndexOutOfBoundsException, and dst problems cause an   *         ArrayIndexOutOfBoundsException)   * @see #getBytes()   * @see #getBytes(String)   * @deprecated use {@link #getBytes()}, which uses a char to byte encoder   */  public native void getBytes(int srcBegin, int srcEnd,			      byte[] dst, int dstBegin);  /**   * Converts the Unicode characters in this String to a byte array. Uses the   * specified encoding method, so the result may be longer or shorter than   * the String. For more encoding control, use   * {@link java.nio.charset.CharsetEncoder}, and for valid character sets,   * see {@link java.nio.charset.Charset}. The behavior is not specified if   * the encoder encounters a problem; this implementation returns null.   *   * @param enc encoding name   * @return the resulting byte array, or null on a problem   * @throws NullPointerException if enc is null   * @throws UnsupportedEncodingException if encoding is not supported   * @since 1.1   */  public native byte[] getBytes(String enc)    throws UnsupportedEncodingException;  /**   * Converts the Unicode characters in this String to a byte array. Uses the   * encoding of the platform's default charset, so the result may be longer   * or shorter than the String. For more encoding control, use   * {@link java.nio.charset.CharsetEncoder}.  The behavior is not specified if   * the encoder encounters a problem; this implementation returns null.   *   * @return the resulting byte array, or null on a problem   * @since 1.1   */  public byte[] getBytes()  {    try      {	return getBytes (System.getProperty("file.encoding", "8859_1"));      }    catch (UnsupportedEncodingException x)      {	// This probably shouldn't happen, but could if file.encoding	// is somehow changed to a value we don't understand.	try	  {	    return getBytes ("8859_1");	  }	catch (UnsupportedEncodingException x2)	  {	    // This really shouldn't happen, because the 8859_1	    // encoding should always be available.	    throw new InternalError ("couldn't find 8859_1 encoder");	  }      }  }  /**   * Predicate which compares anObject to this. This is true only for Strings   * with the same character sequence.   *   * @param anObject the object to compare   * @return true if anObject is semantically equal to this   * @see #compareTo(String)   * @see #equalsIgnoreCase(String)   */  public native boolean equals(Object anObject);  /**   * Compares the given StringBuffer to this String. This is true if the   * StringBuffer has the same content as this String at this moment.   *   * @param buffer the StringBuffer to compare to   * @return true if StringBuffer has the same character sequence   * @throws NullPointerException if the given StringBuffer is null   * @since 1.4   */  public native boolean contentEquals(StringBuffer buffer);  /**   * 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 native boolean equalsIgnoreCase(String anotherString);  /**   * 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 native int compareTo(String anotherString);  /**   * 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)  {    return this.toUpperCase().toLowerCase().compareTo(     str.toUpperCase().toLowerCase());  }    /**   * 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 native boolean regionMatches(int toffset,				      String other, int ooffset, int 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 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 &lt; 0 || begin &gt; 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)

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?