📄 string.java
字号:
/* String.java -- immutable character sequences; the object of string literals Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package java.lang;import java.io.Serializable;import java.io.UnsupportedEncodingException;import java.lang.Comparable;import java.util.Comparator;import java.util.Locale;import java.util.regex.Pattern;import java.util.regex.PatternSyntaxException;/** * Strings represent an immutable set of characters. All String literals * are instances of this class, and two string literals with the same contents * refer to the same String object. * * <p>This class also includes a number of methods for manipulating the * contents of strings (of course, creating a new object if there are any * changes, as String is immutable). Case mapping relies on Unicode 3.0.0 * standards, where some character sequences have a different number of * characters in the uppercase version than the lower case. * * <p>Strings are special, in that they are the only object with an overloaded * operator. When you use '+' with at least one String argument, both * arguments have String conversion performed on them, and another String (not * guaranteed to be unique) results. * * <p>String is special-cased when doing data serialization - rather than * listing the fields of this class, a String object is converted to a string * literal in the object stream. * * @author Paul N. Fisher * @author Eric Blake (ebb9@email.byu.edu) * @author Per Bothner (bothner@cygnus.com) * @since 1.0 * @status updated to 1.4 */public final class String implements Serializable, Comparable, CharSequence{ // WARNING: String is a CORE class in the bootstrap cycle. See the comments // in vm/reference/java/lang/Runtime for implications of this fact. /** * This is probably not necessary because this class is special cased already * but it will avoid showing up as a discrepancy when comparing SUIDs. */ private static final long serialVersionUID = -6849794470754667710L; /** * This is the object that holds the characters that make up the * String. It might be a char[], or it could be String. It could * even be `this'. The actual characters can't be located using * pure Java code. * @see #boffset */ private Object data; /** * This is a <emph>byte</emph> offset of the actual characters from * the start of the character-holding object. Don't use this field * in Java code. */ private int boffset; /** * Holds the number of characters in value. Package visible for use * by trusted code. */ int count; /** * Caches the result of hashCode(). If this value is zero, the hashcode * is considered uncached (even if 0 is the correct hash value). */ private int cachedHashCode; /** * An implementation for {@link CASE_INSENSITIVE_ORDER}. * This must be {@link Serializable}. The class name is dictated by * compatibility with Sun's JDK. */ private static final class CaseInsensitiveComparator implements Comparator, Serializable { /** * Compatible with JDK 1.2. */ private static final long serialVersionUID = 8575799808933029326L; /** * The default private constructor generates unnecessary overhead. */ CaseInsensitiveComparator() {} /** * Compares to Strings, using * <code>String.compareToIgnoreCase(String)</code>. * * @param o1 the first string * @param o2 the second string * @return < 0, 0, or > 0 depending on the case-insensitive * comparison of the two strings. * @throws NullPointerException if either argument is null * @throws ClassCastException if either argument is not a String * @see #compareToIgnoreCase(String) */ public int compare(Object o1, Object o2) { return ((String) o1).compareToIgnoreCase((String) o2); } } // class CaseInsensitiveComparator /** * A Comparator that uses <code>String.compareToIgnoreCase(String)</code>. * This comparator is {@link Serializable}. Note that it ignores Locale, * for that, you want a Collator. * * @see Collator#compare(String, String) * @since 1.2 */ public static final Comparator CASE_INSENSITIVE_ORDER = new CaseInsensitiveComparator(); /** * Creates an empty String (length 0). Unless you really need a new object, * consider using <code>""</code> instead. */ public String() { data = "".data; boffset = 0; count = 0; } /** * Copies the contents of a String to a new String. Since Strings are * immutable, only a shallow copy is performed. * * @param str String to copy * @throws NullPointerException if value is null */ public String(String str) { data = str.data; boffset = str.boffset; count = str.count; cachedHashCode = str.cachedHashCode; } /** * Creates a new String using the character sequence of the char array. * Subsequent changes to data do not affect the String. * * @param data char array to copy * @throws NullPointerException if data is null */ public String(char[] data) { init(data, 0, data.length, false); } /** * Creates a new String using the character sequence of a subarray of * characters. The string starts at offset, and copies count chars. * Subsequent changes to data do not affect the String. * * @param data char array to copy * @param offset position (base 0) to start copying out of data * @param count the number of characters from data to copy * @throws NullPointerException if data is null * @throws IndexOutOfBoundsException if (offset < 0 || count < 0 * || offset + count > data.length) * (while unspecified, this is a StringIndexOutOfBoundsException) */ public String(char[] data, int offset, int count) { init(data, offset, count, false); } /** * Creates a new String using an 8-bit array of integer values, starting at * an offset, and copying up to the count. Each character c, using * corresponding byte b, is created in the new String as if by performing: * * <pre> * c = (char) (((hibyte & 0xff) << 8) | (b & 0xff)) * </pre> * * @param ascii array of integer values * @param hibyte top byte of each Unicode character * @param offset position (base 0) to start copying out of ascii * @param count the number of characters from ascii to copy * @throws NullPointerException if ascii is null * @throws IndexOutOfBoundsException if (offset < 0 || count < 0 * || offset + count > ascii.length) * (while unspecified, this is a StringIndexOutOfBoundsException) * @see #String(byte[]) * @see #String(byte[], String) * @see #String(byte[], int, int) * @see #String(byte[], int, int, String) * @deprecated use {@link #String(byte[], int, int, String)} to perform * correct encoding */ public String(byte[] ascii, int hibyte, int offset, int count) { init(ascii, hibyte, offset, count); } /** * Creates a new String using an 8-bit array of integer values. Each * character c, using corresponding byte b, is created in the new String * as if by performing: * * <pre> * c = (char) (((hibyte & 0xff) << 8) | (b & 0xff)) * </pre> * * @param ascii array of integer values * @param hibyte top byte of each Unicode character * @throws NullPointerException if ascii is null * @see #String(byte[]) * @see #String(byte[], String) * @see #String(byte[], int, int) * @see #String(byte[], int, int, String) * @see #String(byte[], int, int, int) * @deprecated use {@link #String(byte[], String)} to perform * correct encoding */ public String(byte[] ascii, int hibyte) { init(ascii, hibyte, 0, ascii.length); } /** * Creates a new String using the portion of the byte array starting at the * offset and ending at offset + count. Uses the specified encoding type * to decode the byte array, so the resulting string may be longer or * shorter than the byte array. For more decoding control, use * {@link java.nio.charset.CharsetDecoder}, and for valid character sets, * see {@link java.nio.charset.Charset}. The behavior is not specified if * the decoder encounters invalid characters; this implementation throws * an Error. * * @param data byte array to copy * @param offset the offset to start at * @param count the number of characters in the array to use * @param encoding the name of the encoding to use * @throws NullPointerException if data or encoding is null * @throws IndexOutOfBoundsException if offset or count is incorrect * (while unspecified, this is a StringIndexOutOfBoundsException) * @throws UnsupportedEncodingException if encoding is not found * @throws Error if the decoding fails * @since 1.1 */ public String(byte[] data, int offset, int count, String encoding) throws UnsupportedEncodingException { init (data, offset, count, encoding); } /** * Creates a new String using the byte array. Uses the specified encoding * type to decode the byte array, so the resulting string may be longer or * shorter than the byte array. For more decoding control, use * {@link java.nio.charset.CharsetDecoder}, and for valid character sets, * see {@link java.nio.charset.Charset}. The behavior is not specified if * the decoder encounters invalid characters; this implementation throws * an Error. * * @param data byte array to copy * @param encoding the name of the encoding to use * @throws NullPointerException if data or encoding is null * @throws UnsupportedEncodingException if encoding is not found * @throws Error if the decoding fails * @see #String(byte[], int, int, String) * @since 1.1 */ public String(byte[] data, String encoding) throws UnsupportedEncodingException { this(data, 0, data.length, encoding); } /** * Creates a new String using the portion of the byte array starting at the * offset and ending at offset + count. 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 * @param offset the offset to start at * @param count the number of characters in the array to use * @throws NullPointerException if data is null * @throws IndexOutOfBoundsException if offset or count is incorrect * @throws Error if the decoding fails * @see #String(byte[], int, int, String) * @since 1.1 */ public String(byte[] data, int offset, int count) { try { init (data, offset, count, System.getProperty("file.encoding", "8859_1")); } catch (UnsupportedEncodingException x1) { // Maybe the default encoding is bad. try
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -