string.java

来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 1,585 行 · 第 1/4 页

JAVA
1,585
字号
	 * @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) {
			offset = 0;
			count = buffer.count;
			// Share unless buffer is 3/4 empty.
			if ((count << 2) < buffer.value.length) {
				value = new char[count];
				System.arraycopy(buffer.value, 0, value, 0, count);
			} else {
				buffer.shared = true;
				value = buffer.value;
			}
		}
	}

	/**
	 * 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) {
		if (offset < 0 || count < 0 || offset + count > data.length)
			throw new StringIndexOutOfBoundsException();
		if (dont_copy) {
			value = data;
			this.offset = offset;
		} else {
			value = new char[count];
			System.arraycopy(data, offset, value, 0, count);
			this.offset = 0;
		}
		this.count = count;
	}

	/**
	 * 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 char charAt(int index) {
		if (index < 0 || index >= count)
			throw new StringIndexOutOfBoundsException(index);
		return value[offset + 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 void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {
		if (srcBegin < 0 || srcBegin > srcEnd || srcEnd > count) {
			throw new StringIndexOutOfBoundsException();
		}
		System.arraycopy(value, srcBegin + offset, dst, dstBegin, srcEnd - srcBegin);
	}

	/**
	 * 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 void getBytes(int srcBegin, int srcEnd, byte dst[], int dstBegin) {
		if (srcBegin < 0 || srcBegin > srcEnd || srcEnd > count)
			throw new StringIndexOutOfBoundsException();
		int i = srcEnd - srcBegin;
		srcBegin += offset;
		while (--i >= 0)
			dst[dstBegin++] = (byte) value[srcBegin++];
	}

	/**
	 * 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 byte[] getBytes(String enc) throws UnsupportedEncodingException {
		try {
			// XXX Consider using java.nio here.
			return EncodingManager.getEncoder(enc).convertToBytes(value, offset, count);
		} catch (CharConversionException e) {
			return null;
		}
	}

	/**
	 * 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 {
			// XXX Consider using java.nio here.
			return EncodingManager.getEncoder().convertToBytes(value, offset, count);
		} catch (CharConversionException e) {
			return null;
		}
	}

	/**
	 * 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 boolean equals(Object anObject) {
		if (anObject == null) {
			return false;
		}
		if (!(anObject instanceof String))
			return false;
		String str2 = (String) anObject;
		if (count != str2.count)
			return false;
		if (value == str2.value && offset == str2.offset)
			return true;
		int i = count;
		int x = offset;
		int y = str2.offset;
		while (--i >= 0)
			if (value[x++] != str2.value[y++])
				return false;
		return true;
	}

	/**
	 * 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 boolean contentEquals(StringBuffer buffer) {
		synchronized (buffer) {
			if (count != buffer.count)
				return false;
			if (value == buffer.value)
				return true; // Possible if shared.
			int i = count;
			int x = offset + count;
			while (--i >= 0)
				if (value[--x] != buffer.value[i])
					return false;
			return true;
		}
	}

	/**
	 * 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 boolean equalsIgnoreCase(String anotherString) {
		if (anotherString == null || count != anotherString.count)
			return false;
		int i = count;
		int x = offset;
		int y = anotherString.offset;
		while (--i >= 0) {
			char c1 = value[x++];
			char c2 = anotherString.value[y++];
			// Note that checking c1 != c2 is redundant, but avoids method calls.
			if (c1 != c2 && Character.toUpperCase(c1) != Character.toUpperCase(c2) && Character.toLowerCase(c1) != Character.toLowerCase(c2))
				return false;
		}
		return true;
	}

	/**
	 * 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 int compareTo(String anotherString) {
		int i = Math.min(count, anotherString.count);
		int x = offset;
		int y = anotherString.offset;
		while (--i >= 0) {
			int result = value[x++] - anotherString.value[y++];
			if (result != 0)
				return result;
		}
		return count - anotherString.count;
	}

	/**
	 * 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 s the string to compare against
	 * @return the comparison
	 * @see Collator#compare(String, String)
	 * @since 1.2
	 */
	public int compareToIgnoreCase(String s) {
		int i = Math.min(count, s.count);
		int x = offset;
		int y = s.offset;
		while (--i >= 0) {
			int result = Character.toLowerCase(Character.toUpperCase(value[x++])) - Character.toLowerCase(Character.toUpperCase(s.value[y++]));
			if (result != 0)
				return result;
		}
		return count - s.count;
	}

	/**
	 * 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 oofset 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 boolean regionMatches(int toffset, String other, int ooffset, int len) {
		return regionMatches(false, toffset, other, ooffset, 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 boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len) {
		if (toffset < 0 || ooffset < 0 || toffset + len > count || ooffset + len > other.count)
			return false;
		toffset += offset;
		ooffset += other.offset;
		while (--len >= 0) {
			char c1 = value[toffset++];
			char c2 = other.value[ooffset++];
			// Note that checking c1 != c2 is redundant when ignoreCase is true,
			// but it avoids method calls.
			if (c1 != c2
				&& (!ignoreCase || (Character.toLowerCase(c1) != Character.toLowerCase(c2) && (Character.toUpperCase(c1) != Character.toUpperCase(c2)))))
				return false;
		}
		return true;
	}

	/**
	 * 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 boolean startsWith(String prefix, int toffset) {
		return regionMatches(false, toffset, prefix, 0, prefix.count);
	}

	/**
	 * Predicate which determines if this String starts with a given prefix.
	 * If the prefix is an empty String, true is returned.
	 *
	 * @param prefex String to compare
	 * @return true if this String starts with the prefix
	 * @throws NullPointerException if prefix is null

⌨️ 快捷键说明

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