string.java
来自「This is a resource based on j2me embedde」· Java 代码 · 共 1,696 行 · 第 1/5 页
JAVA
1,696 行
/* * @(#)String.java 1.145 06/10/10 * * Copyright 1990-2008 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). * * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. * */package java.lang;import sun.misc.CVM;import java.io.ObjectStreamClass;import java.io.ObjectStreamField;import java.io.UnsupportedEncodingException;import java.util.ArrayList;import java.util.Comparator;import java.util.Locale;/** * The <code>String</code> class represents character strings. All * string literals in Java programs, such as <code>"abc"</code>, are * implemented as instances of this class. * <p> * Strings are constant; their values cannot be changed after they * are created. String buffers support mutable strings. * Because String objects are immutable they can be shared. For example: * <p><blockquote><pre> * String str = "abc"; * </pre></blockquote><p> * is equivalent to: * <p><blockquote><pre> * char data[] = {'a', 'b', 'c'}; * String str = new String(data); * </pre></blockquote><p> * Here are some more examples of how strings can be used: * <p><blockquote><pre> * System.out.println("abc"); * String cde = "cde"; * System.out.println("abc" + cde); * String c = "abc".substring(2,3); * String d = cde.substring(1, 2); * </pre></blockquote> * <p> * The class <code>String</code> includes methods for examining * individual characters of the sequence, for comparing strings, for * searching strings, for extracting substrings, and for creating a * copy of a string with all characters translated to uppercase or to * lowercase. Case mapping relies heavily on the information provided * by the Unicode Consortium's Unicode 3.0 specification. The * specification's UnicodeData.txt and SpecialCasing.txt files are * used extensively to provide case mapping. * <p> * The Java language provides special support for the string * concatenation operator ( + ), and for conversion of * other objects to strings. String concatenation is implemented * through the <code>StringBuffer</code> class and its * <code>append</code> method. * String conversions are implemented through the method * <code>toString</code>, defined by <code>Object</code> and * inherited by all classes in Java. For additional information on * string concatenation and conversion, see Gosling, Joy, and Steele, * <i>The Java Language Specification</i>. * * <p> Unless otherwise noted, passing a <tt>null</tt> argument to a constructor * or method in this class will cause a {@link NullPointerException} to be * thrown. * * @author Lee Boynton * @author Arthur van Hoff * @version 1.152, 02/01/03 * @see java.lang.Object#toString() * @see java.lang.StringBuffer * @see java.lang.StringBuffer#append(boolean) * @see java.lang.StringBuffer#append(char) * @see java.lang.StringBuffer#append(char[]) * @see java.lang.StringBuffer#append(char[], int, int) * @see java.lang.StringBuffer#append(double) * @see java.lang.StringBuffer#append(float) * @see java.lang.StringBuffer#append(int) * @see java.lang.StringBuffer#append(long) * @see java.lang.StringBuffer#append(java.lang.Object) * @see java.lang.StringBuffer#append(java.lang.String) * @since JDK1.0 */public final class String implements java.io.Serializable, Comparable, CharSequence{ /** The value is used for character storage. */ private char value[]; /** The offset is the first index of the storage that is used. */ private int offset; /** The count is the number of characters in the String. */ private int count; /** use serialVersionUID from JDK 1.0.2 for interoperability */ private static final long serialVersionUID = -6849794470754667710L; /** * Class String is special cased within the Serialization Stream Protocol. * * A String instance is written intially into an ObjectOutputStream in the * following format: * <pre> * <code>TC_STRING</code> (utf String) * </pre> * The String is written by method <code>DataOutput.writeUTF</code>. * A new handle is generated to refer to all future references to the * string instance within the stream. */ private static final ObjectStreamField[] serialPersistentFields = new ObjectStreamField[0]; /** * Initializes a newly created <code>String</code> object so that it * represents an empty character sequence. Note that use of this * constructor is unnecessary since Strings are immutable. */ public String() { value = new char[0]; } /** * Initializes a newly created <code>String</code> object so that it * represents the same sequence of characters as the argument; in other * words, the newly created string is a copy of the argument string. Unless * an explicit copy of <code>original</code> is needed, use of this * constructor is unnecessary since Strings are immutable. * * @param original a <code>String</code>. */ public String(String original) { this.count = original.count; if (original.value.length > this.count) { // The array representing the String is bigger than the new // String itself. Perhaps this constructor is being called // in order to trim the baggage, so make a copy of the array. this.value = new char[this.count]; CVM.copyCharArray(original.value, original.offset, this.value, 0, this.count); } else { // The array representing the String is the same // size as the String, so no point in making a copy. this.value = original.value; } } /** * Allocates a new <code>String</code> so that it represents the * sequence of characters currently contained in the character array * argument. The contents of the character array are copied; subsequent * modification of the character array does not affect the newly created * string. * * @param value the initial value of the string. */ public String(char value[]) { this.count = value.length; this.value = new char[count]; CVM.copyCharArray(value, 0, this.value, 0, count); } /** * Allocates a new <code>String</code> that contains characters from * a subarray of the character array argument. The <code>offset</code> * argument is the index of the first character of the subarray and * the <code>count</code> argument specifies the length of the * subarray. The contents of the subarray are copied; subsequent * modification of the character array does not affect the newly * created string. * * @param value array that is the source of characters. * @param offset the initial offset. * @param count the length. * @exception IndexOutOfBoundsException if the <code>offset</code> * and <code>count</code> arguments index characters outside * the bounds of the <code>value</code> array. */ public String(char value[], int offset, int count) { if (offset < 0) { throw new StringIndexOutOfBoundsException(offset); } if (count < 0) { throw new StringIndexOutOfBoundsException(count); } // Note: offset or count might be near -1>>>1. if (offset > value.length - count) { throw new StringIndexOutOfBoundsException(offset + count); } this.value = new char[count]; this.count = count; CVM.copyCharArray(value, offset, this.value, 0, count); } /** * Allocates a new <code>String</code> constructed from a subarray * of an array of 8-bit integer values. * <p> * The <code>offset</code> argument is the index of the first byte * of the subarray, and the <code>count</code> argument specifies the * length of the subarray. * <p> * Each <code>byte</code> in the subarray is converted to a * <code>char</code> as specified in the method above. * * deprecated This method does not properly convert bytes into characters. * As of JDK 1.1, the preferred way to do this is via the * <code>String</code> constructors that take a charset name or that use * the platform's default charset. * * param ascii the bytes to be converted to characters. * param hibyte the top 8 bits of each 16-bit Unicode character. * param offset the initial offset. * param count the length. * exception IndexOutOfBoundsException if the <code>offset</code> * or <code>count</code> argument is invalid. * @see java.lang.String#String(byte[], int) * @see java.lang.String#String(byte[], int, int, java.lang.String) * @see java.lang.String#String(byte[], int, int) * @see java.lang.String#String(byte[], java.lang.String) * @see java.lang.String#String(byte[]) */ /* public String(byte ascii[], int hibyte, int offset, int count) { //checkBounds(ascii, offset, count); char value[] = new char[count]; this.count = count; this.value = value; if (hibyte == 0) { for (int i = count ; i-- > 0 ;) { value[i] = (char) (ascii[i + offset] & 0xff); } } else { hibyte <<= 8; for (int i = count ; i-- > 0 ;) { value[i] = (char) (hibyte | (ascii[i + offset] & 0xff)); } } } */ /** * Allocates a new <code>String</code> containing characters * constructed from an array of 8-bit integer values. Each character * <i>c</i>in the resulting string is constructed from the * corresponding component <i>b</i> in the byte array such that: * <p><blockquote><pre> * <b><i>c</i></b> == (char)(((hibyte & 0xff) << 8) * | (<b><i>b</i></b> & 0xff)) * </pre></blockquote> * * deprecated This method does not properly convert bytes into characters. * As of JDK 1.1, the preferred way to do this is via the * <code>String</code> constructors that take a charset name or * that use the platform's default charset. * * @param ascii the bytes to be converted to characters. * @param hibyte the top 8 bits of each 16-bit Unicode character. * @see java.lang.String#String(byte[], int, int, java.lang.String) * @see java.lang.String#String(byte[], int, int) * @see java.lang.String#String(byte[], java.lang.String) * @see java.lang.String#String(byte[]) */ /* public String(byte ascii[], int hibyte) { this(ascii, hibyte, 0, ascii.length); } */ /* Common private utility method used to bounds check the byte array * and requested offset & length values used by the String(byte[],..) * constructors. */ private static void checkBounds(byte[] bytes, int offset, int length) { if (length < 0) throw new StringIndexOutOfBoundsException(length); if (offset < 0) throw new StringIndexOutOfBoundsException(offset); if (offset > bytes.length - length) throw new StringIndexOutOfBoundsException(offset + length); } /** * Constructs a new <tt>String</tt> by decoding the specified subarray of * bytes using the specified charset. The length of the new * <tt>String</tt> is a function of the charset, and hence may not be equal * to the length of the subarray. * * <p> The behavior of this constructor when the given bytes are not valid * in the given charset is unspecified. * * @param bytes the bytes to be decoded into characters * @param offset the index of the first byte to decode * @param length the number of bytes to decode * @param charsetName the name of a supported encoding * * @throws UnsupportedEncodingException * if the named charset is not supported * @throws IndexOutOfBoundsException * if the <tt>offset</tt> and <tt>length</tt> arguments * index characters outside the bounds of the <tt>bytes</tt> * array * @since JDK1.1 */ public String(byte bytes[], int offset, int length, String charsetName) throws UnsupportedEncodingException { if (charsetName == null) throw new NullPointerException("charsetName"); checkBounds(bytes, offset, length); value = StringCoding.decode(charsetName, bytes, offset, length); count = value.length; }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?