📄 string.java
字号:
{ init (data, offset, count, "8859_1"); } catch (UnsupportedEncodingException x2) { // We know this can't happen. } } } /** * Creates a new String using the byte array. Uses the encoding of the * platform's default charset, so the resulting string may be longer or * shorter than the byte array. For more decoding control, use * {@link java.nio.charset.CharsetDecoder}. The behavior is not specified * if the decoder encounters invalid characters; this implementation throws * an Error. * * @param data byte array to copy * @throws NullPointerException if data is null * @throws Error if the decoding fails * @see #String(byte[], int, int) * @see #String(byte[], int, int, String) * @since 1.1 */ public String(byte[] data) { this(data, 0, data.length); } /** * Creates a new String using the character sequence represented by * the StringBuffer. Subsequent changes to buf do not affect the String. * * @param buffer StringBuffer to copy * @throws NullPointerException if buffer is null */ public String(StringBuffer buffer) { synchronized (buffer) { // Share unless buffer is 3/4 empty. boolean should_copy = ((buffer.count << 2) < buffer.value.length); if (! should_copy) buffer.shared = true; init (buffer.value, 0, buffer.count, ! should_copy); } } /** * Creates a new String using the character sequence represented by * the StringBuilder. Subsequent changes to buf do not affect the String. * * @param buffer StringBuilder to copy * @throws NullPointerException if buffer is null */ public String(StringBuilder buffer) { this(buffer.value, 0, buffer.count); } /** * Special constructor which can share an array when safe to do so. * * @param data the characters to copy * @param offset the location to start from * @param count the number of characters to use * @param dont_copy true if the array is trusted, and need not be copied * @throws NullPointerException if chars is null * @throws StringIndexOutOfBoundsException if bounds check fails */ String(char[] data, int offset, int count, boolean dont_copy) { init(data, offset, count, dont_copy); } // This is used by gnu.gcj.runtime.StringBuffer, so it must have // package-private protection. It is accessed via CNI and so avoids // ordinary protection mechanisms. String(gnu.gcj.runtime.StringBuffer buffer) { // No need to synchronize or mark the buffer, since we know it is // 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 < 0 || index >= length() * (while unspecified, this is a StringIndexOutOfBoundsException) */ public native char charAt(int index); /** * Get the code point at the specified index. This is like #charAt(int), * but if the character is the start of a surrogate pair, and the * following character completes the pair, then the corresponding * supplementary code point is returned. * @param index the index of the codepoint to get, starting at 0 * @return the codepoint at the specified index * @throws IndexOutOfBoundsException if index is negative or >= length() * @since 1.5 */ public synchronized int codePointAt(int index) { // Use the CharSequence overload as we get better range checking // this way. return Character.codePointAt(this, index); } /** * Get the code point before the specified index. This is like * #codePointAt(int), but checks the characters at <code>index-1</code> and * <code>index-2</code> to see if they form a supplementary code point. * @param index the index just past the codepoint to get, starting at 0 * @return the codepoint at the specified index * @throws IndexOutOfBoundsException if index is negative or >= length() * (while unspecified, this is a StringIndexOutOfBoundsException) * @since 1.5 */ public synchronized int codePointBefore(int index) { // Use the CharSequence overload as we get better range checking // this way. return Character.codePointBefore(this, 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 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 native boolean contentEquals(CharSequence seq); /** * 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);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -