java String源码

  1. /* 
  2.  * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved. 
  3.  * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. 
  4.  * 
  5.  * 
  6.  * 
  7.  * 
  8.  * 
  9.  * 
  10.  * 
  11.  * 
  12.  * 
  13.  * 
  14.  * 
  15.  * 
  16.  * 
  17.  * 
  18.  * 
  19.  * 
  20.  * 
  21.  * 
  22.  * 
  23.  * 
  24.  */  
  25. package java.lang;  
  26.   
  27. import java.io.ObjectStreamField;  
  28. import java.io.UnsupportedEncodingException;  
  29. import java.nio.charset.Charset;  
  30. import java.util.ArrayList;  
  31. import java.util.Arrays;  
  32. import java.util.Comparator;  
  33. import java.util.Formatter;  
  34. import java.util.Locale;  
  35. import java.util.regex.Matcher;  
  36. import java.util.regex.Pattern;  
  37. import java.util.regex.PatternSyntaxException;  
  38.   
  39. /** 
  40.  * The <code>String</code> class represents character strings. All 
  41.  * string literals in Java programs, such as <code>"abc"</code>, are 
  42.  * implemented as instances of this class. 
  43.  * <p> 
  44.  * Strings are constant; their values cannot be changed after they 
  45.  * are created. String buffers support mutable strings. 
  46.  * Because String objects are immutable they can be shared. For example: 
  47.  * <p><blockquote><pre> 
  48.  *     String str = "abc"; 
  49.  * </pre></blockquote><p> 
  50.  * is equivalent to: 
  51.  * <p><blockquote><pre> 
  52.  *     char data[] = {'a', 'b', 'c'}; 
  53.  *     String str = new String(data); 
  54.  * </pre></blockquote><p> 
  55.  * Here are some more examples of how strings can be used: 
  56.  * <p><blockquote><pre> 
  57.  *     System.out.println("abc"); 
  58.  *     String cde = "cde"; 
  59.  *     System.out.println("abc" + cde); 
  60.  *     String c = "abc".substring(2,3); 
  61.  *     String d = cde.substring(1, 2); 
  62.  * </pre></blockquote> 
  63.  * <p> 
  64.  * The class <code>String</code> includes methods for examining 
  65.  * individual characters of the sequence, for comparing strings, for 
  66.  * searching strings, for extracting substrings, and for creating a 
  67.  * copy of a string with all characters translated to uppercase or to 
  68.  * lowercase. Case mapping is based on the Unicode Standard version 
  69.  * specified by the {@link java.lang.Character Character} class. 
  70.  * <p> 
  71.  * The Java language provides special support for the string 
  72.  * concatenation operator ( + ), and for conversion of 
  73.  * other objects to strings. String concatenation is implemented 
  74.  * through the <code>StringBuilder</code>(or <code>StringBuffer</code>) 
  75.  * class and its <code>append</code> method. 
  76.  * String conversions are implemented through the method 
  77.  * <code>toString</code>, defined by <code>Object</code> and 
  78.  * inherited by all classes in Java. For additional information on 
  79.  * string concatenation and conversion, see Gosling, Joy, and Steele, 
  80.  * <i>The Java Language Specification</i>. 
  81.  * 
  82.  * <p> Unless otherwise noted, passing a <tt>null</tt> argument to a constructor 
  83.  * or method in this class will cause a {@link NullPointerException} to be 
  84.  * thrown. 
  85.  * 
  86.  * <p>A <code>String</code> represents a string in the UTF-16 format 
  87.  * in which <em>supplementary characters</em> are represented by <em>surrogate 
  88.  * pairs</em> (see the section <a href="Character.html#unicode">Unicode 
  89.  * Character Representations</a> in the <code>Character</code> class for 
  90.  * more information). 
  91.  * Index values refer to <code>char</code> code units, so a supplementary 
  92.  * character uses two positions in a <code>String</code>. 
  93.  * <p>The <code>String</code> class provides methods for dealing with 
  94.  * Unicode code points (i.e., characters), in addition to those for 
  95.  * dealing with Unicode code units (i.e., <code>char</code> values). 
  96.  * 
  97.  * @author  Lee Boynton 
  98.  * @author  Arthur van Hoff 
  99.  * @author  Martin Buchholz 
  100.  * @author  Ulf Zibis 
  101.  * @see     java.lang.Object#toString() 
  102.  * @see     java.lang.StringBuffer 
  103.  * @see     java.lang.StringBuilder 
  104.  * @see     java.nio.charset.Charset 
  105.  * @since   JDK1.0 
  106.  */  
  107.   
  108. public final class String  
  109.     implements java.io.Serializable, Comparable<String>, CharSequence {  
  110.     /** The value is used for character storage. */  
  111.     private final char value[];  
  112.   
  113.     /** Cache the hash code for the string */  
  114.     private int hash; // Default to 0  
  115.   
  116.     /** use serialVersionUID from JDK 1.0.2 for interoperability */  
  117.     private static final long serialVersionUID = -6849794470754667710L;  
  118.   
  119.     /** 
  120.      * Class String is special cased within the Serialization Stream Protocol. 
  121.      * 
  122.      * A String instance is written initially into an ObjectOutputStream in the 
  123.      * following format: 
  124.      * <pre> 
  125.      *      <code>TC_STRING</code> (utf String) 
  126.      * </pre> 
  127.      * The String is written by method <code>DataOutput.writeUTF</code>. 
  128.      * A new handle is generated to  refer to all future references to the 
  129.      * string instance within the stream. 
  130.      */  
  131.     private static final ObjectStreamField[] serialPersistentFields =  
  132.             new ObjectStreamField[0];  
  133.   
  134.     /** 
  135.      * Initializes a newly created {@code String} object so that it represents 
  136.      * an empty character sequence.  Note that use of this constructor is 
  137.      * unnecessary since Strings are immutable. 
  138.      */  
  139.     public String() {  
  140.         this.value = new char[0];  
  141.     }  
  142.   
  143.     /** 
  144.      * Initializes a newly created {@code String} object so that it represents 
  145.      * the same sequence of characters as the argument; in other words, the 
  146.      * newly created string is a copy of the argument string. Unless an 
  147.      * explicit copy of {@code original} is needed, use of this constructor is 
  148.      * unnecessary since Strings are immutable. 
  149.      * 
  150.      * @param  original 
  151.      *         A {@code String} 
  152.      */  
  153.     public String(String original) {  
  154.         this.value = original.value;  
  155.         this.hash = original.hash;  
  156.     }  
  157.   
  158.     /** 
  159.      * Allocates a new {@code String} so that it represents the sequence of 
  160.      * characters currently contained in the character array argument. The 
  161.      * contents of the character array are copied; subsequent modification of 
  162.      * the character array does not affect the newly created string. 
  163.      * 
  164.      * @param  value 
  165.      *         The initial value of the string 
  166.      */  
  167.     public String(char value[]) {  
  168.         this.value = Arrays.copyOf(value, value.length);  
  169.     }  
  170.   
  171.     /** 
  172.      * Allocates a new {@code String} that contains characters from a subarray 
  173.      * of the character array argument. The {@code offset} argument is the 
  174.      * index of the first character of the subarray and the {@code count} 
  175.      * argument specifies the length of the subarray. The contents of the 
  176.      * subarray are copied; subsequent modification of the character array does 
  177.      * not affect the newly created string. 
  178.      * 
  179.      * @param  value 
  180.      *         Array that is the source of characters 
  181.      * 
  182.      * @param  offset 
  183.      *         The initial offset 
  184.      * 
  185.      * @param  count 
  186.      *         The length 
  187.      * 
  188.      * @throws  IndexOutOfBoundsException 
  189.      *          If the {@code offset} and {@code count} arguments index 
  190.      *          characters outside the bounds of the {@code value} array 
  191.      */  
  192.     public String(char value[], int offset, int count) {  
  193.         if (offset < 0) {  
  194.             throw new StringIndexOutOfBoundsException(offset);  
  195.         }  
  196.         if (count < 0) {  
  197.             throw new StringIndexOutOfBoundsException(count);  
  198.         }  
  199.         // Note: offset or count might be near -1>>>1.  
  200.         if (offset > value.length - count) {  
  201.             throw new StringIndexOutOfBoundsException(offset + count);  
  202.         }  
  203.         this.value = Arrays.copyOfRange(value, offset, offset+count);  
  204.     }  
  205.   
  206.     /** 
  207.      * Allocates a new {@code String} that contains characters from a subarray 
  208.      * of the <a href="Character.html#unicode">Unicode code point</a> array 
  209.      * argument.  The {@code offset} argument is the index of the first code 
  210.      * point of the subarray and the {@code count} argument specifies the 
  211.      * length of the subarray.  The contents of the subarray are converted to 
  212.      * {@code char}s; subsequent modification of the {@code int} array does not 
  213.      * affect the newly created string. 
  214.      * 
  215.      * @param  codePoints 
  216.      *         Array that is the source of Unicode code points 
  217.      * 
  218.      * @param  offset 
  219.      *         The initial offset 
  220.      * 
  221.      * @param  count 
  222.      *         The length 
  223.      * 
  224.      * @throws  IllegalArgumentException 
  225.      *          If any invalid Unicode code point is found in {@code 
  226.      *          codePoints} 
  227.      * 
  228.      * @throws  IndexOutOfBoundsException 
  229.      *          If the {@code offset} and {@code count} arguments index 
  230.      *          characters outside the bounds of the {@code codePoints} array 
  231.      * 
  232.      * @since  1.5 
  233.      */  
  234.     public String(int[] codePoints, int offset, int count) {  
  235.         if (offset < 0) {  
  236.             throw new StringIndexOutOfBoundsException(offset);  
  237.         }  
  238.         if (count < 0) {  
  239.             throw new StringIndexOutOfBoundsException(count);  
  240.         }  
  241.         // Note: offset or count might be near -1>>>1.  
  242.         if (offset > codePoints.length - count) {  
  243.             throw new StringIndexOutOfBoundsException(offset + count);  
  244.         }  
  245.   
  246.         final int end = offset + count;  
  247.   
  248.         // Pass 1: Compute precise size of char[]  
  249.         int n = count;  
  250.         for (int i = offset; i < end; i++) {  
  251.             int c = codePoints[i];  
  252.             if (Character.isBmpCodePoint(c))  
  253.                 continue;  
  254.             else if (Character.isValidCodePoint(c))  
  255.                 n++;  
  256.             else throw new IllegalArgumentException(Integer.toString(c));  
  257.         }  
  258.   
  259.         // Pass 2: Allocate and fill in char[]  
  260.         final char[] v = new char[n];  
  261.   
  262.         for (int i = offset, j = 0; i < end; i++, j++) {  
  263.             int c = codePoints[i];  
  264.             if (Character.isBmpCodePoint(c))  
  265.                 v[j] = (char)c;  
  266.             else  
  267.                 Character.toSurrogates(c, v, j++);  
  268.         }  
  269.   
  270.         this.value = v;  
  271.     }  
  272.   
  273.     /** 
  274.      * Allocates a new {@code String} constructed from a subarray of an array 
  275.      * of 8-bit integer values. 
  276.      * 
  277.      * <p> The {@code offset} argument is the index of the first byte of the 
  278.      * subarray, and the {@code count} argument specifies the length of the 
  279.      * subarray. 
  280.      * 
  281.      * <p> Each {@code byte} in the subarray is converted to a {@code char} as 
  282.      * specified in the method above. 
  283.      * 
  284.      * @deprecated This method does not properly convert bytes into characters. 
  285.      * As of JDK 1.1, the preferred way to do this is via the 
  286.      * {@code String} constructors that take a {@link 
  287.      * java.nio.charset.Charset}, charset name, or that use the platform's 
  288.      * default charset. 
  289.      * 
  290.      * @param  ascii 
  291.      *         The bytes to be converted to characters 
  292.      * 
  293.      * @param  hibyte 
  294.      *         The top 8 bits of each 16-bit Unicode code unit 
  295.      * 
  296.      * @param  offset 
  297.      *         The initial offset 
  298.      * @param  count 
  299.      *         The length 
  300.      * 
  301.      * @throws  IndexOutOfBoundsException 
  302.      *          If the {@code offset} or {@code count} argument is invalid 
  303.      * 
  304.      * @see  #String(byte[], int) 
  305.      * @see  #String(byte[], int, int, java.lang.String) 
  306.      * @see  #String(byte[], int, int, java.nio.charset.Charset) 
  307.      * @see  #String(byte[], int, int) 
  308.      * @see  #String(byte[], java.lang.String) 
  309.      * @see  #String(byte[], java.nio.charset.Charset) 
  310.      * @see  #String(byte[]) 
  311.      */  
  312.     @Deprecated  
  313.     public String(byte ascii[], int hibyte, int offset, int count) {  
  314.         checkBounds(ascii, offset, count);  
  315.         char value[] = new char[count];  
  316.   
  317.         if (hibyte == 0) {  
  318.             for (int i = count; i-- > 0;) {  
  319.                 value[i] = (char)(ascii[i + offset] & 0xff);  
  320.             }  
  321.         } else {  
  322.             hibyte <<= 8;  
  323.             for (int i = count; i-- > 0;) {  
  324.                 value[i] = (char)(hibyte | (ascii[i + offset] & 0xff));  
  325.             }  
  326.         }  
  327.         this.value = value;  
  328.     }  
  329.   
  330.     /** 
  331.      * Allocates a new {@code String} containing characters constructed from 
  332.      * an array of 8-bit integer values. Each character <i>c</i>in the 
  333.      * resulting string is constructed from the corresponding component 
  334.      * <i>b</i> in the byte array such that: 
  335.      * 
  336.      * <blockquote><pre> 
  337.      *     <b><i>c</i></b> == (char)(((hibyte & 0xff) << 8) 
  338.      *                         | (<b><i>b</i></b> & 0xff)) 
  339.      * </pre></blockquote> 
  340.      * 
  341.      * @deprecated  This method does not properly convert bytes into 
  342.      * characters.  As of JDK 1.1, the preferred way to do this is via the 
  343.      * {@code String} constructors that take a {@link 
  344.      * java.nio.charset.Charset}, charset name, or that use the platform's 
  345.      * default charset. 
  346.      * 
  347.      * @param  ascii 
  348.      *         The bytes to be converted to characters 
  349.      * 
  350.      * @param  hibyte 
  351.      *         The top 8 bits of each 16-bit Unicode code unit 
  352.      * 
  353.      * @see  #String(byte[], int, int, java.lang.String) 
  354.      * @see  #String(byte[], int, int, java.nio.charset.Charset) 
  355.      * @see  #String(byte[], int, int) 
  356.      * @see  #String(byte[], java.lang.String) 
  357.      * @see  #String(byte[], java.nio.charset.Charset) 
  358.      * @see  #String(byte[]) 
  359.      */  
  360.     @Deprecated  
  361.     public String(byte ascii[], int hibyte) {  
  362.         this(ascii, hibyte, 0, ascii.length);  
  363.     }  
  364.   
  365.     /* Common private utility method used to bounds check the byte array 
  366.      * and requested offset & length values used by the String(byte[],..) 
  367.      * constructors. 
  368.      */  
  369.     private static void checkBounds(byte[] bytes, int offset, int length) {  
  370.         if (length < 0)  
  371.             throw new StringIndexOutOfBoundsException(length);  
  372.         if (offset < 0)  
  373.             throw new StringIndexOutOfBoundsException(offset);  
  374.         if (offset > bytes.length - length)  
  375.             throw new StringIndexOutOfBoundsException(offset + length);  
  376.     }  
  377.   
  378.     /** 
  379.      * Constructs a new {@code String} by decoding the specified subarray of 
  380.      * bytes using the specified charset.  The length of the new {@code String} 
  381.      * is a function of the charset, and hence may not be equal to the length 
  382.      * of the subarray. 
  383.      * 
  384.      * <p> The behavior of this constructor when the given bytes are not valid 
  385.      * in the given charset is unspecified.  The {@link 
  386.      * java.nio.charset.CharsetDecoder} class should be used when more control 
  387.      * over the decoding process is required. 
  388.      * 
  389.      * @param  bytes 
  390.      *         The bytes to be decoded into characters 
  391.      * 
  392.      * @param  offset 
  393.      *         The index of the first byte to decode 
  394.      * 
  395.      * @param  length 
  396.      *         The number of bytes to decode 
  397.  
  398.      * @param  charsetName 
  399.      *         The name of a supported {@linkplain java.nio.charset.Charset 
  400.      *         charset} 
  401.      * 
  402.      * @throws  UnsupportedEncodingException 
  403.      *          If the named charset is not supported 
  404.      * 
  405.      * @throws  IndexOutOfBoundsException 
  406.      *          If the {@code offset} and {@code length} arguments index 
  407.      *          characters outside the bounds of the {@code bytes} array 
  408.      * 
  409.      * @since  JDK1.1 
  410.      */  
  411.     public String(byte bytes[], int offset, int length, String charsetName)  
  412.             throws UnsupportedEncodingException {  
  413.         if (charsetName == null)  
  414.             throw new NullPointerException("charsetName");  
  415.         checkBounds(bytes, offset, length);  
  416.         this.value = StringCoding.decode(charsetName, bytes, offset, length);  
  417.     }  
  418.   
  419.     /** 
  420.      * Constructs a new {@code String} by decoding the specified subarray of 
  421.      * bytes using the specified {@linkplain java.nio.charset.Charset charset}. 
  422.      * The length of the new {@code String} is a function of the charset, and 
  423.      * hence may not be equal to the length of the subarray. 
  424.      * 
  425.      * <p> This method always replaces malformed-input and unmappable-character 
  426.      * sequences with this charset's default replacement string.  The {@link 
  427.      * java.nio.charset.CharsetDecoder} class should be used when more control 
  428.      * over the decoding process is required. 
  429.      * 
  430.      * @param  bytes 
  431.      *         The bytes to be decoded into characters 
  432.      * 
  433.      * @param  offset 
  434.      *         The index of the first byte to decode 
  435.      * 
  436.      * @param  length 
  437.      *         The number of bytes to decode 
  438.      * 
  439.      * @param  charset 
  440.      *         The {@linkplain java.nio.charset.Charset charset} to be used to 
  441.      *         decode the {@code bytes} 
  442.      * 
  443.      * @throws  IndexOutOfBoundsException 
  444.      *          If the {@code offset} and {@code length} arguments index 
  445.      *          characters outside the bounds of the {@code bytes} array 
  446.      * 
  447.      * @since  1.6 
  448.      */  
  449.     public String(byte bytes[], int offset, int length, Charset charset) {  
  450.         if (charset == null)  
  451.             throw new NullPointerException("charset");  
  452.         checkBounds(bytes, offset, length);  
  453.         this.value =  StringCoding.decode(charset, bytes, offset, length);  
  454.     }  
  455.   
  456.     /** 
  457.      * Constructs a new {@code String} by decoding the specified array of bytes 
  458.      * using the specified {@linkplain java.nio.charset.Charset charset}.  The 
  459.      * length of the new {@code String} is a function of the charset, and hence 
  460.      * may not be equal to the length of the byte array. 
  461.      * 
  462.      * <p> The behavior of this constructor when the given bytes are not valid 
  463.      * in the given charset is unspecified.  The {@link 
  464.      * java.nio.charset.CharsetDecoder} class should be used when more control 
  465.      * over the decoding process is required. 
  466.      * 
  467.      * @param  bytes 
  468.      *         The bytes to be decoded into characters 
  469.      * 
  470.      * @param  charsetName 
  471.      *         The name of a supported {@linkplain java.nio.charset.Charset 
  472.      *         charset} 
  473.      * 
  474.      * @throws  UnsupportedEncodingException 
  475.      *          If the named charset is not supported 
  476.      * 
  477.      * @since  JDK1.1 
  478.      */  
  479.     public String(byte bytes[], String charsetName)  
  480.             throws UnsupportedEncodingException {  
  481.         this(bytes, 0, bytes.length, charsetName);  
  482.     }  
  483.   
  484.     /** 
  485.      * Constructs a new {@code String} by decoding the specified array of 
  486.      * bytes using the specified {@linkplain java.nio.charset.Charset charset}. 
  487.      * The length of the new {@code String} is a function of the charset, and 
  488.      * hence may not be equal to the length of the byte array. 
  489.      * 
  490.      * <p> This method always replaces malformed-input and unmappable-character 
  491.      * sequences with this charset's default replacement string.  The {@link 
  492.      * java.nio.charset.CharsetDecoder} class should be used when more control 
  493.      * over the decoding process is required. 
  494.      * 
  495.      * @param  bytes 
  496.      *         The bytes to be decoded into characters 
  497.      * 
  498.      * @param  charset 
  499.      *         The {@linkplain java.nio.charset.Charset charset} to be used to 
  500.      *         decode the {@code bytes} 
  501.      * 
  502.      * @since  1.6 
  503.      */  
  504.     public String(byte bytes[], Charset charset) {  
  505.         this(bytes, 0, bytes.length, charset);  
  506.     }  
  507.   
  508.     /** 
  509.      * Constructs a new {@code String} by decoding the specified subarray of 
  510.      * bytes using the platform's default charset.  The length of the new 
  511.      * {@code String} is a function of the charset, and hence may not be equal 
  512.      * to the length of the subarray. 
  513.      * 
  514.      * <p> The behavior of this constructor when the given bytes are not valid 
  515.      * in the default charset is unspecified.  The {@link 
  516.      * java.nio.charset.CharsetDecoder} class should be used when more control 
  517.      * over the decoding process is required. 
  518.      * 
  519.      * @param  bytes 
  520.      *         The bytes to be decoded into characters 
  521.      * 
  522.      * @param  offset 
  523.      *         The index of the first byte to decode 
  524.      * 
  525.      * @param  length 
  526.      *         The number of bytes to decode 
  527.      * 
  528.      * @throws  IndexOutOfBoundsException 
  529.      *          If the {@code offset} and the {@code length} arguments index 
  530.      *          characters outside the bounds of the {@code bytes} array 
  531.      * 
  532.      * @since  JDK1.1 
  533.      */  
  534.     public String(byte bytes[], int offset, int length) {  
  535.         checkBounds(bytes, offset, length);  
  536.         this.value = StringCoding.decode(bytes, offset, length);  
  537.     }  
  538.   
  539.     /** 
  540.      * Constructs a new {@code String} by decoding the specified array of bytes 
  541.      * using the platform's default charset.  The length of the new {@code 
  542.      * String} is a function of the charset, and hence may not be equal to the 
  543.      * length of the byte array. 
  544.      * 
  545.      * <p> The behavior of this constructor when the given bytes are not valid 
  546.      * in the default charset is unspecified.  The {@link 
  547.      * java.nio.charset.CharsetDecoder} class should be used when more control 
  548.      * over the decoding process is required. 
  549.      * 
  550.      * @param  bytes 
  551.      *         The bytes to be decoded into characters 
  552.      * 
  553.      * @since  JDK1.1 
  554.      */  
  555.     public String(byte bytes[]) {  
  556.         this(bytes, 0, bytes.length);  
  557.     }  
  558.   
  559.     /** 
  560.      * Allocates a new string that contains the sequence of characters 
  561.      * currently contained in the string buffer argument. The contents of the 
  562.      * string buffer are copied; subsequent modification of the string buffer 
  563.      * does not affect the newly created string. 
  564.      * 
  565.      * @param  buffer 
  566.      *         A {@code StringBuffer} 
  567.      */  
  568.     public String(StringBuffer buffer) {  
  569.         synchronized(buffer) {  
  570.             this.value = Arrays.copyOf(buffer.getValue(), buffer.length());  
  571.         }  
  572.     }  
  573.   
  574.     /** 
  575.      * Allocates a new string that contains the sequence of characters 
  576.      * currently contained in the string builder argument. The contents of the 
  577.      * string builder are copied; subsequent modification of the string builder 
  578.      * does not affect the newly created string. 
  579.      * 
  580.      * <p> This constructor is provided to ease migration to {@code 
  581.      * StringBuilder}. Obtaining a string from a string builder via the {@code 
  582.      * toString} method is likely to run faster and is generally preferred. 
  583.      * 
  584.      * @param   builder 
  585.      *          A {@code StringBuilder} 
  586.      * 
  587.      * @since  1.5 
  588.      */  
  589.     public String(StringBuilder builder) {  
  590.         this.value = Arrays.copyOf(builder.getValue(), builder.length());  
  591.     }  
  592.   
  593.     /* 
  594.     * Package private constructor which shares value array for speed. 
  595.     * this constructor is always expected to be called with share==true. 
  596.     * a separate constructor is needed because we already have a public 
  597.     * String(char[]) constructor that makes a copy of the given char[]. 
  598.     */  
  599.     String(char[] value, boolean share) {  
  600.         // assert share : "unshared not supported";  
  601.         this.value = value;  
  602.     }  
  603.   
  604.     /** 
  605.      * Package private constructor 
  606.      * 
  607.      * @deprecated Use {@link #String(char[],int,int)} instead. 
  608.      */  
  609.     @Deprecated  
  610.     String(int offset, int count, char[] value) {  
  611.         this(value, offset, count);  
  612.     }  
  613.   
  614.     /** 
  615.      * Returns the length of this string. 
  616.      * The length is equal to the number of <a href="Character.html#unicode">Unicode 
  617.      * code units</a> in the string. 
  618.      * 
  619.      * @return  the length of the sequence of characters represented by this 
  620.      *          object. 
  621.      */  
  622.     public int length() {  
  623.         return value.length;  
  624.     }  
  625.   
  626.     /** 
  627.      * Returns <tt>true</tt> if, and only if, {@link #length()} is <tt>0</tt>. 
  628.      * 
  629.      * @return <tt>true</tt> if {@link #length()} is <tt>0</tt>, otherwise 
  630.      * <tt>false</tt> 
  631.      * 
  632.      * @since 1.6 
  633.      */  
  634.     public boolean isEmpty() {  
  635.         return value.length == 0;  
  636.     }  
  637.   
  638.     /** 
  639.      * Returns the <code>char</code> value at the 
  640.      * specified index. An index ranges from <code>0</code> to 
  641.      * <code>length() - 1</code>. The first <code>char</code> value of the sequence 
  642.      * is at index <code>0</code>, the next at index <code>1</code>, 
  643.      * and so on, as for array indexing. 
  644.      * 
  645.      * <p>If the <code>char</code> value specified by the index is a 
  646.      * <a href="Character.html#unicode">surrogate</a>, the surrogate 
  647.      * value is returned. 
  648.      * 
  649.      * @param      index   the index of the <code>char</code> value. 
  650.      * @return     the <code>char</code> value at the specified index of this string. 
  651.      *             The first <code>char</code> value is at index <code>0</code>. 
  652.      * @exception  IndexOutOfBoundsException  if the <code>index</code> 
  653.      *             argument is negative or not less than the length of this 
  654.      *             string. 
  655.      */  
  656.     public char charAt(int index) {  
  657.         if ((index < 0) || (index >= value.length)) {  
  658.             throw new StringIndexOutOfBoundsException(index);  
  659.         }  
  660.         return value[index];  
  661.     }  
  662.   
  663.     /** 
  664.      * Returns the character (Unicode code point) at the specified 
  665.      * index. The index refers to <code>char</code> values 
  666.      * (Unicode code units) and ranges from <code>0</code> to 
  667.      * {@link #length()}<code> - 1</code>. 
  668.      * 
  669.      * <p> If the <code>char</code> value specified at the given index 
  670.      * is in the high-surrogate range, the following index is less 
  671.      * than the length of this <code>String</code>, and the 
  672.      * <code>char</code> value at the following index is in the 
  673.      * low-surrogate range, then the supplementary code point 
  674.      * corresponding to this surrogate pair is returned. Otherwise, 
  675.      * the <code>char</code> value at the given index is returned. 
  676.      * 
  677.      * @param      index the index to the <code>char</code> values 
  678.      * @return     the code point value of the character at the 
  679.      *             <code>index</code> 
  680.      * @exception  IndexOutOfBoundsException  if the <code>index</code> 
  681.      *             argument is negative or not less than the length of this 
  682.      *             string. 
  683.      * @since      1.5 
  684.      */  
  685.     public int codePointAt(int index) {  
  686.         if ((index < 0) || (index >= value.length)) {  
  687.             throw new StringIndexOutOfBoundsException(index);  
  688.         }  
  689.         return Character.codePointAtImpl(value, index, value.length);  
  690.     }  
  691.   
  692.     /** 
  693.      * Returns the character (Unicode code point) before the specified 
  694.      * index. The index refers to <code>char</code> values 
  695.      * (Unicode code units) and ranges from <code>1</code> to {@link 
  696.      * CharSequence#length() length}. 
  697.      * 
  698.      * <p> If the <code>char</code> value at <code>(index - 1)</code> 
  699.      * is in the low-surrogate range, <code>(index - 2)</code> is not 
  700.      * negative, and the <code>char</code> value at <code>(index - 
  701.      * 2)</code> is in the high-surrogate range, then the 
  702.      * supplementary code point value of the surrogate pair is 
  703.      * returned. If the <code>char</code> value at <code>index - 
  704.      * 1</code> is an unpaired low-surrogate or a high-surrogate, the 
  705.      * surrogate value is returned. 
  706.      * 
  707.      * @param     index the index following the code point that should be returned 
  708.      * @return    the Unicode code point value before the given index. 
  709.      * @exception IndexOutOfBoundsException if the <code>index</code> 
  710.      *            argument is less than 1 or greater than the length 
  711.      *            of this string. 
  712.      * @since     1.5 
  713.      */  
  714.     public int codePointBefore(int index) {  
  715.         int i = index - 1;  
  716.         if ((i < 0) || (i >= value.length)) {  
  717.             throw new StringIndexOutOfBoundsException(index);  
  718.         }  
  719.         return Character.codePointBeforeImpl(value, index, 0);  
  720.     }  
  721.   
  722.     /** 
  723.      * Returns the number of Unicode code points in the specified text 
  724.      * range of this <code>String</code>. The text range begins at the 
  725.      * specified <code>beginIndex</code> and extends to the 
  726.      * <code>char</code> at index <code>endIndex - 1</code>. Thus the 
  727.      * length (in <code>char</code>s) of the text range is 
  728.      * <code>endIndex-beginIndex</code>. Unpaired surrogates within 
  729.      * the text range count as one code point each. 
  730.      * 
  731.      * @param beginIndex the index to the first <code>char</code> of 
  732.      * the text range. 
  733.      * @param endIndex the index after the last <code>char</code> of 
  734.      * the text range. 
  735.      * @return the number of Unicode code points in the specified text 
  736.      * range 
  737.      * @exception IndexOutOfBoundsException if the 
  738.      * <code>beginIndex</code> is negative, or <code>endIndex</code> 
  739.      * is larger than the length of this <code>String</code>, or 
  740.      * <code>beginIndex</code> is larger than <code>endIndex</code>. 
  741.      * @since  1.5 
  742.      */  
  743.     public int codePointCount(int beginIndex, int endIndex) {  
  744.         if (beginIndex < 0 || endIndex > value.length || beginIndex > endIndex) {  
  745.             throw new IndexOutOfBoundsException();  
  746.         }  
  747.         return Character.codePointCountImpl(value, beginIndex, endIndex - beginIndex);  
  748.     }  
  749.   
  750.     /** 
  751.      * Returns the index within this <code>String</code> that is 
  752.      * offset from the given <code>index</code> by 
  753.      * <code>codePointOffset</code> code points. Unpaired surrogates 
  754.      * within the text range given by <code>index</code> and 
  755.      * <code>codePointOffset</code> count as one code point each. 
  756.      * 
  757.      * @param index the index to be offset 
  758.      * @param codePointOffset the offset in code points 
  759.      * @return the index within this <code>String</code> 
  760.      * @exception IndexOutOfBoundsException if <code>index</code> 
  761.      *   is negative or larger then the length of this 
  762.      *   <code>String</code>, or if <code>codePointOffset</code> is positive 
  763.      *   and the substring starting with <code>index</code> has fewer 
  764.      *   than <code>codePointOffset</code> code points, 
  765.      *   or if <code>codePointOffset</code> is negative and the substring 
  766.      *   before <code>index</code> has fewer than the absolute value 
  767.      *   of <code>codePointOffset</code> code points. 
  768.      * @since 1.5 
  769.      */  
  770.     public int offsetByCodePoints(int index, int codePointOffset) {  
  771.         if (index < 0 || index > value.length) {  
  772.             throw new IndexOutOfBoundsException();  
  773.         }  
  774.         return Character.offsetByCodePointsImpl(value, 0, value.length,  
  775.                 index, codePointOffset);  
  776.     }  
  777.   
  778.     /** 
  779.      * Copy characters from this string into dst starting at dstBegin. 
  780.      * This method doesn't perform any range checking. 
  781.      */  
  782.     void getChars(char dst[], int dstBegin) {  
  783.         System.arraycopy(value, 0, dst, dstBegin, value.length);  
  784.     }  
  785.   
  786.     /** 
  787.      * Copies characters from this string into the destination character 
  788.      * array. 
  789.      * <p> 
  790.      * The first character to be copied is at index <code>srcBegin</code>; 
  791.      * the last character to be copied is at index <code>srcEnd-1</code> 
  792.      * (thus the total number of characters to be copied is 
  793.      * <code>srcEnd-srcBegin</code>). The characters are copied into the 
  794.      * subarray of <code>dst</code> starting at index <code>dstBegin</code> 
  795.      * and ending at index: 
  796.      * <p><blockquote><pre> 
  797.      *     dstbegin + (srcEnd-srcBegin) - 1 
  798.      * </pre></blockquote> 
  799.      * 
  800.      * @param      srcBegin   index of the first character in the string 
  801.      *                        to copy. 
  802.      * @param      srcEnd     index after the last character in the string 
  803.      *                        to copy. 
  804.      * @param      dst        the destination array. 
  805.      * @param      dstBegin   the start offset in the destination array. 
  806.      * @exception IndexOutOfBoundsException If any of the following 
  807.      *            is true: 
  808.      *            <ul><li><code>srcBegin</code> is negative. 
  809.      *            <li><code>srcBegin</code> is greater than <code>srcEnd</code> 
  810.      *            <li><code>srcEnd</code> is greater than the length of this 
  811.      *                string 
  812.      *            <li><code>dstBegin</code> is negative 
  813.      *            <li><code>dstBegin+(srcEnd-srcBegin)</code> is larger than 
  814.      *                <code>dst.length</code></ul> 
  815.      */  
  816.     public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {  
  817.         if (srcBegin < 0) {  
  818.             throw new StringIndexOutOfBoundsException(srcBegin);  
  819.         }  
  820.         if (srcEnd > value.length) {  
  821.             throw new StringIndexOutOfBoundsException(srcEnd);  
  822.         }  
  823.         if (srcBegin > srcEnd) {  
  824.             throw new StringIndexOutOfBoundsException(srcEnd - srcBegin);  
  825.         }  
  826.         System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin);  
  827.     }  
  828.   
  829.     /** 
  830.      * Copies characters from this string into the destination byte array. Each 
  831.      * byte receives the 8 low-order bits of the corresponding character. The 
  832.      * eight high-order bits of each character are not copied and do not 
  833.      * participate in the transfer in any way. 
  834.      * 
  835.      * <p> The first character to be copied is at index {@code srcBegin}; the 
  836.      * last character to be copied is at index {@code srcEnd-1}.  The total 
  837.      * number of characters to be copied is {@code srcEnd-srcBegin}. The 
  838.      * characters, converted to bytes, are copied into the subarray of {@code 
  839.      * dst} starting at index {@code dstBegin} and ending at index: 
  840.      * 
  841.      * <blockquote><pre> 
  842.      *     dstbegin + (srcEnd-srcBegin) - 1 
  843.      * </pre></blockquote> 
  844.      * 
  845.      * @deprecated  This method does not properly convert characters into 
  846.      * bytes.  As of JDK 1.1, the preferred way to do this is via the 
  847.      * {@link #getBytes()} method, which uses the platform's default charset. 
  848.      * 
  849.      * @param  srcBegin 
  850.      *         Index of the first character in the string to copy 
  851.      * 
  852.      * @param  srcEnd 
  853.      *         Index after the last character in the string to copy 
  854.      * 
  855.      * @param  dst 
  856.      *         The destination array 
  857.      * 
  858.      * @param  dstBegin 
  859.      *         The start offset in the destination array 
  860.      * 
  861.      * @throws  IndexOutOfBoundsException 
  862.      *          If any of the following is true: 
  863.      *          <ul> 
  864.      *            <li> {@code srcBegin} is negative 
  865.      *            <li> {@code srcBegin} is greater than {@code srcEnd} 
  866.      *            <li> {@code srcEnd} is greater than the length of this String 
  867.      *            <li> {@code dstBegin} is negative 
  868.      *            <li> {@code dstBegin+(srcEnd-srcBegin)} is larger than {@code 
  869.      *                 dst.length} 
  870.      *          </ul> 
  871.      */  
  872.     @Deprecated  
  873.     public void getBytes(int srcBegin, int srcEnd, byte dst[], int dstBegin) {  
  874.         if (srcBegin < 0) {  
  875.             throw new StringIndexOutOfBoundsException(srcBegin);  
  876.         }  
  877.         if (srcEnd > value.length) {  
  878.             throw new StringIndexOutOfBoundsException(srcEnd);  
  879.         }  
  880.         if (srcBegin > srcEnd) {  
  881.             throw new StringIndexOutOfBoundsException(srcEnd - srcBegin);  
  882.         }  
  883.         int j = dstBegin;  
  884.         int n = srcEnd;  
  885.         int i = srcBegin;  
  886.         char[] val = value;   /* avoid getfield opcode */  
  887.   
  888.         while (i < n) {  
  889.             dst[j++] = (byte)val[i++];  
  890.         }  
  891.     }  
  892.   
  893.     /** 
  894.      * Encodes this {@code String} into a sequence of bytes using the named 
  895.      * charset, storing the result into a new byte array. 
  896.      * 
  897.      * <p> The behavior of this method when this string cannot be encoded in 
  898.      * the given charset is unspecified.  The {@link 
  899.      * java.nio.charset.CharsetEncoder} class should be used when more control 
  900.      * over the encoding process is required. 
  901.      * 
  902.      * @param  charsetName 
  903.      *         The name of a supported {@linkplain java.nio.charset.Charset 
  904.      *         charset} 
  905.      * 
  906.      * @return  The resultant byte array 
  907.      * 
  908.      * @throws  UnsupportedEncodingException 
  909.      *          If the named charset is not supported 
  910.      * 
  911.      * @since  JDK1.1 
  912.      */  
  913.     public byte[] getBytes(String charsetName)  
  914.             throws UnsupportedEncodingException {  
  915.         if (charsetName == nullthrow new NullPointerException();  
  916.         return StringCoding.encode(charsetName, value, 0, value.length);  
  917.     }  
  918.   
  919.     /** 
  920.      * Encodes this {@code String} into a sequence of bytes using the given 
  921.      * {@linkplain java.nio.charset.Charset charset}, storing the result into a 
  922.      * new byte array. 
  923.      * 
  924.      * <p> This method always replaces malformed-input and unmappable-character 
  925.      * sequences with this charset's default replacement byte array.  The 
  926.      * {@link java.nio.charset.CharsetEncoder} class should be used when more 
  927.      * control over the encoding process is required. 
  928.      * 
  929.      * @param  charset 
  930.      *         The {@linkplain java.nio.charset.Charset} to be used to encode 
  931.      *         the {@code String} 
  932.      * 
  933.      * @return  The resultant byte array 
  934.      * 
  935.      * @since  1.6 
  936.      */  
  937.     public byte[] getBytes(Charset charset) {  
  938.         if (charset == nullthrow new NullPointerException();  
  939.         return StringCoding.encode(charset, value, 0, value.length);  
  940.     }  
  941.   
  942.     /** 
  943.      * Encodes this {@code String} into a sequence of bytes using the 
  944.      * platform's default charset, storing the result into a new byte array. 
  945.      * 
  946.      * <p> The behavior of this method when this string cannot be encoded in 
  947.      * the default charset is unspecified.  The {@link 
  948.      * java.nio.charset.CharsetEncoder} class should be used when more control 
  949.      * over the encoding process is required. 
  950.      * 
  951.      * @return  The resultant byte array 
  952.      * 
  953.      * @since      JDK1.1 
  954.      */  
  955.     public byte[] getBytes() {  
  956.         return StringCoding.encode(value, 0, value.length);  
  957.     }  
  958.   
  959.     /** 
  960.      * Compares this string to the specified object.  The result is {@code 
  961.      * true} if and only if the argument is not {@code null} and is a {@code 
  962.      * String} object that represents the same sequence of characters as this 
  963.      * object. 
  964.      * 
  965.      * @param  anObject 
  966.      *         The object to compare this {@code String} against 
  967.      * 
  968.      * @return  {@code true} if the given object represents a {@code String} 
  969.      *          equivalent to this string, {@code false} otherwise 
  970.      * 
  971.      * @see  #compareTo(String) 
  972.      * @see  #equalsIgnoreCase(String) 
  973.      */  
  974.     public boolean equals(Object anObject) {  
  975.         if (this == anObject) {  
  976.             return true;  
  977.         }  
  978.         if (anObject instanceof String) {  
  979.             String anotherString = (String) anObject;  
  980.             int n = value.length;  
  981.             if (n == anotherString.value.length) {  
  982.                 char v1[] = value;  
  983.                 char v2[] = anotherString.value;  
  984.                 int i = 0;  
  985.                 while (n-- != 0) {  
  986.                     if (v1[i] != v2[i])  
  987.                             return false;  
  988.                     i++;  
  989.                 }  
  990.                 return true;  
  991.             }  
  992.         }  
  993.         return false;  
  994.     }  
  995.   
  996.     /** 
  997.      * Compares this string to the specified {@code StringBuffer}.  The result 
  998.      * is {@code true} if and only if this {@code String} represents the same 
  999.      * sequence of characters as the specified {@code StringBuffer}. 
  1000.      * 
  1001.      * @param  sb 
  1002.      *         The {@code StringBuffer} to compare this {@code String} against 
  1003.      * 
  1004.      * @return  {@code true} if this {@code String} represents the same 
  1005.      *          sequence of characters as the specified {@code StringBuffer}, 
  1006.      *          {@code false} otherwise 
  1007.      * 
  1008.      * @since  1.4 
  1009.      */  
  1010.     public boolean contentEquals(StringBuffer sb) {  
  1011.         synchronized (sb) {  
  1012.             return contentEquals((CharSequence) sb);  
  1013.         }  
  1014.     }  
  1015.   
  1016.     /** 
  1017.      * Compares this string to the specified {@code CharSequence}.  The result 
  1018.      * is {@code true} if and only if this {@code String} represents the same 
  1019.      * sequence of char values as the specified sequence. 
  1020.      * 
  1021.      * @param  cs 
  1022.      *         The sequence to compare this {@code String} against 
  1023.      * 
  1024.      * @return  {@code true} if this {@code String} represents the same 
  1025.      *          sequence of char values as the specified sequence, {@code 
  1026.      *          false} otherwise 
  1027.      * 
  1028.      * @since  1.5 
  1029.      */  
  1030.     public boolean contentEquals(CharSequence cs) {  
  1031.         if (value.length != cs.length())  
  1032.             return false;  
  1033.         // Argument is a StringBuffer, StringBuilder  
  1034.         if (cs instanceof AbstractStringBuilder) {  
  1035.             char v1[] = value;  
  1036.             char v2[] = ((AbstractStringBuilder) cs).getValue();  
  1037.             int i = 0;  
  1038.             int n = value.length;  
  1039.             while (n-- != 0) {  
  1040.                 if (v1[i] != v2[i])  
  1041.                     return false;  
  1042.                 i++;  
  1043.             }  
  1044.             return true;  
  1045.         }  
  1046.         // Argument is a String  
  1047.         if (cs.equals(this))  
  1048.             return true;  
  1049.         // Argument is a generic CharSequence  
  1050.         char v1[] = value;  
  1051.         int i = 0;  
  1052.         int n = value.length;  
  1053.         while (n-- != 0) {  
  1054.             if (v1[i] != cs.charAt(i))  
  1055.                 return false;  
  1056.             i++;  
  1057.         }  
  1058.         return true;  
  1059.     }  
  1060.   
  1061.     /** 
  1062.      * Compares this {@code String} to another {@code String}, ignoring case 
  1063.      * considerations.  Two strings are considered equal ignoring case if they 
  1064.      * are of the same length and corresponding characters in the two strings 
  1065.      * are equal ignoring case. 
  1066.      * 
  1067.      * <p> Two characters {@code c1} and {@code c2} are considered the same 
  1068.      * ignoring case if at least one of the following is true: 
  1069.      * <ul> 
  1070.      *   <li> The two characters are the same (as compared by the 
  1071.      *        {@code ==} operator) 
  1072.      *   <li> Applying the method {@link 
  1073.      *        java.lang.Character#toUpperCase(char)} to each character 
  1074.      *        produces the same result 
  1075.      *   <li> Applying the method {@link 
  1076.      *        java.lang.Character#toLowerCase(char)} to each character 
  1077.      *        produces the same result 
  1078.      * </ul> 
  1079.      * 
  1080.      * @param  anotherString 
  1081.      *         The {@code String} to compare this {@code String} against 
  1082.      * 
  1083.      * @return  {@code true} if the argument is not {@code null} and it 
  1084.      *          represents an equivalent {@code String} ignoring case; {@code 
  1085.      *          false} otherwise 
  1086.      * 
  1087.      * @see  #equals(Object) 
  1088.      */  
  1089.     public boolean equalsIgnoreCase(String anotherString) {  
  1090.         return (this == anotherString) ? true  
  1091.                 : (anotherString != null)  
  1092.                 && (anotherString.value.length == value.length)  
  1093.                 && regionMatches(true0, anotherString, 0, value.length);  
  1094.     }  
  1095.   
  1096.     /** 
  1097.      * Compares two strings lexicographically. 
  1098.      * The comparison is based on the Unicode value of each character in 
  1099.      * the strings. The character sequence represented by this 
  1100.      * <code>String</code> object is compared lexicographically to the 
  1101.      * character sequence represented by the argument string. The result is 
  1102.      * a negative integer if this <code>String</code> object 
  1103.      * lexicographically precedes the argument string. The result is a 
  1104.      * positive integer if this <code>String</code> object lexicographically 
  1105.      * follows the argument string. The result is zero if the strings 
  1106.      * are equal; <code>compareTo</code> returns <code>0</code> exactly when 
  1107.      * the {@link #equals(Object)} method would return <code>true</code>. 
  1108.      * <p> 
  1109.      * This is the definition of lexicographic ordering. If two strings are 
  1110.      * different, then either they have different characters at some index 
  1111.      * that is a valid index for both strings, or their lengths are different, 
  1112.      * or both. If they have different characters at one or more index 
  1113.      * positions, let <i>k</i> be the smallest such index; then the string 
  1114.      * whose character at position <i>k</i> has the smaller value, as 
  1115.      * determined by using the < operator, lexicographically precedes the 
  1116.      * other string. In this case, <code>compareTo</code> returns the 
  1117.      * difference of the two character values at position <code>k</code> in 
  1118.      * the two string -- that is, the value: 
  1119.      * <blockquote><pre> 
  1120.      * this.charAt(k)-anotherString.charAt(k) 
  1121.      * </pre></blockquote> 
  1122.      * If there is no index position at which they differ, then the shorter 
  1123.      * string lexicographically precedes the longer string. In this case, 
  1124.      * <code>compareTo</code> returns the difference of the lengths of the 
  1125.      * strings -- that is, the value: 
  1126.      * <blockquote><pre> 
  1127.      * this.length()-anotherString.length() 
  1128.      * </pre></blockquote> 
  1129.      * 
  1130.      * @param   anotherString   the <code>String</code> to be compared. 
  1131.      * @return  the value <code>0</code> if the argument string is equal to 
  1132.      *          this string; a value less than <code>0</code> if this string 
  1133.      *          is lexicographically less than the string argument; and a 
  1134.      *          value greater than <code>0</code> if this string is 
  1135.      *          lexicographically greater than the string argument. 
  1136.      */  
  1137.     public int compareTo(String anotherString) {  
  1138.         int len1 = value.length;  
  1139.         int len2 = anotherString.value.length;  
  1140.         int lim = Math.min(len1, len2);  
  1141.         char v1[] = value;  
  1142.         char v2[] = anotherString.value;  
  1143.   
  1144.         int k = 0;  
  1145.         while (k < lim) {  
  1146.             char c1 = v1[k];  
  1147.             char c2 = v2[k];  
  1148.             if (c1 != c2) {  
  1149.                 return c1 - c2;  
  1150.             }  
  1151.             k++;  
  1152.         }  
  1153.         return len1 - len2;  
  1154.     }  
  1155.   
  1156.     /** 
  1157.      * A Comparator that orders <code>String</code> objects as by 
  1158.      * <code>compareToIgnoreCase</code>. This comparator is serializable. 
  1159.      * <p> 
  1160.      * Note that this Comparator does <em>not</em> take locale into account, 
  1161.      * and will result in an unsatisfactory ordering for certain locales. 
  1162.      * The java.text package provides <em>Collators</em> to allow 
  1163.      * locale-sensitive ordering. 
  1164.      * 
  1165.      * @see     java.text.Collator#compare(String, String) 
  1166.      * @since   1.2 
  1167.      */  
  1168.     public static final Comparator<String> CASE_INSENSITIVE_ORDER  
  1169.                                          = new CaseInsensitiveComparator();  
  1170.     private static class CaseInsensitiveComparator  
  1171.             implements Comparator<String>, java.io.Serializable {  
  1172.         // use serialVersionUID from JDK 1.2.2 for interoperability  
  1173.         private static final long serialVersionUID = 8575799808933029326L;  
  1174.   
  1175.         public int compare(String s1, String s2) {  
  1176.             int n1 = s1.length();  
  1177.             int n2 = s2.length();  
  1178.             int min = Math.min(n1, n2);  
  1179.             for (int i = 0; i < min; i++) {  
  1180.                 char c1 = s1.charAt(i);  
  1181.                 char c2 = s2.charAt(i);  
  1182.                 if (c1 != c2) {  
  1183.                     c1 = Character.toUpperCase(c1);  
  1184.                     c2 = Character.toUpperCase(c2);  
  1185.                     if (c1 != c2) {  
  1186.                         c1 = Character.toLowerCase(c1);  
  1187.                         c2 = Character.toLowerCase(c2);  
  1188.                         if (c1 != c2) {  
  1189.                             // No overflow because of numeric promotion  
  1190.                             return c1 - c2;  
  1191.                         }  
  1192.                     }  
  1193.                 }  
  1194.             }  
  1195.             return n1 - n2;  
  1196.         }  
  1197.     }  
  1198.   
  1199.     /** 
  1200.      * Compares two strings lexicographically, ignoring case 
  1201.      * differences. This method returns an integer whose sign is that of 
  1202.      * calling <code>compareTo</code> with normalized versions of the strings 
  1203.      * where case differences have been eliminated by calling 
  1204.      * <code>Character.toLowerCase(Character.toUpperCase(character))</code> on 
  1205.      * each character. 
  1206.      * <p> 
  1207.      * Note that this method does <em>not</em> take locale into account, 
  1208.      * and will result in an unsatisfactory ordering for certain locales. 
  1209.      * The java.text package provides <em>collators</em> to allow 
  1210.      * locale-sensitive ordering. 
  1211.      * 
  1212.      * @param   str   the <code>String</code> to be compared. 
  1213.      * @return  a negative integer, zero, or a positive integer as the 
  1214.      *          specified String is greater than, equal to, or less 
  1215.      *          than this String, ignoring case considerations. 
  1216.      * @see     java.text.Collator#compare(String, String) 
  1217.      * @since   1.2 
  1218.      */  
  1219.     public int compareToIgnoreCase(String str) {  
  1220.         return CASE_INSENSITIVE_ORDER.compare(this, str);  
  1221.     }  
  1222.   
  1223.     /** 
  1224.      * Tests if two string regions are equal. 
  1225.      * <p> 
  1226.      * A substring of this <tt>String</tt> object is compared to a substring 
  1227.      * of the argument other. The result is true if these substrings 
  1228.      * represent identical character sequences. The substring of this 
  1229.      * <tt>String</tt> object to be compared begins at index <tt>toffset</tt> 
  1230.      * and has length <tt>len</tt>. The substring of other to be compared 
  1231.      * begins at index <tt>ooffset</tt> and has length <tt>len</tt>. The 
  1232.      * result is <tt>false</tt> if and only if at least one of the following 
  1233.      * is true: 
  1234.      * <ul><li><tt>toffset</tt> is negative. 
  1235.      * <li><tt>ooffset</tt> is negative. 
  1236.      * <li><tt>toffset+len</tt> is greater than the length of this 
  1237.      * <tt>String</tt> object. 
  1238.      * <li><tt>ooffset+len</tt> is greater than the length of the other 
  1239.      * argument. 
  1240.      * <li>There is some nonnegative integer <i>k</i> less than <tt>len</tt> 
  1241.      * such that: 
  1242.      * <tt>this.charAt(toffset+<i>k</i>) != other.charAt(ooffset+<i>k</i>)</tt> 
  1243.      * </ul> 
  1244.      * 
  1245.      * @param   toffset   the starting offset of the subregion in this string. 
  1246.      * @param   other     the string argument. 
  1247.      * @param   ooffset   the starting offset of the subregion in the string 
  1248.      *                    argument. 
  1249.      * @param   len       the number of characters to compare. 
  1250.      * @return  <code>true</code> if the specified subregion of this string 
  1251.      *          exactly matches the specified subregion of the string argument; 
  1252.      *          <code>false</code> otherwise. 
  1253.      */  
  1254.     public boolean regionMatches(int toffset, String other, int ooffset,  
  1255.             int len) {  
  1256.         char ta[] = value;  
  1257.         int to = toffset;  
  1258.         char pa[] = other.value;  
  1259.         int po = ooffset;  
  1260.         // Note: toffset, ooffset, or len might be near -1>>>1.  
  1261.         if ((ooffset < 0) || (toffset < 0)  
  1262.                 || (toffset > (long)value.length - len)  
  1263.                 || (ooffset > (long)other.value.length - len)) {  
  1264.             return false;  
  1265.         }  
  1266.         while (len-- > 0) {  
  1267.             if (ta[to++] != pa[po++]) {  
  1268.                 return false;  
  1269.             }  
  1270.         }  
  1271.         return true;  
  1272.     }  
  1273.   
  1274.     /** 
  1275.      * Tests if two string regions are equal. 
  1276.      * <p> 
  1277.      * A substring of this <tt>String</tt> object is compared to a substring 
  1278.      * of the argument <tt>other</tt>. The result is <tt>true</tt> if these 
  1279.      * substrings represent character sequences that are the same, ignoring 
  1280.      * case if and only if <tt>ignoreCase</tt> is true. The substring of 
  1281.      * this <tt>String</tt> object to be compared begins at index 
  1282.      * <tt>toffset</tt> and has length <tt>len</tt>. The substring of 
  1283.      * <tt>other</tt> to be compared begins at index <tt>ooffset</tt> and 
  1284.      * has length <tt>len</tt>. The result is <tt>false</tt> if and only if 
  1285.      * at least one of the following is true: 
  1286.      * <ul><li><tt>toffset</tt> is negative. 
  1287.      * <li><tt>ooffset</tt> is negative. 
  1288.      * <li><tt>toffset+len</tt> is greater than the length of this 
  1289.      * <tt>String</tt> object. 
  1290.      * <li><tt>ooffset+len</tt> is greater than the length of the other 
  1291.      * argument. 
  1292.      * <li><tt>ignoreCase</tt> is <tt>false</tt> and there is some nonnegative 
  1293.      * integer <i>k</i> less than <tt>len</tt> such that: 
  1294.      * <blockquote><pre> 
  1295.      * this.charAt(toffset+k) != other.charAt(ooffset+k) 
  1296.      * </pre></blockquote> 
  1297.      * <li><tt>ignoreCase</tt> is <tt>true</tt> and there is some nonnegative 
  1298.      * integer <i>k</i> less than <tt>len</tt> such that: 
  1299.      * <blockquote><pre> 
  1300.      * Character.toLowerCase(this.charAt(toffset+k)) != 
  1301.      Character.toLowerCase(other.charAt(ooffset+k)) 
  1302.      * </pre></blockquote> 
  1303.      * and: 
  1304.      * <blockquote><pre> 
  1305.      * Character.toUpperCase(this.charAt(toffset+k)) != 
  1306.      *         Character.toUpperCase(other.charAt(ooffset+k)) 
  1307.      * </pre></blockquote> 
  1308.      * </ul> 
  1309.      * 
  1310.      * @param   ignoreCase   if <code>true</code>, ignore case when comparing 
  1311.      *                       characters. 
  1312.      * @param   toffset      the starting offset of the subregion in this 
  1313.      *                       string. 
  1314.      * @param   other        the string argument. 
  1315.      * @param   ooffset      the starting offset of the subregion in the string 
  1316.      *                       argument. 
  1317.      * @param   len          the number of characters to compare. 
  1318.      * @return  <code>true</code> if the specified subregion of this string 
  1319.      *          matches the specified subregion of the string argument; 
  1320.      *          <code>false</code> otherwise. Whether the matching is exact 
  1321.      *          or case insensitive depends on the <code>ignoreCase</code> 
  1322.      *          argument. 
  1323.      */  
  1324.     public boolean regionMatches(boolean ignoreCase, int toffset,  
  1325.             String other, int ooffset, int len) {  
  1326.         char ta[] = value;  
  1327.         int to = toffset;  
  1328.         char pa[] = other.value;  
  1329.         int po = ooffset;  
  1330.         // Note: toffset, ooffset, or len might be near -1>>>1.  
  1331.         if ((ooffset < 0) || (toffset < 0)  
  1332.                 || (toffset > (long)value.length - len)  
  1333.                 || (ooffset > (long)other.value.length - len)) {  
  1334.             return false;  
  1335.         }  
  1336.         while (len-- > 0) {  
  1337.             char c1 = ta[to++];  
  1338.             char c2 = pa[po++];  
  1339.             if (c1 == c2) {  
  1340.                 continue;  
  1341.             }  
  1342.             if (ignoreCase) {  
  1343.                 // If characters don't match but case may be ignored,  
  1344.                 // try converting both characters to uppercase.  
  1345.                 // If the results match, then the comparison scan should  
  1346.                 // continue.  
  1347.                 char u1 = Character.toUpperCase(c1);  
  1348.                 char u2 = Character.toUpperCase(c2);  
  1349.                 if (u1 == u2) {  
  1350.                     continue;  
  1351.                 }  
  1352.                 // Unfortunately, conversion to uppercase does not work properly  
  1353.                 // for the Georgian alphabet, which has strange rules about case  
  1354.                 // conversion.  So we need to make one last check before  
  1355.                 // exiting.  
  1356.                 if (Character.toLowerCase(u1) == Character.toLowerCase(u2)) {  
  1357.                     continue;  
  1358.                 }  
  1359.             }  
  1360.             return false;  
  1361.         }  
  1362.         return true;  
  1363.     }  
  1364.   
  1365.     /** 
  1366.      * Tests if the substring of this string beginning at the 
  1367.      * specified index starts with the specified prefix. 
  1368.      * 
  1369.      * @param   prefix    the prefix. 
  1370.      * @param   toffset   where to begin looking in this string. 
  1371.      * @return  <code>true</code> if the character sequence represented by the 
  1372.      *          argument is a prefix of the substring of this object starting 
  1373.      *          at index <code>toffset</code>; <code>false</code> otherwise. 
  1374.      *          The result is <code>false</code> if <code>toffset</code> is 
  1375.      *          negative or greater than the length of this 
  1376.      *          <code>String</code> object; otherwise the result is the same 
  1377.      *          as the result of the expression 
  1378.      *          <pre> 
  1379.      *          this.substring(toffset).startsWith(prefix) 
  1380.      *          </pre> 
  1381.      */  
  1382.     public boolean startsWith(String prefix, int toffset) {  
  1383.         char ta[] = value;  
  1384.         int to = toffset;  
  1385.         char pa[] = prefix.value;  
  1386.         int po = 0;  
  1387.         int pc = prefix.value.length;  
  1388.         // Note: toffset might be near -1>>>1.  
  1389.         if ((toffset < 0) || (toffset > value.length - pc)) {  
  1390.             return false;  
  1391.         }  
  1392.         while (--pc >= 0) {  
  1393.             if (ta[to++] != pa[po++]) {  
  1394.                 return false;  
  1395.             }  
  1396.         }  
  1397.         return true;  
  1398.     }  
  1399.   
  1400.     /** 
  1401.      * Tests if this string starts with the specified prefix. 
  1402.      * 
  1403.      * @param   prefix   the prefix. 
  1404.      * @return  <code>true</code> if the character sequence represented by the 
  1405.      *          argument is a prefix of the character sequence represented by 
  1406.      *          this string; <code>false</code> otherwise. 
  1407.      *          Note also that <code>true</code> will be returned if the 
  1408.      *          argument is an empty string or is equal to this 
  1409.      *          <code>String</code> object as determined by the 
  1410.      *          {@link #equals(Object)} method. 
  1411.      * @since   1. 0 
  1412.      */  
  1413.     public boolean startsWith(String prefix) {  
  1414.         return startsWith(prefix, 0);  
  1415.     }  
  1416.   
  1417.     /** 
  1418.      * Tests if this string ends with the specified suffix. 
  1419.      * 
  1420.      * @param   suffix   the suffix. 
  1421.      * @return  <code>true</code> if the character sequence represented by the 
  1422.      *          argument is a suffix of the character sequence represented by 
  1423.      *          this object; <code>false</code> otherwise. Note that the 
  1424.      *          result will be <code>true</code> if the argument is the 
  1425.      *          empty string or is equal to this <code>String</code> object 
  1426.      *          as determined by the {@link #equals(Object)} method. 
  1427.      */  
  1428.     public boolean endsWith(String suffix) {  
  1429.         return startsWith(suffix, value.length - suffix.value.length);  
  1430.     }  
  1431.   
  1432.     /** 
  1433.      * Returns a hash code for this string. The hash code for a 
  1434.      * <code>String</code> object is computed as 
  1435.      * <blockquote><pre> 
  1436.      * s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] 
  1437.      * </pre></blockquote> 
  1438.      * using <code>int</code> arithmetic, where <code>s[i]</code> is the 
  1439.      * <i>i</i>th character of the string, <code>n</code> is the length of 
  1440.      * the string, and <code>^</code> indicates exponentiation. 
  1441.      * (The hash value of the empty string is zero.) 
  1442.      * 
  1443.      * @return  a hash code value for this object. 
  1444.      */  
  1445.     public int hashCode() {  
  1446.         int h = hash;  
  1447.         if (h == 0 && value.length > 0) {  
  1448.             char val[] = value;  
  1449.   
  1450.             for (int i = 0; i < value.length; i++) {  
  1451.                 h = 31 * h + val[i];  
  1452.             }  
  1453.             hash = h;  
  1454.         }  
  1455.         return h;  
  1456.     }  
  1457.   
  1458.     /** 
  1459.      * Returns the index within this string of the first occurrence of 
  1460.      * the specified character. If a character with value 
  1461.      * <code>ch</code> occurs in the character sequence represented by 
  1462.      * this <code>String</code> object, then the index (in Unicode 
  1463.      * code units) of the first such occurrence is returned. For 
  1464.      * values of <code>ch</code> in the range from 0 to 0xFFFF 
  1465.      * (inclusive), this is the smallest value <i>k</i> such that: 
  1466.      * <blockquote><pre> 
  1467.      * this.charAt(<i>k</i>) == ch 
  1468.      * </pre></blockquote> 
  1469.      * is true. For other values of <code>ch</code>, it is the 
  1470.      * smallest value <i>k</i> such that: 
  1471.      * <blockquote><pre> 
  1472.      * this.codePointAt(<i>k</i>) == ch 
  1473.      * </pre></blockquote> 
  1474.      * is true. In either case, if no such character occurs in this 
  1475.      * string, then <code>-1</code> is returned. 
  1476.      * 
  1477.      * @param   ch   a character (Unicode code point). 
  1478.      * @return  the index of the first occurrence of the character in the 
  1479.      *          character sequence represented by this object, or 
  1480.      *          <code>-1</code> if the character does not occur. 
  1481.      */  
  1482.     public int indexOf(int ch) {  
  1483.         return indexOf(ch, 0);  
  1484.     }  
  1485.   
  1486.     /** 
  1487.      * Returns the index within this string of the first occurrence of the 
  1488.      * specified character, starting the search at the specified index. 
  1489.      * <p> 
  1490.      * If a character with value <code>ch</code> occurs in the 
  1491.      * character sequence represented by this <code>String</code> 
  1492.      * object at an index no smaller than <code>fromIndex</code>, then 
  1493.      * the index of the first such occurrence is returned. For values 
  1494.      * of <code>ch</code> in the range from 0 to 0xFFFF (inclusive), 
  1495.      * this is the smallest value <i>k</i> such that: 
  1496.      * <blockquote><pre> 
  1497.      * (this.charAt(<i>k</i>) == ch) && (<i>k</i> >= fromIndex) 
  1498.      * </pre></blockquote> 
  1499.      * is true. For other values of <code>ch</code>, it is the 
  1500.      * smallest value <i>k</i> such that: 
  1501.      * <blockquote><pre> 
  1502.      * (this.codePointAt(<i>k</i>) == ch) && (<i>k</i> >= fromIndex) 
  1503.      * </pre></blockquote> 
  1504.      * is true. In either case, if no such character occurs in this 
  1505.      * string at or after position <code>fromIndex</code>, then 
  1506.      * <code>-1</code> is returned. 
  1507.      * 
  1508.      * <p> 
  1509.      * There is no restriction on the value of <code>fromIndex</code>. If it 
  1510.      * is negative, it has the same effect as if it were zero: this entire 
  1511.      * string may be searched. If it is greater than the length of this 
  1512.      * string, it has the same effect as if it were equal to the length of 
  1513.      * this string: <code>-1</code> is returned. 
  1514.      * 
  1515.      * <p>All indices are specified in <code>char</code> values 
  1516.      * (Unicode code units). 
  1517.      * 
  1518.      * @param   ch          a character (Unicode code point). 
  1519.      * @param   fromIndex   the index to start the search from. 
  1520.      * @return  the index of the first occurrence of the character in the 
  1521.      *          character sequence represented by this object that is greater 
  1522.      *          than or equal to <code>fromIndex</code>, or <code>-1</code> 
  1523.      *          if the character does not occur. 
  1524.      */  
  1525.     public int indexOf(int ch, int fromIndex) {  
  1526.         final int max = value.length;  
  1527.         if (fromIndex < 0) {  
  1528.             fromIndex = 0;  
  1529.         } else if (fromIndex >= max) {  
  1530.             // Note: fromIndex might be near -1>>>1.  
  1531.             return -1;  
  1532.         }  
  1533.   
  1534.         if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) {  
  1535.             // handle most cases here (ch is a BMP code point or a  
  1536.             // negative value (invalid code point))  
  1537.             final char[] value = this.value;  
  1538.             for (int i = fromIndex; i < max; i++) {  
  1539.                 if (value[i] == ch) {  
  1540.                     return i;  
  1541.                 }  
  1542.             }  
  1543.             return -1;  
  1544.         } else {  
  1545.             return indexOfSupplementary(ch, fromIndex);  
  1546.         }  
  1547.     }  
  1548.   
  1549.     /** 
  1550.      * Handles (rare) calls of indexOf with a supplementary character. 
  1551.      */  
  1552.     private int indexOfSupplementary(int ch, int fromIndex) {  
  1553.         if (Character.isValidCodePoint(ch)) {  
  1554.             final char[] value = this.value;  
  1555.             final char hi = Character.highSurrogate(ch);  
  1556.             final char lo = Character.lowSurrogate(ch);  
  1557.             final int max = value.length - 1;  
  1558.             for (int i = fromIndex; i < max; i++) {  
  1559.                 if (value[i] == hi && value[i + 1] == lo) {  
  1560.                     return i;  
  1561.                 }  
  1562.             }  
  1563.         }  
  1564.         return -1;  
  1565.     }  
  1566.   
  1567.     /** 
  1568.      * Returns the index within this string of the last occurrence of 
  1569.      * the specified character. For values of <code>ch</code> in the 
  1570.      * range from 0 to 0xFFFF (inclusive), the index (in Unicode code 
  1571.      * units) returned is the largest value <i>k</i> such that: 
  1572.      * <blockquote><pre> 
  1573.      * this.charAt(<i>k</i>) == ch 
  1574.      * </pre></blockquote> 
  1575.      * is true. For other values of <code>ch</code>, it is the 
  1576.      * largest value <i>k</i> such that: 
  1577.      * <blockquote><pre> 
  1578.      * this.codePointAt(<i>k</i>) == ch 
  1579.      * </pre></blockquote> 
  1580.      * is true.  In either case, if no such character occurs in this 
  1581.      * string, then <code>-1</code> is returned.  The 
  1582.      * <code>String</code> is searched backwards starting at the last 
  1583.      * character. 
  1584.      * 
  1585.      * @param   ch   a character (Unicode code point). 
  1586.      * @return  the index of the last occurrence of the character in the 
  1587.      *          character sequence represented by this object, or 
  1588.      *          <code>-1</code> if the character does not occur. 
  1589.      */  
  1590.     public int lastIndexOf(int ch) {  
  1591.         return lastIndexOf(ch, value.length - 1);  
  1592.     }  
  1593.   
  1594.     /** 
  1595.      * Returns the index within this string of the last occurrence of 
  1596.      * the specified character, searching backward starting at the 
  1597.      * specified index. For values of <code>ch</code> in the range 
  1598.      * from 0 to 0xFFFF (inclusive), the index returned is the largest 
  1599.      * value <i>k</i> such that: 
  1600.      * <blockquote><pre> 
  1601.      * (this.charAt(<i>k</i>) == ch) && (<i>k</i> <= fromIndex) 
  1602.      * </pre></blockquote> 
  1603.      * is true. For other values of <code>ch</code>, it is the 
  1604.      * largest value <i>k</i> such that: 
  1605.      * <blockquote><pre> 
  1606.      * (this.codePointAt(<i>k</i>) == ch) && (<i>k</i> <= fromIndex) 
  1607.      * </pre></blockquote> 
  1608.      * is true. In either case, if no such character occurs in this 
  1609.      * string at or before position <code>fromIndex</code>, then 
  1610.      * <code>-1</code> is returned. 
  1611.      * 
  1612.      * <p>All indices are specified in <code>char</code> values 
  1613.      * (Unicode code units). 
  1614.      * 
  1615.      * @param   ch          a character (Unicode code point). 
  1616.      * @param   fromIndex   the index to start the search from. There is no 
  1617.      *          restriction on the value of <code>fromIndex</code>. If it is 
  1618.      *          greater than or equal to the length of this string, it has 
  1619.      *          the same effect as if it were equal to one less than the 
  1620.      *          length of this string: this entire string may be searched. 
  1621.      *          If it is negative, it has the same effect as if it were -1: 
  1622.      *          -1 is returned. 
  1623.      * @return  the index of the last occurrence of the character in the 
  1624.      *          character sequence represented by this object that is less 
  1625.      *          than or equal to <code>fromIndex</code>, or <code>-1</code> 
  1626.      *          if the character does not occur before that point. 
  1627.      */  
  1628.     public int lastIndexOf(int ch, int fromIndex) {  
  1629.         if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) {  
  1630.             // handle most cases here (ch is a BMP code point or a  
  1631.             // negative value (invalid code point))  
  1632.             final char[] value = this.value;  
  1633.             int i = Math.min(fromIndex, value.length - 1);  
  1634.             for (; i >= 0; i--) {  
  1635.                 if (value[i] == ch) {  
  1636.                     return i;  
  1637.                 }  
  1638.             }  
  1639.             return -1;  
  1640.         } else {  
  1641.             return lastIndexOfSupplementary(ch, fromIndex);  
  1642.         }  
  1643.     }  
  1644.   
  1645.     /** 
  1646.      * Handles (rare) calls of lastIndexOf with a supplementary character. 
  1647.      */  
  1648.     private int lastIndexOfSupplementary(int ch, int fromIndex) {  
  1649.         if (Character.isValidCodePoint(ch)) {  
  1650.             final char[] value = this.value;  
  1651.             char hi = Character.highSurrogate(ch);  
  1652.             char lo = Character.lowSurrogate(ch);  
  1653.             int i = Math.min(fromIndex, value.length - 2);  
  1654.             for (; i >= 0; i--) {  
  1655.                 if (value[i] == hi && value[i + 1] == lo) {  
  1656.                     return i;  
  1657.                 }  
  1658.             }  
  1659.         }  
  1660.         return -1;  
  1661.     }  
  1662.   
  1663.     /** 
  1664.      * Returns the index within this string of the first occurrence of the 
  1665.      * specified substring. 
  1666.      * 
  1667.      * <p>The returned index is the smallest value <i>k</i> for which: 
  1668.      * <blockquote><pre> 
  1669.      * this.startsWith(str, <i>k</i>) 
  1670.      * </pre></blockquote> 
  1671.      * If no such value of <i>k</i> exists, then {@code -1} is returned. 
  1672.      * 
  1673.      * @param   str   the substring to search for. 
  1674.      * @return  the index of the first occurrence of the specified substring, 
  1675.      *          or {@code -1} if there is no such occurrence. 
  1676.      */  
  1677.     public int indexOf(String str) {  
  1678.         return indexOf(str, 0);  
  1679.     }  
  1680.   
  1681.     /** 
  1682.      * Returns the index within this string of the first occurrence of the 
  1683.      * specified substring, starting at the specified index. 
  1684.      * 
  1685.      * <p>The returned index is the smallest value <i>k</i> for which: 
  1686.      * <blockquote><pre> 
  1687.      * <i>k</i> >= fromIndex && this.startsWith(str, <i>k</i>) 
  1688.      * </pre></blockquote> 
  1689.      * If no such value of <i>k</i> exists, then {@code -1} is returned. 
  1690.      * 
  1691.      * @param   str         the substring to search for. 
  1692.      * @param   fromIndex   the index from which to start the search. 
  1693.      * @return  the index of the first occurrence of the specified substring, 
  1694.      *          starting at the specified index, 
  1695.      *          or {@code -1} if there is no such occurrence. 
  1696.      */  
  1697.     public int indexOf(String str, int fromIndex) {  
  1698.         return indexOf(value, 0, value.length,  
  1699.                 str.value, 0, str.value.length, fromIndex);  
  1700.     }  
  1701.   
  1702.     /** 
  1703.      * Code shared by String and StringBuffer to do searches. The 
  1704.      * source is the character array being searched, and the target 
  1705.      * is the string being searched for. 
  1706.      * 
  1707.      * @param   source       the characters being searched. 
  1708.      * @param   sourceOffset offset of the source string. 
  1709.      * @param   sourceCount  count of the source string. 
  1710.      * @param   target       the characters being searched for. 
  1711.      * @param   targetOffset offset of the target string. 
  1712.      * @param   targetCount  count of the target string. 
  1713.      * @param   fromIndex    the index to begin searching from. 
  1714.      */  
  1715.     static int indexOf(char[] source, int sourceOffset, int sourceCount,  
  1716.             char[] target, int targetOffset, int targetCount,  
  1717.             int fromIndex) {  
  1718.         if (fromIndex >= sourceCount) {  
  1719.             return (targetCount == 0 ? sourceCount : -1);  
  1720.         }  
  1721.         if (fromIndex < 0) {  
  1722.             fromIndex = 0;  
  1723.         }  
  1724.         if (targetCount == 0) {  
  1725.             return fromIndex;  
  1726.         }  
  1727.   
  1728.         char first = target[targetOffset];  
  1729.         int max = sourceOffset + (sourceCount - targetCount);  
  1730.   
  1731.         for (int i = sourceOffset + fromIndex; i <= max; i++) {  
  1732.             /* Look for first character. */  
  1733.             if (source[i] != first) {  
  1734.                 while (++i <= max && source[i] != first);  
  1735.             }  
  1736.   
  1737.             /* Found first character, now look at the rest of v2 */  
  1738.             if (i <= max) {  
  1739.                 int j = i + 1;  
  1740.                 int end = j + targetCount - 1;  
  1741.                 for (int k = targetOffset + 1; j < end && source[j]  
  1742.                         == target[k]; j++, k++);  
  1743.   
  1744.                 if (j == end) {  
  1745.                     /* Found whole string. */  
  1746.                     return i - sourceOffset;  
  1747.                 }  
  1748.             }  
  1749.         }  
  1750.         return -1;  
  1751.     }  
  1752.   
  1753.     /** 
  1754.      * Returns the index within this string of the last occurrence of the 
  1755.      * specified substring.  The last occurrence of the empty string "" 
  1756.      * is considered to occur at the index value {@code this.length()}. 
  1757.      * 
  1758.      * <p>The returned index is the largest value <i>k</i> for which: 
  1759.      * <blockquote><pre> 
  1760.      * this.startsWith(str, <i>k</i>) 
  1761.      * </pre></blockquote> 
  1762.      * If no such value of <i>k</i> exists, then {@code -1} is returned. 
  1763.      * 
  1764.      * @param   str   the substring to search for. 
  1765.      * @return  the index of the last occurrence of the specified substring, 
  1766.      *          or {@code -1} if there is no such occurrence. 
  1767.      */  
  1768.     public int lastIndexOf(String str) {  
  1769.         return lastIndexOf(str, value.length);  
  1770.     }  
  1771.   
  1772.     /** 
  1773.      * Returns the index within this string of the last occurrence of the 
  1774.      * specified substring, searching backward starting at the specified index. 
  1775.      * 
  1776.      * <p>The returned index is the largest value <i>k</i> for which: 
  1777.      * <blockquote><pre> 
  1778.      * <i>k</i> <= fromIndex && this.startsWith(str, <i>k</i>) 
  1779.      * </pre></blockquote> 
  1780.      * If no such value of <i>k</i> exists, then {@code -1} is returned. 
  1781.      * 
  1782.      * @param   str         the substring to search for. 
  1783.      * @param   fromIndex   the index to start the search from. 
  1784.      * @return  the index of the last occurrence of the specified substring, 
  1785.      *          searching backward from the specified index, 
  1786.      *          or {@code -1} if there is no such occurrence. 
  1787.      */  
  1788.     public int lastIndexOf(String str, int fromIndex) {  
  1789.         return lastIndexOf(value, 0, value.length,  
  1790.                 str.value, 0, str.value.length, fromIndex);  
  1791.     }  
  1792.   
  1793.     /** 
  1794.      * Code shared by String and StringBuffer to do searches. The 
  1795.      * source is the character array being searched, and the target 
  1796.      * is the string being searched for. 
  1797.      * 
  1798.      * @param   source       the characters being searched. 
  1799.      * @param   sourceOffset offset of the source string. 
  1800.      * @param   sourceCount  count of the source string. 
  1801.      * @param   target       the characters being searched for. 
  1802.      * @param   targetOffset offset of the target string. 
  1803.      * @param   targetCount  count of the target string. 
  1804.      * @param   fromIndex    the index to begin searching from. 
  1805.      */  
  1806.     static int lastIndexOf(char[] source, int sourceOffset, int sourceCount,  
  1807.             char[] target, int targetOffset, int targetCount,  
  1808.             int fromIndex) {  
  1809.         /* 
  1810.          * Check arguments; return immediately where possible. For 
  1811.          * consistency, don't check for null str. 
  1812.          */  
  1813.         int rightIndex = sourceCount - targetCount;  
  1814.         if (fromIndex < 0) {  
  1815.             return -1;  
  1816.         }  
  1817.         if (fromIndex > rightIndex) {  
  1818.             fromIndex = rightIndex;  
  1819.         }  
  1820.         /* Empty string always matches. */  
  1821.         if (targetCount == 0) {  
  1822.             return fromIndex;  
  1823.         }  
  1824.   
  1825.         int strLastIndex = targetOffset + targetCount - 1;  
  1826.         char strLastChar = target[strLastIndex];  
  1827.         int min = sourceOffset + targetCount - 1;  
  1828.         int i = min + fromIndex;  
  1829.   
  1830.         startSearchForLastChar:  
  1831.         while (true) {  
  1832.             while (i >= min && source[i] != strLastChar) {  
  1833.                 i--;  
  1834.             }  
  1835.             if (i < min) {  
  1836.                 return -1;  
  1837.             }  
  1838.             int j = i - 1;  
  1839.             int start = j - (targetCount - 1);  
  1840.             int k = strLastIndex - 1;  
  1841.   
  1842.             while (j > start) {  
  1843.                 if (source[j--] != target[k--]) {  
  1844.                     i--;  
  1845.                     continue startSearchForLastChar;  
  1846.                 }  
  1847.             }  
  1848.             return start - sourceOffset + 1;  
  1849.         }  
  1850.     }  
  1851.   
  1852.     /** 
  1853.      * Returns a new string that is a substring of this string. The 
  1854.      * substring begins with the character at the specified index and 
  1855.      * extends to the end of this string. <p> 
  1856.      * Examples: 
  1857.      * <blockquote><pre> 
  1858.      * "unhappy".substring(2) returns "happy" 
  1859.      * "Harbison".substring(3) returns "bison" 
  1860.      * "emptiness".substring(9) returns "" (an empty string) 
  1861.      * </pre></blockquote> 
  1862.      * 
  1863.      * @param      beginIndex   the beginning index, inclusive. 
  1864.      * @return     the specified substring. 
  1865.      * @exception  IndexOutOfBoundsException  if 
  1866.      *             <code>beginIndex</code> is negative or larger than the 
  1867.      *             length of this <code>String</code> object. 
  1868.      */  
  1869.     public String substring(int beginIndex) {  
  1870.         if (beginIndex < 0) {  
  1871.             throw new StringIndexOutOfBoundsException(beginIndex);  
  1872.         }  
  1873.         int subLen = value.length - beginIndex;  
  1874.         if (subLen < 0) {  
  1875.             throw new StringIndexOutOfBoundsException(subLen);  
  1876.         }  
  1877.         return (beginIndex == 0) ? this : new String(value, beginIndex, subLen);  
  1878.     }  
  1879.   
  1880.     /** 
  1881.      * Returns a new string that is a substring of this string. The 
  1882.      * substring begins at the specified <code>beginIndex</code> and 
  1883.      * extends to the character at index <code>endIndex - 1</code>. 
  1884.      * Thus the length of the substring is <code>endIndex-beginIndex</code>. 
  1885.      * <p> 
  1886.      * Examples: 
  1887.      * <blockquote><pre> 
  1888.      * "hamburger".substring(4, 8) returns "urge" 
  1889.      * "smiles".substring(1, 5) returns "mile" 
  1890.      * </pre></blockquote> 
  1891.      * 
  1892.      * @param      beginIndex   the beginning index, inclusive. 
  1893.      * @param      endIndex     the ending index, exclusive. 
  1894.      * @return     the specified substring. 
  1895.      * @exception  IndexOutOfBoundsException  if the 
  1896.      *             <code>beginIndex</code> is negative, or 
  1897.      *             <code>endIndex</code> is larger than the length of 
  1898.      *             this <code>String</code> object, or 
  1899.      *             <code>beginIndex</code> is larger than 
  1900.      *             <code>endIndex</code>. 
  1901.      */  
  1902.     public String substring(int beginIndex, int endIndex) {  
  1903.         if (beginIndex < 0) {  
  1904.             throw new StringIndexOutOfBoundsException(beginIndex);  
  1905.         }  
  1906.         if (endIndex > value.length) {  
  1907.             throw new StringIndexOutOfBoundsException(endIndex);  
  1908.         }  
  1909.         int subLen = endIndex - beginIndex;  
  1910.         if (subLen < 0) {  
  1911.             throw new StringIndexOutOfBoundsException(subLen);  
  1912.         }  
  1913.         return ((beginIndex == 0) && (endIndex == value.length)) ? this  
  1914.                 : new String(value, beginIndex, subLen);  
  1915.     }  
  1916.   
  1917.     /** 
  1918.      * Returns a new character sequence that is a subsequence of this sequence. 
  1919.      * 
  1920.      * <p> An invocation of this method of the form 
  1921.      * 
  1922.      * <blockquote><pre> 
  1923.      * str.subSequence(begin, end)</pre></blockquote> 
  1924.      * 
  1925.      * behaves in exactly the same way as the invocation 
  1926.      * 
  1927.      * <blockquote><pre> 
  1928.      * str.substring(begin, end)</pre></blockquote> 
  1929.      * 
  1930.      * This method is defined so that the <tt>String</tt> class can implement 
  1931.      * the {@link CharSequence} interface. </p> 
  1932.      * 
  1933.      * @param      beginIndex   the begin index, inclusive. 
  1934.      * @param      endIndex     the end index, exclusive. 
  1935.      * @return     the specified subsequence. 
  1936.      * 
  1937.      * @throws  IndexOutOfBoundsException 
  1938.      *          if <tt>beginIndex</tt> or <tt>endIndex</tt> are negative, 
  1939.      *          if <tt>endIndex</tt> is greater than <tt>length()</tt>, 
  1940.      *          or if <tt>beginIndex</tt> is greater than <tt>startIndex</tt> 
  1941.      * 
  1942.      * @since 1.4 
  1943.      * @spec JSR-51 
  1944.      */  
  1945.     public CharSequence subSequence(int beginIndex, int endIndex) {  
  1946.         return this.substring(beginIndex, endIndex);  
  1947.     }  
  1948.   
  1949.     /** 
  1950.      * Concatenates the specified string to the end of this string. 
  1951.      * <p> 
  1952.      * If the length of the argument string is <code>0</code>, then this 
  1953.      * <code>String</code> object is returned. Otherwise, a new 
  1954.      * <code>String</code> object is created, representing a character 
  1955.      * sequence that is the concatenation of the character sequence 
  1956.      * represented by this <code>String</code> object and the character 
  1957.      * sequence represented by the argument string.<p> 
  1958.      * Examples: 
  1959.      * <blockquote><pre> 
  1960.      * "cares".concat("s") returns "caress" 
  1961.      * "to".concat("get").concat("her") returns "together" 
  1962.      * </pre></blockquote> 
  1963.      * 
  1964.      * @param   str   the <code>String</code> that is concatenated to the end 
  1965.      *                of this <code>String</code>. 
  1966.      * @return  a string that represents the concatenation of this object's 
  1967.      *          characters followed by the string argument's characters. 
  1968.      */  
  1969.     public String concat(String str) {  
  1970.         int otherLen = str.length();  
  1971.         if (otherLen == 0) {  
  1972.             return this;  
  1973.         }  
  1974.         int len = value.length;  
  1975.         char buf[] = Arrays.copyOf(value, len + otherLen);  
  1976.         str.getChars(buf, len);  
  1977.         return new String(buf, true);  
  1978.     }  
  1979.   
  1980.     /** 
  1981.      * Returns a new string resulting from replacing all occurrences of 
  1982.      * <code>oldChar</code> in this string with <code>newChar</code>. 
  1983.      * <p> 
  1984.      * If the character <code>oldChar</code> does not occur in the 
  1985.      * character sequence represented by this <code>String</code> object, 
  1986.      * then a reference to this <code>String</code> object is returned. 
  1987.      * Otherwise, a new <code>String</code> object is created that 
  1988.      * represents a character sequence identical to the character sequence 
  1989.      * represented by this <code>String</code> object, except that every 
  1990.      * occurrence of <code>oldChar</code> is replaced by an occurrence 
  1991.      * of <code>newChar</code>. 
  1992.      * <p> 
  1993.      * Examples: 
  1994.      * <blockquote><pre> 
  1995.      * "mesquite in your cellar".replace('e', 'o') 
  1996.      *         returns "mosquito in your collar" 
  1997.      * "the war of baronets".replace('r', 'y') 
  1998.      *         returns "the way of bayonets" 
  1999.      * "sparring with a purple porpoise".replace('p', 't') 
  2000.      *         returns "starring with a turtle tortoise" 
  2001.      * "JonL".replace('q', 'x') returns "JonL" (no change) 
  2002.      * </pre></blockquote> 
  2003.      * 
  2004.      * @param   oldChar   the old character. 
  2005.      * @param   newChar   the new character. 
  2006.      * @return  a string derived from this string by replacing every 
  2007.      *          occurrence of <code>oldChar</code> with <code>newChar</code>. 
  2008.      */  
  2009.     public String replace(char oldChar, char newChar) {  
  2010.         if (oldChar != newChar) {  
  2011.             int len = value.length;  
  2012.             int i = -1;  
  2013.             char[] val = value; /* avoid getfield opcode */  
  2014.   
  2015.             while (++i < len) {  
  2016.                 if (val[i] == oldChar) {  
  2017.                     break;  
  2018.                 }  
  2019.             }  
  2020.             if (i < len) {  
  2021.                 char buf[] = new char[len];  
  2022.                 for (int j = 0; j < i; j++) {  
  2023.                     buf[j] = val[j];  
  2024.                 }  
  2025.                 while (i < len) {  
  2026.                     char c = val[i];  
  2027.                     buf[i] = (c == oldChar) ? newChar : c;  
  2028.                     i++;  
  2029.                 }  
  2030.                 return new String(buf, true);  
  2031.             }  
  2032.         }  
  2033.         return this;  
  2034.     }  
  2035.   
  2036.     /** 
  2037.      * Tells whether or not this string matches the given <a 
  2038.      * href="../util/regex/Pattern.html#sum">regular expression</a>. 
  2039.      * 
  2040.      * <p> An invocation of this method of the form 
  2041.      * <i>str</i><tt>.matches(</tt><i>regex</i><tt>)</tt> yields exactly the 
  2042.      * same result as the expression 
  2043.      * 
  2044.      * <blockquote><tt> {@link java.util.regex.Pattern}.{@link 
  2045.      * java.util.regex.Pattern#matches(String,CharSequence) 
  2046.      * matches}(</tt><i>regex</i><tt>,</tt> <i>str</i><tt>)</tt></blockquote> 
  2047.      * 
  2048.      * @param   regex 
  2049.      *          the regular expression to which this string is to be matched 
  2050.      * 
  2051.      * @return  <tt>true</tt> if, and only if, this string matches the 
  2052.      *          given regular expression 
  2053.      * 
  2054.      * @throws  PatternSyntaxException 
  2055.      *          if the regular expression's syntax is invalid 
  2056.      * 
  2057.      * @see java.util.regex.Pattern 
  2058.      * 
  2059.      * @since 1.4 
  2060.      * @spec JSR-51 
  2061.      */  
  2062.     public boolean matches(String regex) {  
  2063.         return Pattern.matches(regex, this);  
  2064.     }  
  2065.   
  2066.     /** 
  2067.      * Returns true if and only if this string contains the specified 
  2068.      * sequence of char values. 
  2069.      * 
  2070.      * @param s the sequence to search for 
  2071.      * @return true if this string contains <code>s</code>, false otherwise 
  2072.      * @throws NullPointerException if <code>s</code> is <code>null</code> 
  2073.      * @since 1.5 
  2074.      */  
  2075.     public boolean contains(CharSequence s) {  
  2076.         return indexOf(s.toString()) > -1;  
  2077.     }  
  2078.   
  2079.     /** 
  2080.      * Replaces the first substring of this string that matches the given <a 
  2081.      * href="../util/regex/Pattern.html#sum">regular expression</a> with the 
  2082.      * given replacement. 
  2083.      * 
  2084.      * <p> An invocation of this method of the form 
  2085.      * <i>str</i><tt>.replaceFirst(</tt><i>regex</i><tt>,</tt> <i>repl</i><tt>)</tt> 
  2086.      * yields exactly the same result as the expression 
  2087.      * 
  2088.      * <blockquote><tt> 
  2089.      * {@link java.util.regex.Pattern}.{@link java.util.regex.Pattern#compile 
  2090.      * compile}(</tt><i>regex</i><tt>).{@link 
  2091.      * java.util.regex.Pattern#matcher(java.lang.CharSequence) 
  2092.      * matcher}(</tt><i>str</i><tt>).{@link java.util.regex.Matcher#replaceFirst 
  2093.      * replaceFirst}(</tt><i>repl</i><tt>)</tt></blockquote> 
  2094.      * 
  2095.      *<p> 
  2096.      * Note that backslashes (<tt>\</tt>) and dollar signs (<tt>$</tt>) in the 
  2097.      * replacement string may cause the results to be different than if it were 
  2098.      * being treated as a literal replacement string; see 
  2099.      * {@link java.util.regex.Matcher#replaceFirst}. 
  2100.      * Use {@link java.util.regex.Matcher#quoteReplacement} to suppress the special 
  2101.      * meaning of these characters, if desired. 
  2102.      * 
  2103.      * @param   regex 
  2104.      *          the regular expression to which this string is to be matched 
  2105.      * @param   replacement 
  2106.      *          the string to be substituted for the first match 
  2107.      * 
  2108.      * @return  The resulting <tt>String</tt> 
  2109.      * 
  2110.      * @throws  PatternSyntaxException 
  2111.      *          if the regular expression's syntax is invalid 
  2112.      * 
  2113.      * @see java.util.regex.Pattern 
  2114.      * 
  2115.      * @since 1.4 
  2116.      * @spec JSR-51 
  2117.      */  
  2118.     public String replaceFirst(String regex, String replacement) {  
  2119.         return Pattern.compile(regex).matcher(this).replaceFirst(replacement);  
  2120.     }  
  2121.   
  2122.     /** 
  2123.      * Replaces each substring of this string that matches the given <a 
  2124.      * href="../util/regex/Pattern.html#sum">regular expression</a> with the 
  2125.      * given replacement. 
  2126.      * 
  2127.      * <p> An invocation of this method of the form 
  2128.      * <i>str</i><tt>.replaceAll(</tt><i>regex</i><tt>,</tt> <i>repl</i><tt>)</tt> 
  2129.      * yields exactly the same result as the expression 
  2130.      * 
  2131.      * <blockquote><tt> 
  2132.      * {@link java.util.regex.Pattern}.{@link java.util.regex.Pattern#compile 
  2133.      * compile}(</tt><i>regex</i><tt>).{@link 
  2134.      * java.util.regex.Pattern#matcher(java.lang.CharSequence) 
  2135.      * matcher}(</tt><i>str</i><tt>).{@link java.util.regex.Matcher#replaceAll 
  2136.      * replaceAll}(</tt><i>repl</i><tt>)</tt></blockquote> 
  2137.      * 
  2138.      *<p> 
  2139.      * Note that backslashes (<tt>\</tt>) and dollar signs (<tt>$</tt>) in the 
  2140.      * replacement string may cause the results to be different than if it were 
  2141.      * being treated as a literal replacement string; see 
  2142.      * {@link java.util.regex.Matcher#replaceAll Matcher.replaceAll}. 
  2143.      * Use {@link java.util.regex.Matcher#quoteReplacement} to suppress the special 
  2144.      * meaning of these characters, if desired. 
  2145.      * 
  2146.      * @param   regex 
  2147.      *          the regular expression to which this string is to be matched 
  2148.      * @param   replacement 
  2149.      *          the string to be substituted for each match 
  2150.      * 
  2151.      * @return  The resulting <tt>String</tt> 
  2152.      * 
  2153.      * @throws  PatternSyntaxException 
  2154.      *          if the regular expression's syntax is invalid 
  2155.      * 
  2156.      * @see java.util.regex.Pattern 
  2157.      * 
  2158.      * @since 1.4 
  2159.      * @spec JSR-51 
  2160.      */  
  2161.     public String replaceAll(String regex, String replacement) {  
  2162.         return Pattern.compile(regex).matcher(this).replaceAll(replacement);  
  2163.     }  
  2164.   
  2165.     /** 
  2166.      * Replaces each substring of this string that matches the literal target 
  2167.      * sequence with the specified literal replacement sequence. The 
  2168.      * replacement proceeds from the beginning of the string to the end, for 
  2169.      * example, replacing "aa" with "b" in the string "aaa" will result in 
  2170.      * "ba" rather than "ab". 
  2171.      * 
  2172.      * @param  target The sequence of char values to be replaced 
  2173.      * @param  replacement The replacement sequence of char values 
  2174.      * @return  The resulting string 
  2175.      * @throws NullPointerException if <code>target</code> or 
  2176.      *         <code>replacement</code> is <code>null</code>. 
  2177.      * @since 1.5 
  2178.      */  
  2179.     public String replace(CharSequence target, CharSequence replacement) {  
  2180.         return Pattern.compile(target.toString(), Pattern.LITERAL).matcher(  
  2181.                 this).replaceAll(Matcher.quoteReplacement(replacement.toString()));  
  2182.     }  
  2183.   
  2184.     /** 
  2185.      * Splits this string around matches of the given 
  2186.      * <a href="../util/regex/Pattern.html#sum">regular expression</a>. 
  2187.      * 
  2188.      * <p> The array returned by this method contains each substring of this 
  2189.      * string that is terminated by another substring that matches the given 
  2190.      * expression or is terminated by the end of the string.  The substrings in 
  2191.      * the array are in the order in which they occur in this string.  If the 
  2192.      * expression does not match any part of the input then the resulting array 
  2193.      * has just one element, namely this string. 
  2194.      * 
  2195.      * <p> The <tt>limit</tt> parameter controls the number of times the 
  2196.      * pattern is applied and therefore affects the length of the resulting 
  2197.      * array.  If the limit <i>n</i> is greater than zero then the pattern 
  2198.      * will be applied at most <i>n</i> - 1 times, the array's 
  2199.      * length will be no greater than <i>n</i>, and the array's last entry 
  2200.      * will contain all input beyond the last matched delimiter.  If <i>n</i> 
  2201.      * is non-positive then the pattern will be applied as many times as 
  2202.      * possible and the array can have any length.  If <i>n</i> is zero then 
  2203.      * the pattern will be applied as many times as possible, the array can 
  2204.      * have any length, and trailing empty strings will be discarded. 
  2205.      * 
  2206.      * <p> The string <tt>"boo:and:foo"</tt>, for example, yields the 
  2207.      * following results with these parameters: 
  2208.      * 
  2209.      * <blockquote><table cellpadding=1 cellspacing=0 summary="Split example showing regex, limit, and result"> 
  2210.      * <tr> 
  2211.      *     <th>Regex</th> 
  2212.      *     <th>Limit</th> 
  2213.      *     <th>Result</th> 
  2214.      * </tr> 
  2215.      * <tr><td align=center>:</td> 
  2216.      *     <td align=center>2</td> 
  2217.      *     <td><tt>{ "boo", "and:foo" }</tt></td></tr> 
  2218.      * <tr><td align=center>:</td> 
  2219.      *     <td align=center>5</td> 
  2220.      *     <td><tt>{ "boo", "and", "foo" }</tt></td></tr> 
  2221.      * <tr><td align=center>:</td> 
  2222.      *     <td align=center>-2</td> 
  2223.      *     <td><tt>{ "boo", "and", "foo" }</tt></td></tr> 
  2224.      * <tr><td align=center>o</td> 
  2225.      *     <td align=center>5</td> 
  2226.      *     <td><tt>{ "b", "", ":and:f", "", "" }</tt></td></tr> 
  2227.      * <tr><td align=center>o</td> 
  2228.      *     <td align=center>-2</td> 
  2229.      *     <td><tt>{ "b", "", ":and:f", "", "" }</tt></td></tr> 
  2230.      * <tr><td align=center>o</td> 
  2231.      *     <td align=center>0</td> 
  2232.      *     <td><tt>{ "b", "", ":and:f" }</tt></td></tr> 
  2233.      * </table></blockquote> 
  2234.      * 
  2235.      * <p> An invocation of this method of the form 
  2236.      * <i>str.</i><tt>split(</tt><i>regex</i><tt>,</tt> <i>n</i><tt>)</tt> 
  2237.      * yields the same result as the expression 
  2238.      * 
  2239.      * <blockquote> 
  2240.      * {@link java.util.regex.Pattern}.{@link java.util.regex.Pattern#compile 
  2241.      * compile}<tt>(</tt><i>regex</i><tt>)</tt>.{@link 
  2242.      * java.util.regex.Pattern#split(java.lang.CharSequence,int) 
  2243.      * split}<tt>(</tt><i>str</i><tt>,</tt> <i>n</i><tt>)</tt> 
  2244.      * </blockquote> 
  2245.      * 
  2246.      * 
  2247.      * @param  regex 
  2248.      *         the delimiting regular expression 
  2249.      * 
  2250.      * @param  limit 
  2251.      *         the result threshold, as described above 
  2252.      * 
  2253.      * @return  the array of strings computed by splitting this string 
  2254.      *          around matches of the given regular expression 
  2255.      * 
  2256.      * @throws  PatternSyntaxException 
  2257.      *          if the regular expression's syntax is invalid 
  2258.      * 
  2259.      * @see java.util.regex.Pattern 
  2260.      * 
  2261.      * @since 1.4 
  2262.      * @spec JSR-51 
  2263.      */  
  2264.     public String[] split(String regex, int limit) {  
  2265.         /* fastpath if the regex is a 
  2266.          (1)one-char String and this character is not one of the 
  2267.             RegEx's meta characters ".$|()[{^?*+\\", or 
  2268.          (2)two-char String and the first char is the backslash and 
  2269.             the second is not the ascii digit or ascii letter. 
  2270.          */  
  2271.         char ch = 0;  
  2272.         if (((regex.value.length == 1 &&  
  2273.              ".$|()[{^?*+\\".indexOf(ch = regex.charAt(0)) == -1) ||  
  2274.              (regex.length() == 2 &&  
  2275.               regex.charAt(0) == '\\' &&  
  2276.               (((ch = regex.charAt(1))-'0')|('9'-ch)) < 0 &&  
  2277.               ((ch-'a')|('z'-ch)) < 0 &&  
  2278.               ((ch-'A')|('Z'-ch)) < 0)) &&  
  2279.             (ch < Character.MIN_HIGH_SURROGATE ||  
  2280.              ch > Character.MAX_LOW_SURROGATE))  
  2281.         {  
  2282.             int off = 0;  
  2283.             int next = 0;  
  2284.             boolean limited = limit > 0;  
  2285.             ArrayList<String> list = new ArrayList<>();  
  2286.             while ((next = indexOf(ch, off)) != -1) {  
  2287.                 if (!limited || list.size() < limit - 1) {  
  2288.                     list.add(substring(off, next));  
  2289.                     off = next + 1;  
  2290.                 } else {    // last one  
  2291.                     //assert (list.size() == limit - 1);  
  2292.                     list.add(substring(off, value.length));  
  2293.                     off = value.length;  
  2294.                     break;  
  2295.                 }  
  2296.             }  
  2297.             // If no match was found, return this  
  2298.             if (off == 0)  
  2299.                 return new String[]{this};  
  2300.   
  2301.             // Add remaining segment  
  2302.             if (!limited || list.size() < limit)  
  2303.                 list.add(substring(off, value.length));  
  2304.   
  2305.             // Construct result  
  2306.             int resultSize = list.size();  
  2307.             if (limit == 0)  
  2308.                 while (resultSize > 0 && list.get(resultSize - 1).length() == 0)  
  2309.                     resultSize--;  
  2310.             String[] result = new String[resultSize];  
  2311.             return list.subList(0, resultSize).toArray(result);  
  2312.         }  
  2313.         return Pattern.compile(regex).split(this, limit);  
  2314.     }  
  2315.   
  2316.     /** 
  2317.      * Splits this string around matches of the given <a 
  2318.      * href="../util/regex/Pattern.html#sum">regular expression</a>. 
  2319.      * 
  2320.      * <p> This method works as if by invoking the two-argument {@link 
  2321.      * #split(String, int) split} method with the given expression and a limit 
  2322.      * argument of zero.  Trailing empty strings are therefore not included in 
  2323.      * the resulting array. 
  2324.      * 
  2325.      * <p> The string <tt>"boo:and:foo"</tt>, for example, yields the following 
  2326.      * results with these expressions: 
  2327.      * 
  2328.      * <blockquote><table cellpadding=1 cellspacing=0 summary="Split examples showing regex and result"> 
  2329.      * <tr> 
  2330.      *  <th>Regex</th> 
  2331.      *  <th>Result</th> 
  2332.      * </tr> 
  2333.      * <tr><td align=center>:</td> 
  2334.      *     <td><tt>{ "boo", "and", "foo" }</tt></td></tr> 
  2335.      * <tr><td align=center>o</td> 
  2336.      *     <td><tt>{ "b", "", ":and:f" }</tt></td></tr> 
  2337.      * </table></blockquote> 
  2338.      * 
  2339.      * 
  2340.      * @param  regex 
  2341.      *         the delimiting regular expression 
  2342.      * 
  2343.      * @return  the array of strings computed by splitting this string 
  2344.      *          around matches of the given regular expression 
  2345.      * 
  2346.      * @throws  PatternSyntaxException 
  2347.      *          if the regular expression's syntax is invalid 
  2348.      * 
  2349.      * @see java.util.regex.Pattern 
  2350.      * 
  2351.      * @since 1.4 
  2352.      * @spec JSR-51 
  2353.      */  
  2354.     public String[] split(String regex) {  
  2355.         return split(regex, 0);  
  2356.     }  
  2357.   
  2358.     /** 
  2359.      * Converts all of the characters in this <code>String</code> to lower 
  2360.      * case using the rules of the given <code>Locale</code>.  Case mapping is based 
  2361.      * on the Unicode Standard version specified by the {@link java.lang.Character Character} 
  2362.      * class. Since case mappings are not always 1:1 char mappings, the resulting 
  2363.      * <code>String</code> may be a different length than the original <code>String</code>. 
  2364.      * <p> 
  2365.      * Examples of lowercase  mappings are in the following table: 
  2366.      * <table border="1" summary="Lowercase mapping examples showing language code of locale, upper case, lower case, and description"> 
  2367.      * <tr> 
  2368.      *   <th>Language Code of Locale</th> 
  2369.      *   <th>Upper Case</th> 
  2370.      *   <th>Lower Case</th> 
  2371.      *   <th>Description</th> 
  2372.      * </tr> 
  2373.      * <tr> 
  2374.      *   <td>tr (Turkish)</td> 
  2375.      *   <td>\u0130</td> 
  2376.      *   <td>\u0069</td> 
  2377.      *   <td>capital letter I with dot above -> small letter i</td> 
  2378.      * </tr> 
  2379.      * <tr> 
  2380.      *   <td>tr (Turkish)</td> 
  2381.      *   <td>\u0049</td> 
  2382.      *   <td>\u0131</td> 
  2383.      *   <td>capital letter I -> small letter dotless i </td> 
  2384.      * </tr> 
  2385.      * <tr> 
  2386.      *   <td>(all)</td> 
  2387.      *   <td>French Fries</td> 
  2388.      *   <td>french fries</td> 
  2389.      *   <td>lowercased all chars in String</td> 
  2390.      * </tr> 
  2391.      * <tr> 
  2392.      *   <td>(all)</td> 
  2393.      *   <td><img src="doc-files/capiota.gif" alt="capiota"><img src="doc-files/capchi.gif" alt="capchi"> 
  2394.      *       <img src="doc-files/captheta.gif" alt="captheta"><img src="doc-files/capupsil.gif" alt="capupsil"> 
  2395.      *       <img src="doc-files/capsigma.gif" alt="capsigma"></td> 
  2396.      *   <td><img src="doc-files/iota.gif" alt="iota"><img src="doc-files/chi.gif" alt="chi"> 
  2397.      *       <img src="doc-files/theta.gif" alt="theta"><img src="doc-files/upsilon.gif" alt="upsilon"> 
  2398.      *       <img src="doc-files/sigma1.gif" alt="sigma"></td> 
  2399.      *   <td>lowercased all chars in String</td> 
  2400.      * </tr> 
  2401.      * </table> 
  2402.      * 
  2403.      * @param locale use the case transformation rules for this locale 
  2404.      * @return the <code>String</code>, converted to lowercase. 
  2405.      * @see     java.lang.String#toLowerCase() 
  2406.      * @see     java.lang.String#toUpperCase() 
  2407.      * @see     java.lang.String#toUpperCase(Locale) 
  2408.      * @since   1.1 
  2409.      */  
  2410.     public String toLowerCase(Locale locale) {  
  2411.         if (locale == null) {  
  2412.             throw new NullPointerException();  
  2413.         }  
  2414.   
  2415.         int firstUpper;  
  2416.         final int len = value.length;  
  2417.   
  2418.         /* Now check if there are any characters that need to be changed. */  
  2419.         scan: {  
  2420.             for (firstUpper = 0 ; firstUpper < len; ) {  
  2421.                 char c = value[firstUpper];  
  2422.                 if ((c >= Character.MIN_HIGH_SURROGATE)  
  2423.                         && (c <= Character.MAX_HIGH_SURROGATE)) {  
  2424.                     int supplChar = codePointAt(firstUpper);  
  2425.                     if (supplChar != Character.toLowerCase(supplChar)) {  
  2426.                         break scan;  
  2427.                     }  
  2428.                     firstUpper += Character.charCount(supplChar);  
  2429.                 } else {  
  2430.                     if (c != Character.toLowerCase(c)) {  
  2431.                         break scan;  
  2432.                     }  
  2433.                     firstUpper++;  
  2434.                 }  
  2435.             }  
  2436.             return this;  
  2437.         }  
  2438.   
  2439.         char[] result = new char[len];  
  2440.         int resultOffset = 0;  /* result may grow, so i+resultOffset 
  2441.                                 * is the write location in result */  
  2442.   
  2443.         /* Just copy the first few lowerCase characters. */  
  2444.         System.arraycopy(value, 0, result, 0, firstUpper);  
  2445.   
  2446.         String lang = locale.getLanguage();  
  2447.         boolean localeDependent =  
  2448.                 (lang == "tr" || lang == "az" || lang == "lt");  
  2449.         char[] lowerCharArray;  
  2450.         int lowerChar;  
  2451.         int srcChar;  
  2452.         int srcCount;  
  2453.         for (int i = firstUpper; i < len; i += srcCount) {  
  2454.             srcChar = (int)value[i];  
  2455.             if ((char)srcChar >= Character.MIN_HIGH_SURROGATE  
  2456.                     && (char)srcChar <= Character.MAX_HIGH_SURROGATE) {  
  2457.                 srcChar = codePointAt(i);  
  2458.                 srcCount = Character.charCount(srcChar);  
  2459.             } else {  
  2460.                 srcCount = 1;  
  2461.             }  
  2462.             if (localeDependent || srcChar == '\u03A3') { // GREEK CAPITAL LETTER SIGMA  
  2463.                 lowerChar = ConditionalSpecialCasing.toLowerCaseEx(this, i, locale);  
  2464.             } else if (srcChar == '\u0130') { // LATIN CAPITAL LETTER I DOT  
  2465.                 lowerChar = Character.ERROR;  
  2466.             } else {  
  2467.                 lowerChar = Character.toLowerCase(srcChar);  
  2468.             }  
  2469.             if ((lowerChar == Character.ERROR)  
  2470.                     || (lowerChar >= Character.MIN_SUPPLEMENTARY_CODE_POINT)) {  
  2471.                 if (lowerChar == Character.ERROR) {  
  2472.                     if (!localeDependent && srcChar == '\u0130') {  
  2473.                         lowerCharArray =  
  2474.                                 ConditionalSpecialCasing.toLowerCaseCharArray(this, i, Locale.ENGLISH);  
  2475.                     } else {  
  2476.                         lowerCharArray =  
  2477.                                 ConditionalSpecialCasing.toLowerCaseCharArray(this, i, locale);  
  2478.                     }  
  2479.                 } else if (srcCount == 2) {  
  2480.                     resultOffset += Character.toChars(lowerChar, result, i + resultOffset) - srcCount;  
  2481.                     continue;  
  2482.                 } else {  
  2483.                     lowerCharArray = Character.toChars(lowerChar);  
  2484.                 }  
  2485.   
  2486.                 /* Grow result if needed */  
  2487.                 int mapLen = lowerCharArray.length;  
  2488.                 if (mapLen > srcCount) {  
  2489.                     char[] result2 = new char[result.length + mapLen - srcCount];  
  2490.                     System.arraycopy(result, 0, result2, 0, i + resultOffset);  
  2491.                     result = result2;  
  2492.                 }  
  2493.                 for (int x = 0; x < mapLen; ++x) {  
  2494.                     result[i + resultOffset + x] = lowerCharArray[x];  
  2495.                 }  
  2496.                 resultOffset += (mapLen - srcCount);  
  2497.             } else {  
  2498.                 result[i + resultOffset] = (char)lowerChar;  
  2499.             }  
  2500.         }  
  2501.         return new String(result, 0, len + resultOffset);  
  2502.     }  
  2503.   
  2504.     /** 
  2505.      * Converts all of the characters in this <code>String</code> to lower 
  2506.      * case using the rules of the default locale. This is equivalent to calling 
  2507.      * <code>toLowerCase(Locale.getDefault())</code>. 
  2508.      * <p> 
  2509.      * <b>Note:</b> This method is locale sensitive, and may produce unexpected 
  2510.      * results if used for strings that are intended to be interpreted locale 
  2511.      * independently. 
  2512.      * Examples are programming language identifiers, protocol keys, and HTML 
  2513.      * tags. 
  2514.      * For instance, <code>"TITLE".toLowerCase()</code> in a Turkish locale 
  2515.      * returns <code>"t\u005Cu0131tle"</code>, where '\u005Cu0131' is the 
  2516.      * LATIN SMALL LETTER DOTLESS I character. 
  2517.      * To obtain correct results for locale insensitive strings, use 
  2518.      * <code>toLowerCase(Locale.ENGLISH)</code>. 
  2519.      * <p> 
  2520.      * @return  the <code>String</code>, converted to lowercase. 
  2521.      * @see     java.lang.String#toLowerCase(Locale) 
  2522.      */  
  2523.     public String toLowerCase() {  
  2524.         return toLowerCase(Locale.getDefault());  
  2525.     }  
  2526.   
  2527.     /** 
  2528.      * Converts all of the characters in this <code>String</code> to upper 
  2529.      * case using the rules of the given <code>Locale</code>. Case mapping is based 
  2530.      * on the Unicode Standard version specified by the {@link java.lang.Character Character} 
  2531.      * class. Since case mappings are not always 1:1 char mappings, the resulting 
  2532.      * <code>String</code> may be a different length than the original <code>String</code>. 
  2533.      * <p> 
  2534.      * Examples of locale-sensitive and 1:M case mappings are in the following table. 
  2535.      * <p> 
  2536.      * <table border="1" summary="Examples of locale-sensitive and 1:M case mappings. Shows Language code of locale, lower case, upper case, and description."> 
  2537.      * <tr> 
  2538.      *   <th>Language Code of Locale</th> 
  2539.      *   <th>Lower Case</th> 
  2540.      *   <th>Upper Case</th> 
  2541.      *   <th>Description</th> 
  2542.      * </tr> 
  2543.      * <tr> 
  2544.      *   <td>tr (Turkish)</td> 
  2545.      *   <td>\u0069</td> 
  2546.      *   <td>\u0130</td> 
  2547.      *   <td>small letter i -> capital letter I with dot above</td> 
  2548.      * </tr> 
  2549.      * <tr> 
  2550.      *   <td>tr (Turkish)</td> 
  2551.      *   <td>\u0131</td> 
  2552.      *   <td>\u0049</td> 
  2553.      *   <td>small letter dotless i -> capital letter I</td> 
  2554.      * </tr> 
  2555.      * <tr> 
  2556.      *   <td>(all)</td> 
  2557.      *   <td>\u00df</td> 
  2558.      *   <td>\u0053 \u0053</td> 
  2559.      *   <td>small letter sharp s -> two letters: SS</td> 
  2560.      * </tr> 
  2561.      * <tr> 
  2562.      *   <td>(all)</td> 
  2563.      *   <td>Fahrvergnügen</td> 
  2564.      *   <td>FAHRVERGNÜGEN</td> 
  2565.      *   <td></td> 
  2566.      * </tr> 
  2567.      * </table> 
  2568.      * @param locale use the case transformation rules for this locale 
  2569.      * @return the <code>String</code>, converted to uppercase. 
  2570.      * @see     java.lang.String#toUpperCase() 
  2571.      * @see     java.lang.String#toLowerCase() 
  2572.      * @see     java.lang.String#toLowerCase(Locale) 
  2573.      * @since   1.1 
  2574.      */  
  2575.     public String toUpperCase(Locale locale) {  
  2576.         if (locale == null) {  
  2577.             throw new NullPointerException();  
  2578.         }  
  2579.   
  2580.         int firstLower;  
  2581.         final int len = value.length;  
  2582.   
  2583.         /* Now check if there are any characters that need to be changed. */  
  2584.         scan: {  
  2585.            for (firstLower = 0 ; firstLower < len; ) {  
  2586.                 int c = (int)value[firstLower];  
  2587.                 int srcCount;  
  2588.                 if ((c >= Character.MIN_HIGH_SURROGATE)  
  2589.                         && (c <= Character.MAX_HIGH_SURROGATE)) {  
  2590.                     c = codePointAt(firstLower);  
  2591.                     srcCount = Character.charCount(c);  
  2592.                 } else {  
  2593.                     srcCount = 1;  
  2594.                 }  
  2595.                 int upperCaseChar = Character.toUpperCaseEx(c);  
  2596.                 if ((upperCaseChar == Character.ERROR)  
  2597.                         || (c != upperCaseChar)) {  
  2598.                     break scan;  
  2599.                 }  
  2600.                 firstLower += srcCount;  
  2601.             }  
  2602.             return this;  
  2603.         }  
  2604.   
  2605.         char[] result = new char[len]; /* may grow */  
  2606.         int resultOffset = 0;  /* result may grow, so i+resultOffset 
  2607.          * is the write location in result */  
  2608.   
  2609.         /* Just copy the first few upperCase characters. */  
  2610.         System.arraycopy(value, 0, result, 0, firstLower);  
  2611.   
  2612.         String lang = locale.getLanguage();  
  2613.         boolean localeDependent =  
  2614.                 (lang == "tr" || lang == "az" || lang == "lt");  
  2615.         char[] upperCharArray;  
  2616.         int upperChar;  
  2617.         int srcChar;  
  2618.         int srcCount;  
  2619.         for (int i = firstLower; i < len; i += srcCount) {  
  2620.             srcChar = (int)value[i];  
  2621.             if ((char)srcChar >= Character.MIN_HIGH_SURROGATE &&  
  2622.                 (char)srcChar <= Character.MAX_HIGH_SURROGATE) {  
  2623.                 srcChar = codePointAt(i);  
  2624.                 srcCount = Character.charCount(srcChar);  
  2625.             } else {  
  2626.                 srcCount = 1;  
  2627.             }  
  2628.             if (localeDependent) {  
  2629.                 upperChar = ConditionalSpecialCasing.toUpperCaseEx(this, i, locale);  
  2630.             } else {  
  2631.                 upperChar = Character.toUpperCaseEx(srcChar);  
  2632.             }  
  2633.             if ((upperChar == Character.ERROR)  
  2634.                     || (upperChar >= Character.MIN_SUPPLEMENTARY_CODE_POINT)) {  
  2635.                 if (upperChar == Character.ERROR) {  
  2636.                     if (localeDependent) {  
  2637.                         upperCharArray =  
  2638.                                 ConditionalSpecialCasing.toUpperCaseCharArray(this, i, locale);  
  2639.                     } else {  
  2640.                         upperCharArray = Character.toUpperCaseCharArray(srcChar);  
  2641.                     }  
  2642.                 } else if (srcCount == 2) {  
  2643.                     resultOffset += Character.toChars(upperChar, result, i + resultOffset) - srcCount;  
  2644.                     continue;  
  2645.                 } else {  
  2646.                     upperCharArray = Character.toChars(upperChar);  
  2647.                 }  
  2648.   
  2649.                 /* Grow result if needed */  
  2650.                 int mapLen = upperCharArray.length;  
  2651.                 if (mapLen > srcCount) {  
  2652.                     char[] result2 = new char[result.length + mapLen - srcCount];  
  2653.                     System.arraycopy(result, 0, result2, 0, i + resultOffset);  
  2654.                     result = result2;  
  2655.                 }  
  2656.                 for (int x = 0; x < mapLen; ++x) {  
  2657.                     result[i + resultOffset + x] = upperCharArray[x];  
  2658.                 }  
  2659.                 resultOffset += (mapLen - srcCount);  
  2660.             } else {  
  2661.                 result[i + resultOffset] = (char)upperChar;  
  2662.             }  
  2663.         }  
  2664.         return new String(result, 0, len + resultOffset);  
  2665.     }  
  2666.   
  2667.     /** 
  2668.      * Converts all of the characters in this <code>String</code> to upper 
  2669.      * case using the rules of the default locale. This method is equivalent to 
  2670.      * <code>toUpperCase(Locale.getDefault())</code>. 
  2671.      * <p> 
  2672.      * <b>Note:</b> This method is locale sensitive, and may produce unexpected 
  2673.      * results if used for strings that are intended to be interpreted locale 
  2674.      * independently. 
  2675.      * Examples are programming language identifiers, protocol keys, and HTML 
  2676.      * tags. 
  2677.      * For instance, <code>"title".toUpperCase()</code> in a Turkish locale 
  2678.      * returns <code>"T\u005Cu0130TLE"</code>, where '\u005Cu0130' is the 
  2679.      * LATIN CAPITAL LETTER I WITH DOT ABOVE character. 
  2680.      * To obtain correct results for locale insensitive strings, use 
  2681.      * <code>toUpperCase(Locale.ENGLISH)</code>. 
  2682.      * <p> 
  2683.      * @return  the <code>String</code>, converted to uppercase. 
  2684.      * @see     java.lang.String#toUpperCase(Locale) 
  2685.      */  
  2686.     public String toUpperCase() {  
  2687.         return toUpperCase(Locale.getDefault());  
  2688.     }  
  2689.   
  2690.     /** 
  2691.      * Returns a copy of the string, with leading and trailing whitespace 
  2692.      * omitted. 
  2693.      * <p> 
  2694.      * If this <code>String</code> object represents an empty character 
  2695.      * sequence, or the first and last characters of character sequence 
  2696.      * represented by this <code>String</code> object both have codes 
  2697.      * greater than <code>'\u0020'</code> (the space character), then a 
  2698.      * reference to this <code>String</code> object is returned. 
  2699.      * <p> 
  2700.      * Otherwise, if there is no character with a code greater than 
  2701.      * <code>'\u0020'</code> in the string, then a new 
  2702.      * <code>String</code> object representing an empty string is created 
  2703.      * and returned. 
  2704.      * <p> 
  2705.      * Otherwise, let <i>k</i> be the index of the first character in the 
  2706.      * string whose code is greater than <code>'\u0020'</code>, and let 
  2707.      * <i>m</i> be the index of the last character in the string whose code 
  2708.      * is greater than <code>'\u0020'</code>. A new <code>String</code> 
  2709.      * object is created, representing the substring of this string that 
  2710.      * begins with the character at index <i>k</i> and ends with the 
  2711.      * character at index <i>m</i>-that is, the result of 
  2712.      * <code>this.substring(<i>k</i>, <i>m</i>+1)</code>. 
  2713.      * <p> 
  2714.      * This method may be used to trim whitespace (as defined above) from 
  2715.      * the beginning and end of a string. 
  2716.      * 
  2717.      * @return  A copy of this string with leading and trailing white 
  2718.      *          space removed, or this string if it has no leading or 
  2719.      *          trailing white space. 
  2720.      */  
  2721.     public String trim() {  
  2722.         int len = value.length;  
  2723.         int st = 0;  
  2724.         char[] val = value;    /* avoid getfield opcode */  
  2725.   
  2726.         while ((st < len) && (val[st] <= ' ')) {  
  2727.             st++;  
  2728.         }  
  2729.         while ((st < len) && (val[len - 1] <= ' ')) {  
  2730.             len--;  
  2731.         }  
  2732.         return ((st > 0) || (len < value.length)) ? substring(st, len) : this;  
  2733.     }  
  2734.   
  2735.     /** 
  2736.      * This object (which is already a string!) is itself returned. 
  2737.      * 
  2738.      * @return  the string itself. 
  2739.      */  
  2740.     public String toString() {  
  2741.         return this;  
  2742.     }  
  2743.   
  2744.     /** 
  2745.      * Converts this string to a new character array. 
  2746.      * 
  2747.      * @return  a newly allocated character array whose length is the length 
  2748.      *          of this string and whose contents are initialized to contain 
  2749.      *          the character sequence represented by this string. 
  2750.      */  
  2751.     public char[] toCharArray() {  
  2752.         // Cannot use Arrays.copyOf because of class initialization order issues  
  2753.         char result[] = new char[value.length];  
  2754.         System.arraycopy(value, 0, result, 0, value.length);  
  2755.         return result;  
  2756.     }  
  2757.   
  2758.     /** 
  2759.      * Returns a formatted string using the specified format string and 
  2760.      * arguments. 
  2761.      * 
  2762.      * <p> The locale always used is the one returned by {@link 
  2763.      * java.util.Locale#getDefault() Locale.getDefault()}. 
  2764.      * 
  2765.      * @param  format 
  2766.      *         A <a href="../util/Formatter.html#syntax">format string</a> 
  2767.      * 
  2768.      * @param  args 
  2769.      *         Arguments referenced by the format specifiers in the format 
  2770.      *         string.  If there are more arguments than format specifiers, the 
  2771.      *         extra arguments are ignored.  The number of arguments is 
  2772.      *         variable and may be zero.  The maximum number of arguments is 
  2773.      *         limited by the maximum dimension of a Java array as defined by 
  2774.      *         <cite>The Java™ Virtual Machine Specification</cite>. 
  2775.      *         The behaviour on a 
  2776.      *         <tt>null</tt> argument depends on the <a 
  2777.      *         href="../util/Formatter.html#syntax">conversion</a>. 
  2778.      * 
  2779.      * @throws  IllegalFormatException 
  2780.      *          If a format string contains an illegal syntax, a format 
  2781.      *          specifier that is incompatible with the given arguments, 
  2782.      *          insufficient arguments given the format string, or other 
  2783.      *          illegal conditions.  For specification of all possible 
  2784.      *          formatting errors, see the <a 
  2785.      *          href="../util/Formatter.html#detail">Details</a> section of the 
  2786.      *          formatter class specification. 
  2787.      * 
  2788.      * @throws  NullPointerException 
  2789.      *          If the <tt>format</tt> is <tt>null</tt> 
  2790.      * 
  2791.      * @return  A formatted string 
  2792.      * 
  2793.      * @see  java.util.Formatter 
  2794.      * @since  1.5 
  2795.      */  
  2796.     public static String format(String format, Object... args) {  
  2797.         return new Formatter().format(format, args).toString();  
  2798.     }  
  2799.   
  2800.     /** 
  2801.      * Returns a formatted string using the specified locale, format string, 
  2802.      * and arguments. 
  2803.      * 
  2804.      * @param  l 
  2805.      *         The {@linkplain java.util.Locale locale} to apply during 
  2806.      *         formatting.  If <tt>l</tt> is <tt>null</tt> then no localization 
  2807.      *         is applied. 
  2808.      * 
  2809.      * @param  format 
  2810.      *         A <a href="../util/Formatter.html#syntax">format string</a> 
  2811.      * 
  2812.      * @param  args 
  2813.      *         Arguments referenced by the format specifiers in the format 
  2814.      *         string.  If there are more arguments than format specifiers, the 
  2815.      *         extra arguments are ignored.  The number of arguments is 
  2816.      *         variable and may be zero.  The maximum number of arguments is 
  2817.      *         limited by the maximum dimension of a Java array as defined by 
  2818.      *         <cite>The Java™ Virtual Machine Specification</cite>. 
  2819.      *         The behaviour on a 
  2820.      *         <tt>null</tt> argument depends on the <a 
  2821.      *         href="../util/Formatter.html#syntax">conversion</a>. 
  2822.      * 
  2823.      * @throws  IllegalFormatException 
  2824.      *          If a format string contains an illegal syntax, a format 
  2825.      *          specifier that is incompatible with the given arguments, 
  2826.      *          insufficient arguments given the format string, or other 
  2827.      *          illegal conditions.  For specification of all possible 
  2828.      *          formatting errors, see the <a 
  2829.      *          href="../util/Formatter.html#detail">Details</a> section of the 
  2830.      *          formatter class specification 
  2831.      * 
  2832.      * @throws  NullPointerException 
  2833.      *          If the <tt>format</tt> is <tt>null</tt> 
  2834.      * 
  2835.      * @return  A formatted string 
  2836.      * 
  2837.      * @see  java.util.Formatter 
  2838.      * @since  1.5 
  2839.      */  
  2840.     public static String format(Locale l, String format, Object... args) {  
  2841.         return new Formatter(l).format(format, args).toString();  
  2842.     }  
  2843.   
  2844.     /** 
  2845.      * Returns the string representation of the <code>Object</code> argument. 
  2846.      * 
  2847.      * @param   obj   an <code>Object</code>. 
  2848.      * @return  if the argument is <code>null</code>, then a string equal to 
  2849.      *          <code>"null"</code>; otherwise, the value of 
  2850.      *          <code>obj.toString()</code> is returned. 
  2851.      * @see     java.lang.Object#toString() 
  2852.      */  
  2853.     public static String valueOf(Object obj) {  
  2854.         return (obj == null) ? "null" : obj.toString();  
  2855.     }  
  2856.   
  2857.     /** 
  2858.      * Returns the string representation of the <code>char</code> array 
  2859.      * argument. The contents of the character array are copied; subsequent 
  2860.      * modification of the character array does not affect the newly 
  2861.      * created string. 
  2862.      * 
  2863.      * @param   data   a <code>char</code> array. 
  2864.      * @return  a newly allocated string representing the same sequence of 
  2865.      *          characters contained in the character array argument. 
  2866.      */  
  2867.     public static String valueOf(char data[]) {  
  2868.         return new String(data);  
  2869.     }  
  2870.   
  2871.     /** 
  2872.      * Returns the string representation of a specific subarray of the 
  2873.      * <code>char</code> array argument. 
  2874.      * <p> 
  2875.      * The <code>offset</code> argument is the index of the first 
  2876.      * character of the subarray. The <code>count</code> argument 
  2877.      * specifies the length of the subarray. The contents of the subarray 
  2878.      * are copied; subsequent modification of the character array does not 
  2879.      * affect the newly created string. 
  2880.      * 
  2881.      * @param   data     the character array. 
  2882.      * @param   offset   the initial offset into the value of the 
  2883.      *                  <code>String</code>. 
  2884.      * @param   count    the length of the value of the <code>String</code>. 
  2885.      * @return  a string representing the sequence of characters contained 
  2886.      *          in the subarray of the character array argument. 
  2887.      * @exception IndexOutOfBoundsException if <code>offset</code> is 
  2888.      *          negative, or <code>count</code> is negative, or 
  2889.      *          <code>offset+count</code> is larger than 
  2890.      *          <code>data.length</code>. 
  2891.      */  
  2892.     public static String valueOf(char data[], int offset, int count) {  
  2893.         return new String(data, offset, count);  
  2894.     }  
  2895.   
  2896.     /** 
  2897.      * Returns a String that represents the character sequence in the 
  2898.      * array specified. 
  2899.      * 
  2900.      * @param   data     the character array. 
  2901.      * @param   offset   initial offset of the subarray. 
  2902.      * @param   count    length of the subarray. 
  2903.      * @return  a <code>String</code> that contains the characters of the 
  2904.      *          specified subarray of the character array. 
  2905.      */  
  2906.     public static String copyValueOf(char data[], int offset, int count) {  
  2907.         // All public String constructors now copy the data.  
  2908.         return new String(data, offset, count);  
  2909.     }  
  2910.   
  2911.     /** 
  2912.      * Returns a String that represents the character sequence in the 
  2913.      * array specified. 
  2914.      * 
  2915.      * @param   data   the character array. 
  2916.      * @return  a <code>String</code> that contains the characters of the 
  2917.      *          character array. 
  2918.      */  
  2919.     public static String copyValueOf(char data[]) {  
  2920.         return new String(data);  
  2921.     }  
  2922.   
  2923.     /** 
  2924.      * Returns the string representation of the <code>boolean</code> argument. 
  2925.      * 
  2926.      * @param   b   a <code>boolean</code>. 
  2927.      * @return  if the argument is <code>true</code>, a string equal to 
  2928.      *          <code>"true"</code> is returned; otherwise, a string equal to 
  2929.      *          <code>"false"</code> is returned. 
  2930.      */  
  2931.     public static String valueOf(boolean b) {  
  2932.         return b ? "true" : "false";  
  2933.     }  
  2934.   
  2935.     /** 
  2936.      * Returns the string representation of the <code>char</code> 
  2937.      * argument. 
  2938.      * 
  2939.      * @param   c   a <code>char</code>. 
  2940.      * @return  a string of length <code>1</code> containing 
  2941.      *          as its single character the argument <code>c</code>. 
  2942.      */  
  2943.     public static String valueOf(char c) {  
  2944.         char data[] = {c};  
  2945.         return new String(data, true);  
  2946.     }  
  2947.   
  2948.     /** 
  2949.      * Returns the string representation of the <code>int</code> argument. 
  2950.      * <p> 
  2951.      * The representation is exactly the one returned by the 
  2952.      * <code>Integer.toString</code> method of one argument. 
  2953.      * 
  2954.      * @param   i   an <code>int</code>. 
  2955.      * @return  a string representation of the <code>int</code> argument. 
  2956.      * @see     java.lang.Integer#toString(int, int) 
  2957.      */  
  2958.     public static String valueOf(int i) {  
  2959.         return Integer.toString(i);  
  2960.     }  
  2961.   
  2962.     /** 
  2963.      * Returns the string representation of the <code>long</code> argument. 
  2964.      * <p> 
  2965.      * The representation is exactly the one returned by the 
  2966.      * <code>Long.toString</code> method of one argument. 
  2967.      * 
  2968.      * @param   l   a <code>long</code>. 
  2969.      * @return  a string representation of the <code>long</code> argument. 
  2970.      * @see     java.lang.Long#toString(long) 
  2971.      */  
  2972.     public static String valueOf(long l) {  
  2973.         return Long.toString(l);  
  2974.     }  
  2975.   
  2976.     /** 
  2977.      * Returns the string representation of the <code>float</code> argument. 
  2978.      * <p> 
  2979.      * The representation is exactly the one returned by the 
  2980.      * <code>Float.toString</code> method of one argument. 
  2981.      * 
  2982.      * @param   f   a <code>float</code>. 
  2983.      * @return  a string representation of the <code>float</code> argument. 
  2984.      * @see     java.lang.Float#toString(float) 
  2985.      */  
  2986.     public static String valueOf(float f) {  
  2987.         return Float.toString(f);  
  2988.     }  
  2989.   
  2990.     /** 
  2991.      * Returns the string representation of the <code>double</code> argument. 
  2992.      * <p> 
  2993.      * The representation is exactly the one returned by the 
  2994.      * <code>Double.toString</code> method of one argument. 
  2995.      * 
  2996.      * @param   d   a <code>double</code>. 
  2997.      * @return  a  string representation of the <code>double</code> argument. 
  2998.      * @see     java.lang.Double#toString(double) 
  2999.      */  
  3000.     public static String valueOf(double d) {  
  3001.         return Double.toString(d);  
  3002.     }  
  3003.   
  3004.     /** 
  3005.      * Returns a canonical representation for the string object. 
  3006.      * <p> 
  3007.      * A pool of strings, initially empty, is maintained privately by the 
  3008.      * class <code>String</code>. 
  3009.      * <p> 
  3010.      * When the intern method is invoked, if the pool already contains a 
  3011.      * string equal to this <code>String</code> object as determined by 
  3012.      * the {@link #equals(Object)} method, then the string from the pool is 
  3013.      * returned. Otherwise, this <code>String</code> object is added to the 
  3014.      * pool and a reference to this <code>String</code> object is returned. 
  3015.      * <p> 
  3016.      * It follows that for any two strings <code>s</code> and <code>t</code>, 
  3017.      * <code>s.intern() == t.intern()</code> is <code>true</code> 
  3018.      * if and only if <code>s.equals(t)</code> is <code>true</code>. 
  3019.      * <p> 
  3020.      * All literal strings and string-valued constant expressions are 
  3021.      * interned. String literals are defined in section 3.10.5 of the 
  3022.      * <cite>The Java™ Language Specification</cite>. 
  3023.      * 
  3024.      * @return  a string that has the same contents as this string, but is 
  3025.      *          guaranteed to be from a pool of unique strings. 
  3026.      */  
  3027.     public native String intern();  
  3028.   
  3029.     /** 
  3030.      * Seed value used for each alternative hash calculated. 
  3031.      */  
  3032.     private static final int HASHING_SEED;  
  3033.   
  3034.     static {  
  3035.         long nanos = System.nanoTime();  
  3036.         long now = System.currentTimeMillis();  
  3037.         int SEED_MATERIAL[] = {  
  3038.                 System.identityHashCode(String.class),  
  3039.                 System.identityHashCode(System.class),  
  3040.                 (int) (nanos >>> 32),  
  3041.                 (int) nanos,  
  3042.                 (int) (now >>> 32),  
  3043.                 (int) now,  
  3044.                 (int) (System.nanoTime() >>> 2)  
  3045.         };  
  3046.   
  3047.         // Use murmur3 to scramble the seeding material.  
  3048.         // Inline implementation to avoid loading classes  
  3049.         int h1 = 0;  
  3050.   
  3051.         // body  
  3052.         for (int k1 : SEED_MATERIAL) {  
  3053.             k1 *= 0xcc9e2d51;  
  3054.             k1 = (k1 << 15) | (k1 >>> 17);  
  3055.             k1 *= 0x1b873593;  
  3056.   
  3057.             h1 ^= k1;  
  3058.             h1 = (h1 << 13) | (h1 >>> 19);  
  3059.             h1 = h1 * 5 + 0xe6546b64;  
  3060.         }  
  3061.   
  3062.         // tail (always empty, as body is always 32-bit chunks)  
  3063.   
  3064.         // finalization  
  3065.   
  3066.         h1 ^= SEED_MATERIAL.length * 4;  
  3067.   
  3068.         // finalization mix force all bits of a hash block to avalanche  
  3069.         h1 ^= h1 >>> 16;  
  3070.         h1 *= 0x85ebca6b;  
  3071.         h1 ^= h1 >>> 13;  
  3072.         h1 *= 0xc2b2ae35;  
  3073.         h1 ^= h1 >>> 16;  
  3074.   
  3075.         HASHING_SEED = h1;  
  3076.     }  
  3077.   
  3078.     /** 
  3079.      * Cached value of the alternative hashing algorithm result 
  3080.      */  
  3081.     private transient int hash32 = 0;  
  3082.   
  3083.     /** 
  3084.      * Calculates a 32-bit hash value for this string. 
  3085.      * 
  3086.      * @return a 32-bit hash value for this string. 
  3087.      */  
  3088.     int hash32() {  
  3089.         int h = hash32;  
  3090.         if (0 == h) {  
  3091.            // harmless data race on hash32 here.  
  3092.            h = sun.misc.Hashing.murmur3_32(HASHING_SEED, value, 0, value.length);  
  3093.   
  3094.            // ensure result is not zero to avoid recalcing  
  3095.            h = (0 != h) ? h : 1;  
  3096.   
  3097.            hash32 = h;  
  3098.         }  
  3099.   
  3100.         return h;  
  3101.     }  
  3102.   
  3103. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值