string.java
来自「《移动Agent技术》一书的所有章节源代码。」· Java 代码 · 共 1,565 行 · 第 1/4 页
JAVA
1,565 行
/*
* @(#)String.java 1.84 97/12/18
*
* Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
*
* This software is the confidential and proprietary information of Sun
* Microsystems, Inc. ("Confidential Information"). You shall not
* disclose such Confidential Information and shall use it only in
* accordance with the terms of the license agreement you entered into
* with Sun.
*
* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
* SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
* SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
* THIS SOFTWARE OR ITS DERIVATIVES.
*
* CopyrightVersion 1.1_beta
*
*/
package java.lang;
import java.util.Hashtable;
import java.util.Locale;
import sun.io.ByteToCharConverter;
import sun.io.CharToByteConverter;
import java.io.CharConversionException;
import java.io.UnsupportedEncodingException;
/**
* 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.
* <p>
* The Java language provides special support for the string
* concatentation 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>.
*
* @author Lee Boynton
* @author Arthur van Hoff
* @version 1.84, 12/18/97
* @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 {
/** 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;
/**
* Allocates a new <code>String</code> containing no characters.
*/
public String() {
value = new char[0];
}
/**
* Allocates a new string that contains the same sequence of
* characters as the string argument.
*
* @param value a <code>String</code>.
*/
public String(String value) {
count = value.length();
this.value = new char[count];
value.getChars(0, count, this.value, 0);
}
/**
* Allocates a new <code>String</code> so that it represents the
* sequence of characters currently contained in the character array
* argument.
*
* @param value the initial value of the string.
*/
public String(char value[]) {
this.count = value.length;
this.value = new char[count];
System.arraycopy(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.
*
* @param value array that is the source of characters.
* @param offset the initial offset.
* @param count the length.
* @exception StringIndexOutOfBoundsException 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;
System.arraycopy(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 character-encoding name or
* that use the platform's default encoding.
*
* @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 StringIndexOutOfBoundsException 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) {
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 > ascii.length - count) {
throw new StringIndexOutOfBoundsException(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 character-encoding name or
* that use the platform's default encoding.
*
* @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);
}
/**
* Construct a new <code>String</code> by converting the specified
* subarray of bytes using the specified character-encoding converter. The
* length of the new <code>String</code> is a function of the encoding, and
* hence may not be equal to the length of the subarray.
*
* @param bytes The bytes to be converted into characters
* @param offset Index of the first byte to convert
* @param length Number of bytes to convert
* @param btc A ByteToCharConverter
*/
private String(byte bytes[], int offset, int length,
ByteToCharConverter btc)
{
int estCount = btc.getMaxCharsPerByte() * length;
value = new char[estCount];
try {
count = btc.convert(bytes, offset, offset+length,
value, 0, estCount);
count += btc.flush(value, btc.nextCharIndex(), estCount);
}
catch (CharConversionException x) {
count = btc.nextCharIndex();
}
if (count < estCount) {
// A multi-byte format was used: Trim the char array.
char[] trimValue = new char[count];
System.arraycopy(value, 0, trimValue, 0, count);
value = trimValue;
}
}
/**
* Construct a new <code>String</code> by converting the specified
* subarray of bytes using the specified character encoding. The length of
* the new <code>String</code> is a function of the encoding, and hence may
* not be equal to the length of the subarray.
*
* @param bytes The bytes to be converted into characters
* @param offset Index of the first byte to convert
* @param length Number of bytes to convert
* @param enc The name of a character encoding
*
* @exception UnsupportedEncodingException
* If the named encoding is not supported
* @since JDK1.1
*/
public String(byte bytes[], int offset, int length, String enc)
throws UnsupportedEncodingException
{
this(bytes, offset, length, ByteToCharConverter.getConverter(enc));
}
/**
* Construct a new <code>String</code> by converting the specified array
* of bytes using the specified character encoding. The length of the new
* <code>String</code> is a function of the encoding, and hence may not be
* equal to the length of the byte array.
*
* @param bytes The bytes to be converted into characters
* @param enc A character-encoding name
*
* @exception UnsupportedEncodingException
* If the named encoding is not supported
* @since JDK1.1
*/
public String(byte bytes[], String enc)
throws UnsupportedEncodingException
{
this(bytes, 0, bytes.length, enc);
}
/**
* Construct a new <code>String</code> by converting the specified
* subarray of bytes using the platform's default character encoding. The
* length of the new <code>String</code> is a function of the encoding, and
* hence may not be equal to the length of the subarray.
*
* @param bytes The bytes to be converted into characters
* @param offset Index of the first byte to convert
* @param length Number of bytes to convert
* @since JDK1.1
*/
public String(byte bytes[], int offset, int length) {
this(bytes, offset, length, ByteToCharConverter.getDefault());
}
/**
* Construct a new <code>String</code> by converting the specified array
* of bytes using the platform's default character encoding. The length of
* the new <code>String</code> is a function of the encoding, and hence may
* not be equal to the length of the byte array.
*
* @param bytes The bytes to be converted into characters
* @since JDK1.1
*/
public String(byte bytes[]) {
this(bytes, 0, bytes.length, ByteToCharConverter.getDefault());
}
/**
* Allocates a new string that contains the sequence of characters
* currently contained in the string buffer argument.
*
* @param buffer a <code>StringBuffer</code>.
*/
public String (StringBuffer buffer) {
synchronized(buffer) {
buffer.setShared();
this.value = buffer.getValue();
this.offset = 0;
this.count = buffer.length();
}
}
// Private constructor which shares value array for speed.
private String(int offset, int count, char value[]) {
this.value = value;
this.offset = offset;
this.count = count;
}
/**
* Returns the length of this string.
* The length is equal to the number of 16-bit
* Unicode characters in the string.
*
* @return the length of the sequence of characters represented by this
* object.
*/
public int length() {
return count;
}
/**
* Returns the character at the specified index. An index ranges
* from <code>0</code> to <code>length() - 1</code>.
*
* @param index the index of the character.
* @return the character at the specified index of this string.
* The first character is at index <code>0</code>.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?