📄 string.java
字号:
/* * Copyright 1994-2002 Sun Microsystems, Inc. All Rights Reserved. * * This software is the proprietary information of Sun Microsystems, Inc. * Use is subject to license terms. * */package java.lang;import java.io.UnsupportedEncodingException;import com.sun.cldc.i18n.*;/** * 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 * 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>. * * @author Lee Boynton * @author Arthur van Hoff * @version 1.121, 10/06/99 (CLDC 1.0, Spring 2000) * @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(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, CLDC 1.0 */public finalclass String { /** 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; /** * Initializes a newly created <code>String</code> object so that it * represents an empty character sequence. */ 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. * * @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. 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. * @throws NullPointerException if <code>value</code> is <code>null</code>. */ 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. 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. * @exception NullPointerException if <code>value</code> is * <code>null</code>. */ 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); } /** * 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 off Index of the first byte to convert * @param len 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 off, int len, String enc) throws UnsupportedEncodingException { this(Helper.byteToCharArray(bytes, off, len, 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 The name of a supported character encoding * * @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 off Index of the first byte to convert * @param len Number of bytes to convert * @since JDK1.1 */ public String(byte bytes[], int off, int len) { this(Helper.byteToCharArray(bytes, off, len)); } /** * 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); } /** * Allocates a new string that contains the sequence of characters * currently contained in the string buffer argument. The contents of * the string buffer are copied; subsequent modification of the string * buffer does not affect the newly created string. * * @param buffer a <code>StringBuffer</code>. * @throws NullPointerException If <code>buffer</code> is * <code>null</code>. */ public String (StringBuffer buffer) { synchronized(buffer) { buffer.setShared(); this.value = buffer.getValue(); this.offset = 0; this.count = buffer.length(); } } // Package private constructor which shares value array for speed. 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>. The first character * of the sequence is at index <code>0</code>, the next at index * <code>1</code>, and so on, as for array indexing. * * @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>. * @exception IndexOutOfBoundsException if the <code>index</code> * argument is negative or not less than the length of this * string. */ public native char charAt(int index); /****** * public char charAt(int index) { * if ((index < 0) || (index >= count)) { * throw new StringIndexOutOfBoundsException(index); * } * return value[index + offset]; * } *****/ /** * Copies characters from this string into the destination character * array. * <p> * The first character to be copied is at index <code>srcBegin</code>; * the last character to be copied is at index <code>srcEnd-1</code> * (thus the total number of characters to be copied is * <code>srcEnd-srcBegin</code>). The characters are copied into the * subarray of <code>dst</code> starting at index <code>dstBegin</code> * and ending at index: * <p><blockquote><pre> * dstbegin + (srcEnd-srcBegin) - 1 * </pre></blockquote> * * @param srcBegin index of the first character in the string * to copy. * @param srcEnd index after the last character in the string * to copy. * @param dst the destination array. * @param dstBegin the start offset in the destination array. * @exception IndexOutOfBoundsException If any of the following * is true: * <ul><li><code>srcBegin</code> is negative. * <li><code>srcBegin</code> is greater than <code>srcEnd</code> * <li><code>srcEnd</code> is greater than the length of this * string * <li><code>dstBegin</code> is negative * <li><code>dstBegin+(srcEnd-srcBegin)</code> is larger than * <code>dst.length</code></ul> * @exception NullPointerException if <code>dst</code> is <code>null</code> */ public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) { if (srcBegin < 0) { throw new StringIndexOutOfBoundsException(srcBegin);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -