src/share/classes/java/lang/String.java

Print this page




  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang;
  27 
  28 import java.io.ObjectStreamClass;
  29 import java.io.ObjectStreamField;
  30 import java.io.UnsupportedEncodingException;
  31 import java.nio.charset.Charset;
  32 import java.util.ArrayList;
  33 import java.util.Arrays;
  34 import java.util.Comparator;
  35 import java.util.Formatter;
  36 import java.util.Locale;
  37 import java.util.regex.Matcher;
  38 import java.util.regex.Pattern;
  39 import java.util.regex.PatternSyntaxException;
  40 
  41 /**
  42  * The <code>String</code> class represents character strings. All
  43  * string literals in Java programs, such as <code>"abc"</code>, are
  44  * implemented as instances of this class.
  45  * <p>
  46  * Strings are constant; their values cannot be changed after they
  47  * are created. String buffers support mutable strings.
  48  * Because String objects are immutable they can be shared. For example:
  49  * <p><blockquote><pre>
  50  *     String str = "abc";
  51  * </pre></blockquote><p>
  52  * is equivalent to:
  53  * <p><blockquote><pre>
  54  *     char data[] = {'a', 'b', 'c'};
  55  *     String str = new String(data);
  56  * </pre></blockquote><p>
  57  * Here are some more examples of how strings can be used:
  58  * <p><blockquote><pre>
  59  *     System.out.println("abc");
  60  *     String cde = "cde";
  61  *     System.out.println("abc" + cde);
  62  *     String c = "abc".substring(2,3);
  63  *     String d = cde.substring(1, 2);
  64  * </pre></blockquote>
  65  * <p>
  66  * The class <code>String</code> includes methods for examining
  67  * individual characters of the sequence, for comparing strings, for
  68  * searching strings, for extracting substrings, and for creating a
  69  * copy of a string with all characters translated to uppercase or to
  70  * lowercase. Case mapping is based on the Unicode Standard version
  71  * specified by the {@link java.lang.Character Character} class.
  72  * <p>
  73  * The Java language provides special support for the string
  74  * concatenation operator (&nbsp;+&nbsp;), and for conversion of
  75  * other objects to strings. String concatenation is implemented
  76  * through the <code>StringBuilder</code>(or <code>StringBuffer</code>)
  77  * class and its <code>append</code> method.
  78  * String conversions are implemented through the method
  79  * <code>toString</code>, defined by <code>Object</code> and
  80  * inherited by all classes in Java. For additional information on
  81  * string concatenation and conversion, see Gosling, Joy, and Steele,
  82  * <i>The Java Language Specification</i>.
  83  *
  84  * <p> Unless otherwise noted, passing a <tt>null</tt> argument to a constructor
  85  * or method in this class will cause a {@link NullPointerException} to be
  86  * thrown.
  87  *
  88  * <p>A <code>String</code> represents a string in the UTF-16 format
  89  * in which <em>supplementary characters</em> are represented by <em>surrogate
  90  * pairs</em> (see the section <a href="Character.html#unicode">Unicode
  91  * Character Representations</a> in the <code>Character</code> class for
  92  * more information).
  93  * Index values refer to <code>char</code> code units, so a supplementary
  94  * character uses two positions in a <code>String</code>.
  95  * <p>The <code>String</code> class provides methods for dealing with
  96  * Unicode code points (i.e., characters), in addition to those for
  97  * dealing with Unicode code units (i.e., <code>char</code> values).
  98  *
  99  * @author  Lee Boynton
 100  * @author  Arthur van Hoff
 101  * @author  Martin Buchholz
 102  * @author  Ulf Zibis
 103  * @see     java.lang.Object#toString()
 104  * @see     java.lang.StringBuffer
 105  * @see     java.lang.StringBuilder
 106  * @see     java.nio.charset.Charset
 107  * @since   JDK1.0
 108  */
 109 
 110 public final class String
 111     implements java.io.Serializable, Comparable<String>, CharSequence
 112 {
 113     /** The value is used for character storage. */
 114     private final char value[];
 115 
 116     /** The offset is the first index of the storage that is used. */
 117     private final int offset;
 118 
 119     /** The count is the number of characters in the String. */
 120     private final int count;
 121 
 122     /** Cache the hash code for the string */
 123     private int hash; // Default to 0
 124 
 125     /** use serialVersionUID from JDK 1.0.2 for interoperability */
 126     private static final long serialVersionUID = -6849794470754667710L;
 127 
 128     /**
 129      * Class String is special cased within the Serialization Stream Protocol.
 130      *
 131      * A String instance is written initially into an ObjectOutputStream in the
 132      * following format:
 133      * <pre>
 134      *      <code>TC_STRING</code> (utf String)
 135      * </pre>
 136      * The String is written by method <code>DataOutput.writeUTF</code>.
 137      * A new handle is generated to  refer to all future references to the
 138      * string instance within the stream.
 139      */
 140     private static final ObjectStreamField[] serialPersistentFields =
 141         new ObjectStreamField[0];
 142 
 143     /**
 144      * Initializes a newly created {@code String} object so that it represents
 145      * an empty character sequence.  Note that use of this constructor is
 146      * unnecessary since Strings are immutable.
 147      */
 148     public String() {
 149         this.offset = 0;
 150         this.count = 0;
 151         this.value = new char[0];
 152     }
 153 
 154     /**
 155      * Initializes a newly created {@code String} object so that it represents
 156      * the same sequence of characters as the argument; in other words, the


 656      * @return  the length of the sequence of characters represented by this
 657      *          object.
 658      */
 659     public int length() {
 660         return count;
 661     }
 662 
 663     /**
 664      * Returns <tt>true</tt> if, and only if, {@link #length()} is <tt>0</tt>.
 665      *
 666      * @return <tt>true</tt> if {@link #length()} is <tt>0</tt>, otherwise
 667      * <tt>false</tt>
 668      *
 669      * @since 1.6
 670      */
 671     public boolean isEmpty() {
 672         return count == 0;
 673     }
 674 
 675     /**
 676      * Returns the <code>char</code> value at the
 677      * specified index. An index ranges from <code>0</code> to
 678      * <code>length() - 1</code>. The first <code>char</code> value of the sequence
 679      * is at index <code>0</code>, the next at index <code>1</code>,
 680      * and so on, as for array indexing.
 681      *
 682      * <p>If the <code>char</code> value specified by the index is a
 683      * <a href="Character.html#unicode">surrogate</a>, the surrogate
 684      * value is returned.
 685      *
 686      * @param      index   the index of the <code>char</code> value.
 687      * @return     the <code>char</code> value at the specified index of this string.
 688      *             The first <code>char</code> value is at index <code>0</code>.
 689      * @exception  IndexOutOfBoundsException  if the <code>index</code>
 690      *             argument is negative or not less than the length of this
 691      *             string.
 692      */
 693     public char charAt(int index) {
 694         if ((index < 0) || (index >= count)) {
 695             throw new StringIndexOutOfBoundsException(index);
 696         }
 697         return value[index + offset];
 698     }
 699 
 700     /**
 701      * Returns the character (Unicode code point) at the specified
 702      * index. The index refers to <code>char</code> values
 703      * (Unicode code units) and ranges from <code>0</code> to
 704      * {@link #length()}<code> - 1</code>.
 705      *
 706      * <p> If the <code>char</code> value specified at the given index
 707      * is in the high-surrogate range, the following index is less
 708      * than the length of this <code>String</code>, and the
 709      * <code>char</code> value at the following index is in the
 710      * low-surrogate range, then the supplementary code point
 711      * corresponding to this surrogate pair is returned. Otherwise,
 712      * the <code>char</code> value at the given index is returned.
 713      *
 714      * @param      index the index to the <code>char</code> values
 715      * @return     the code point value of the character at the
 716      *             <code>index</code>
 717      * @exception  IndexOutOfBoundsException  if the <code>index</code>
 718      *             argument is negative or not less than the length of this
 719      *             string.
 720      * @since      1.5
 721      */
 722     public int codePointAt(int index) {
 723         if ((index < 0) || (index >= count)) {
 724             throw new StringIndexOutOfBoundsException(index);
 725         }
 726         return Character.codePointAtImpl(value, offset + index, offset + count);
 727     }
 728 
 729     /**
 730      * Returns the character (Unicode code point) before the specified
 731      * index. The index refers to <code>char</code> values
 732      * (Unicode code units) and ranges from <code>1</code> to {@link
 733      * CharSequence#length() length}.
 734      *
 735      * <p> If the <code>char</code> value at <code>(index - 1)</code>
 736      * is in the low-surrogate range, <code>(index - 2)</code> is not
 737      * negative, and the <code>char</code> value at <code>(index -
 738      * 2)</code> is in the high-surrogate range, then the
 739      * supplementary code point value of the surrogate pair is
 740      * returned. If the <code>char</code> value at <code>index -
 741      * 1</code> is an unpaired low-surrogate or a high-surrogate, the
 742      * surrogate value is returned.
 743      *
 744      * @param     index the index following the code point that should be returned
 745      * @return    the Unicode code point value before the given index.
 746      * @exception IndexOutOfBoundsException if the <code>index</code>
 747      *            argument is less than 1 or greater than the length
 748      *            of this string.
 749      * @since     1.5
 750      */
 751     public int codePointBefore(int index) {
 752         int i = index - 1;
 753         if ((i < 0) || (i >= count)) {
 754             throw new StringIndexOutOfBoundsException(index);
 755         }
 756         return Character.codePointBeforeImpl(value, offset + index, offset);
 757     }
 758 
 759     /**
 760      * Returns the number of Unicode code points in the specified text
 761      * range of this <code>String</code>. The text range begins at the
 762      * specified <code>beginIndex</code> and extends to the
 763      * <code>char</code> at index <code>endIndex - 1</code>. Thus the
 764      * length (in <code>char</code>s) of the text range is
 765      * <code>endIndex-beginIndex</code>. Unpaired surrogates within
 766      * the text range count as one code point each.
 767      *
 768      * @param beginIndex the index to the first <code>char</code> of
 769      * the text range.
 770      * @param endIndex the index after the last <code>char</code> of
 771      * the text range.
 772      * @return the number of Unicode code points in the specified text
 773      * range
 774      * @exception IndexOutOfBoundsException if the
 775      * <code>beginIndex</code> is negative, or <code>endIndex</code>
 776      * is larger than the length of this <code>String</code>, or
 777      * <code>beginIndex</code> is larger than <code>endIndex</code>.
 778      * @since  1.5
 779      */
 780     public int codePointCount(int beginIndex, int endIndex) {
 781         if (beginIndex < 0 || endIndex > count || beginIndex > endIndex) {
 782             throw new IndexOutOfBoundsException();
 783         }
 784         return Character.codePointCountImpl(value, offset+beginIndex, endIndex-beginIndex);
 785     }
 786 
 787     /**
 788      * Returns the index within this <code>String</code> that is
 789      * offset from the given <code>index</code> by
 790      * <code>codePointOffset</code> code points. Unpaired surrogates
 791      * within the text range given by <code>index</code> and
 792      * <code>codePointOffset</code> count as one code point each.
 793      *
 794      * @param index the index to be offset
 795      * @param codePointOffset the offset in code points
 796      * @return the index within this <code>String</code>
 797      * @exception IndexOutOfBoundsException if <code>index</code>
 798      *   is negative or larger then the length of this
 799      *   <code>String</code>, or if <code>codePointOffset</code> is positive
 800      *   and the substring starting with <code>index</code> has fewer
 801      *   than <code>codePointOffset</code> code points,
 802      *   or if <code>codePointOffset</code> is negative and the substring
 803      *   before <code>index</code> has fewer than the absolute value
 804      *   of <code>codePointOffset</code> code points.
 805      * @since 1.5
 806      */
 807     public int offsetByCodePoints(int index, int codePointOffset) {
 808         if (index < 0 || index > count) {
 809             throw new IndexOutOfBoundsException();
 810         }
 811         return Character.offsetByCodePointsImpl(value, offset, count,
 812                                                 offset+index, codePointOffset) - offset;
 813     }
 814 
 815     /**
 816      * Copy characters from this string into dst starting at dstBegin.
 817      * This method doesn't perform any range checking.
 818      */
 819     void getChars(char dst[], int dstBegin) {
 820         System.arraycopy(value, offset, dst, dstBegin, count);
 821     }
 822 
 823     /**
 824      * Copies characters from this string into the destination character
 825      * array.
 826      * <p>
 827      * The first character to be copied is at index <code>srcBegin</code>;
 828      * the last character to be copied is at index <code>srcEnd-1</code>
 829      * (thus the total number of characters to be copied is
 830      * <code>srcEnd-srcBegin</code>). The characters are copied into the
 831      * subarray of <code>dst</code> starting at index <code>dstBegin</code>
 832      * and ending at index:
 833      * <p><blockquote><pre>
 834      *     dstbegin + (srcEnd-srcBegin) - 1
 835      * </pre></blockquote>
 836      *
 837      * @param      srcBegin   index of the first character in the string
 838      *                        to copy.
 839      * @param      srcEnd     index after the last character in the string
 840      *                        to copy.
 841      * @param      dst        the destination array.
 842      * @param      dstBegin   the start offset in the destination array.
 843      * @exception IndexOutOfBoundsException If any of the following
 844      *            is true:
 845      *            <ul><li><code>srcBegin</code> is negative.
 846      *            <li><code>srcBegin</code> is greater than <code>srcEnd</code>
 847      *            <li><code>srcEnd</code> is greater than the length of this
 848      *                string
 849      *            <li><code>dstBegin</code> is negative
 850      *            <li><code>dstBegin+(srcEnd-srcBegin)</code> is larger than
 851      *                <code>dst.length</code></ul>
 852      */
 853     public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {
 854         if (srcBegin < 0) {
 855             throw new StringIndexOutOfBoundsException(srcBegin);
 856         }
 857         if (srcEnd > count) {
 858             throw new StringIndexOutOfBoundsException(srcEnd);
 859         }
 860         if (srcBegin > srcEnd) {
 861             throw new StringIndexOutOfBoundsException(srcEnd - srcBegin);
 862         }
 863         System.arraycopy(value, offset + srcBegin, dst, dstBegin,
 864              srcEnd - srcBegin);
 865     }
 866 
 867     /**
 868      * Copies characters from this string into the destination byte array. Each
 869      * byte receives the 8 low-order bits of the corresponding character. The
 870      * eight high-order bits of each character are not copied and do not
 871      * participate in the transfer in any way.


1118      *
1119      * @param  anotherString
1120      *         The {@code String} to compare this {@code String} against
1121      *
1122      * @return  {@code true} if the argument is not {@code null} and it
1123      *          represents an equivalent {@code String} ignoring case; {@code
1124      *          false} otherwise
1125      *
1126      * @see  #equals(Object)
1127      */
1128     public boolean equalsIgnoreCase(String anotherString) {
1129         return (this == anotherString) ? true :
1130                (anotherString != null) && (anotherString.count == count) &&
1131                regionMatches(true, 0, anotherString, 0, count);
1132     }
1133 
1134     /**
1135      * Compares two strings lexicographically.
1136      * The comparison is based on the Unicode value of each character in
1137      * the strings. The character sequence represented by this
1138      * <code>String</code> object is compared lexicographically to the
1139      * character sequence represented by the argument string. The result is
1140      * a negative integer if this <code>String</code> object
1141      * lexicographically precedes the argument string. The result is a
1142      * positive integer if this <code>String</code> object lexicographically
1143      * follows the argument string. The result is zero if the strings
1144      * are equal; <code>compareTo</code> returns <code>0</code> exactly when
1145      * the {@link #equals(Object)} method would return <code>true</code>.
1146      * <p>
1147      * This is the definition of lexicographic ordering. If two strings are
1148      * different, then either they have different characters at some index
1149      * that is a valid index for both strings, or their lengths are different,
1150      * or both. If they have different characters at one or more index
1151      * positions, let <i>k</i> be the smallest such index; then the string
1152      * whose character at position <i>k</i> has the smaller value, as
1153      * determined by using the &lt; operator, lexicographically precedes the
1154      * other string. In this case, <code>compareTo</code> returns the
1155      * difference of the two character values at position <code>k</code> in
1156      * the two string -- that is, the value:
1157      * <blockquote><pre>
1158      * this.charAt(k)-anotherString.charAt(k)
1159      * </pre></blockquote>
1160      * If there is no index position at which they differ, then the shorter
1161      * string lexicographically precedes the longer string. In this case,
1162      * <code>compareTo</code> returns the difference of the lengths of the
1163      * strings -- that is, the value:
1164      * <blockquote><pre>
1165      * this.length()-anotherString.length()
1166      * </pre></blockquote>
1167      *
1168      * @param   anotherString   the <code>String</code> to be compared.
1169      * @return  the value <code>0</code> if the argument string is equal to
1170      *          this string; a value less than <code>0</code> if this string
1171      *          is lexicographically less than the string argument; and a
1172      *          value greater than <code>0</code> if this string is
1173      *          lexicographically greater than the string argument.
1174      */
1175     public int compareTo(String anotherString) {
1176         int len1 = count;
1177         int len2 = anotherString.count;
1178         int n = Math.min(len1, len2);
1179         char v1[] = value;
1180         char v2[] = anotherString.value;
1181         int i = offset;
1182         int j = anotherString.offset;
1183 
1184         if (i == j) {
1185             int k = i;
1186             int lim = n + i;
1187             while (k < lim) {
1188                 char c1 = v1[k];
1189                 char c2 = v2[k];
1190                 if (c1 != c2) {
1191                     return c1 - c2;
1192                 }
1193                 k++;
1194             }
1195         } else {
1196             while (n-- != 0) {
1197                 char c1 = v1[i++];
1198                 char c2 = v2[j++];
1199                 if (c1 != c2) {
1200                     return c1 - c2;
1201                 }
1202             }
1203         }
1204         return len1 - len2;
1205     }
1206 
1207     /**
1208      * A Comparator that orders <code>String</code> objects as by
1209      * <code>compareToIgnoreCase</code>. This comparator is serializable.
1210      * <p>
1211      * Note that this Comparator does <em>not</em> take locale into account,
1212      * and will result in an unsatisfactory ordering for certain locales.
1213      * The java.text package provides <em>Collators</em> to allow
1214      * locale-sensitive ordering.
1215      *
1216      * @see     java.text.Collator#compare(String, String)
1217      * @since   1.2
1218      */
1219     public static final Comparator<String> CASE_INSENSITIVE_ORDER
1220                                          = new CaseInsensitiveComparator();
1221     private static class CaseInsensitiveComparator
1222                          implements Comparator<String>, java.io.Serializable {
1223         // use serialVersionUID from JDK 1.2.2 for interoperability
1224         private static final long serialVersionUID = 8575799808933029326L;
1225 
1226         public int compare(String s1, String s2) {
1227             int n1 = s1.length();
1228             int n2 = s2.length();
1229             int min = Math.min(n1, n2);


1236                     if (c1 != c2) {
1237                         c1 = Character.toLowerCase(c1);
1238                         c2 = Character.toLowerCase(c2);
1239                         if (c1 != c2) {
1240                             // No overflow because of numeric promotion
1241                             return c1 - c2;
1242                         }
1243                     }
1244                 }
1245             }
1246             return n1 - n2;
1247         }
1248 
1249         /** Replaces the de-serialized object. */
1250         private Object readResolve() { return CASE_INSENSITIVE_ORDER; }
1251     }
1252 
1253     /**
1254      * Compares two strings lexicographically, ignoring case
1255      * differences. This method returns an integer whose sign is that of
1256      * calling <code>compareTo</code> with normalized versions of the strings
1257      * where case differences have been eliminated by calling
1258      * <code>Character.toLowerCase(Character.toUpperCase(character))</code> on
1259      * each character.
1260      * <p>
1261      * Note that this method does <em>not</em> take locale into account,
1262      * and will result in an unsatisfactory ordering for certain locales.
1263      * The java.text package provides <em>collators</em> to allow
1264      * locale-sensitive ordering.
1265      *
1266      * @param   str   the <code>String</code> to be compared.
1267      * @return  a negative integer, zero, or a positive integer as the
1268      *          specified String is greater than, equal to, or less
1269      *          than this String, ignoring case considerations.
1270      * @see     java.text.Collator#compare(String, String)
1271      * @since   1.2
1272      */
1273     public int compareToIgnoreCase(String str) {
1274         return CASE_INSENSITIVE_ORDER.compare(this, str);
1275     }
1276 
1277     /**
1278      * Tests if two string regions are equal.
1279      * <p>
1280      * A substring of this <tt>String</tt> object is compared to a substring
1281      * of the argument other. The result is true if these substrings
1282      * represent identical character sequences. The substring of this
1283      * <tt>String</tt> object to be compared begins at index <tt>toffset</tt>
1284      * and has length <tt>len</tt>. The substring of other to be compared
1285      * begins at index <tt>ooffset</tt> and has length <tt>len</tt>. The
1286      * result is <tt>false</tt> if and only if at least one of the following
1287      * is true:
1288      * <ul><li><tt>toffset</tt> is negative.
1289      * <li><tt>ooffset</tt> is negative.
1290      * <li><tt>toffset+len</tt> is greater than the length of this
1291      * <tt>String</tt> object.
1292      * <li><tt>ooffset+len</tt> is greater than the length of the other
1293      * argument.
1294      * <li>There is some nonnegative integer <i>k</i> less than <tt>len</tt>
1295      * such that:
1296      * <tt>this.charAt(toffset+<i>k</i>)&nbsp;!=&nbsp;other.charAt(ooffset+<i>k</i>)</tt>
1297      * </ul>
1298      *
1299      * @param   toffset   the starting offset of the subregion in this string.
1300      * @param   other     the string argument.
1301      * @param   ooffset   the starting offset of the subregion in the string
1302      *                    argument.
1303      * @param   len       the number of characters to compare.
1304      * @return  <code>true</code> if the specified subregion of this string
1305      *          exactly matches the specified subregion of the string argument;
1306      *          <code>false</code> otherwise.
1307      */
1308     public boolean regionMatches(int toffset, String other, int ooffset,
1309                                  int len) {
1310         char ta[] = value;
1311         int to = offset + toffset;
1312         char pa[] = other.value;
1313         int po = other.offset + ooffset;
1314         // Note: toffset, ooffset, or len might be near -1>>>1.
1315         if ((ooffset < 0) || (toffset < 0) || (toffset > (long)count - len)
1316             || (ooffset > (long)other.count - len)) {
1317             return false;
1318         }
1319         while (len-- > 0) {
1320             if (ta[to++] != pa[po++]) {
1321                 return false;
1322             }
1323         }
1324         return true;
1325     }
1326 


1343      * <li><tt>ooffset+len</tt> is greater than the length of the other
1344      * argument.
1345      * <li><tt>ignoreCase</tt> is <tt>false</tt> and there is some nonnegative
1346      * integer <i>k</i> less than <tt>len</tt> such that:
1347      * <blockquote><pre>
1348      * this.charAt(toffset+k) != other.charAt(ooffset+k)
1349      * </pre></blockquote>
1350      * <li><tt>ignoreCase</tt> is <tt>true</tt> and there is some nonnegative
1351      * integer <i>k</i> less than <tt>len</tt> such that:
1352      * <blockquote><pre>
1353      * Character.toLowerCase(this.charAt(toffset+k)) !=
1354                Character.toLowerCase(other.charAt(ooffset+k))
1355      * </pre></blockquote>
1356      * and:
1357      * <blockquote><pre>
1358      * Character.toUpperCase(this.charAt(toffset+k)) !=
1359      *         Character.toUpperCase(other.charAt(ooffset+k))
1360      * </pre></blockquote>
1361      * </ul>
1362      *
1363      * @param   ignoreCase   if <code>true</code>, ignore case when comparing
1364      *                       characters.
1365      * @param   toffset      the starting offset of the subregion in this
1366      *                       string.
1367      * @param   other        the string argument.
1368      * @param   ooffset      the starting offset of the subregion in the string
1369      *                       argument.
1370      * @param   len          the number of characters to compare.
1371      * @return  <code>true</code> if the specified subregion of this string
1372      *          matches the specified subregion of the string argument;
1373      *          <code>false</code> otherwise. Whether the matching is exact
1374      *          or case insensitive depends on the <code>ignoreCase</code>
1375      *          argument.
1376      */
1377     public boolean regionMatches(boolean ignoreCase, int toffset,
1378                            String other, int ooffset, int len) {
1379         char ta[] = value;
1380         int to = offset + toffset;
1381         char pa[] = other.value;
1382         int po = other.offset + ooffset;
1383         // Note: toffset, ooffset, or len might be near -1>>>1.
1384         if ((ooffset < 0) || (toffset < 0) || (toffset > (long)count - len) ||
1385                 (ooffset > (long)other.count - len)) {
1386             return false;
1387         }
1388         while (len-- > 0) {
1389             char c1 = ta[to++];
1390             char c2 = pa[po++];
1391             if (c1 == c2) {
1392                 continue;
1393             }
1394             if (ignoreCase) {


1403                 }
1404                 // Unfortunately, conversion to uppercase does not work properly
1405                 // for the Georgian alphabet, which has strange rules about case
1406                 // conversion.  So we need to make one last check before
1407                 // exiting.
1408                 if (Character.toLowerCase(u1) == Character.toLowerCase(u2)) {
1409                     continue;
1410                 }
1411             }
1412             return false;
1413         }
1414         return true;
1415     }
1416 
1417     /**
1418      * Tests if the substring of this string beginning at the
1419      * specified index starts with the specified prefix.
1420      *
1421      * @param   prefix    the prefix.
1422      * @param   toffset   where to begin looking in this string.
1423      * @return  <code>true</code> if the character sequence represented by the
1424      *          argument is a prefix of the substring of this object starting
1425      *          at index <code>toffset</code>; <code>false</code> otherwise.
1426      *          The result is <code>false</code> if <code>toffset</code> is
1427      *          negative or greater than the length of this
1428      *          <code>String</code> object; otherwise the result is the same
1429      *          as the result of the expression
1430      *          <pre>
1431      *          this.substring(toffset).startsWith(prefix)
1432      *          </pre>
1433      */
1434     public boolean startsWith(String prefix, int toffset) {
1435         char ta[] = value;
1436         int to = offset + toffset;
1437         char pa[] = prefix.value;
1438         int po = prefix.offset;
1439         int pc = prefix.count;
1440         // Note: toffset might be near -1>>>1.
1441         if ((toffset < 0) || (toffset > count - pc)) {
1442             return false;
1443         }
1444         while (--pc >= 0) {
1445             if (ta[to++] != pa[po++]) {
1446                 return false;
1447             }
1448         }
1449         return true;
1450     }
1451 
1452     /**
1453      * Tests if this string starts with the specified prefix.
1454      *
1455      * @param   prefix   the prefix.
1456      * @return  <code>true</code> if the character sequence represented by the
1457      *          argument is a prefix of the character sequence represented by
1458      *          this string; <code>false</code> otherwise.
1459      *          Note also that <code>true</code> will be returned if the
1460      *          argument is an empty string or is equal to this
1461      *          <code>String</code> object as determined by the
1462      *          {@link #equals(Object)} method.
1463      * @since   1. 0
1464      */
1465     public boolean startsWith(String prefix) {
1466         return startsWith(prefix, 0);
1467     }
1468 
1469     /**
1470      * Tests if this string ends with the specified suffix.
1471      *
1472      * @param   suffix   the suffix.
1473      * @return  <code>true</code> if the character sequence represented by the
1474      *          argument is a suffix of the character sequence represented by
1475      *          this object; <code>false</code> otherwise. Note that the
1476      *          result will be <code>true</code> if the argument is the
1477      *          empty string or is equal to this <code>String</code> object
1478      *          as determined by the {@link #equals(Object)} method.
1479      */
1480     public boolean endsWith(String suffix) {
1481         return startsWith(suffix, count - suffix.count);
1482     }
1483 
1484     /**
1485      * Returns a hash code for this string. The hash code for a
1486      * <code>String</code> object is computed as
1487      * <blockquote><pre>
1488      * s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
1489      * </pre></blockquote>
1490      * using <code>int</code> arithmetic, where <code>s[i]</code> is the
1491      * <i>i</i>th character of the string, <code>n</code> is the length of
1492      * the string, and <code>^</code> indicates exponentiation.
1493      * (The hash value of the empty string is zero.)
1494      *
1495      * @return  a hash code value for this object.
1496      */
1497     public int hashCode() {
1498         int h = hash;
1499         if (h == 0 && count > 0) {
1500             int off = offset;
1501             char val[] = value;
1502             int len = count;
1503 
1504             for (int i = 0; i < len; i++) {
1505                 h = 31*h + val[off++];
1506             }
1507             hash = h;
1508         }
1509         return h;
1510     }
1511 
1512     /**
1513      * Returns the index within this string of the first occurrence of
1514      * the specified character. If a character with value
1515      * <code>ch</code> occurs in the character sequence represented by
1516      * this <code>String</code> object, then the index (in Unicode
1517      * code units) of the first such occurrence is returned. For
1518      * values of <code>ch</code> in the range from 0 to 0xFFFF
1519      * (inclusive), this is the smallest value <i>k</i> such that:
1520      * <blockquote><pre>
1521      * this.charAt(<i>k</i>) == ch
1522      * </pre></blockquote>
1523      * is true. For other values of <code>ch</code>, it is the
1524      * smallest value <i>k</i> such that:
1525      * <blockquote><pre>
1526      * this.codePointAt(<i>k</i>) == ch
1527      * </pre></blockquote>
1528      * is true. In either case, if no such character occurs in this
1529      * string, then <code>-1</code> is returned.
1530      *
1531      * @param   ch   a character (Unicode code point).
1532      * @return  the index of the first occurrence of the character in the
1533      *          character sequence represented by this object, or
1534      *          <code>-1</code> if the character does not occur.
1535      */
1536     public int indexOf(int ch) {
1537         return indexOf(ch, 0);
1538     }
1539 
1540     /**
1541      * Returns the index within this string of the first occurrence of the
1542      * specified character, starting the search at the specified index.
1543      * <p>
1544      * If a character with value <code>ch</code> occurs in the
1545      * character sequence represented by this <code>String</code>
1546      * object at an index no smaller than <code>fromIndex</code>, then
1547      * the index of the first such occurrence is returned. For values
1548      * of <code>ch</code> in the range from 0 to 0xFFFF (inclusive),
1549      * this is the smallest value <i>k</i> such that:
1550      * <blockquote><pre>
1551      * (this.charAt(<i>k</i>) == ch) && (<i>k</i> &gt;= fromIndex)
1552      * </pre></blockquote>
1553      * is true. For other values of <code>ch</code>, it is the
1554      * smallest value <i>k</i> such that:
1555      * <blockquote><pre>
1556      * (this.codePointAt(<i>k</i>) == ch) && (<i>k</i> &gt;= fromIndex)
1557      * </pre></blockquote>
1558      * is true. In either case, if no such character occurs in this
1559      * string at or after position <code>fromIndex</code>, then
1560      * <code>-1</code> is returned.
1561      *
1562      * <p>
1563      * There is no restriction on the value of <code>fromIndex</code>. If it
1564      * is negative, it has the same effect as if it were zero: this entire
1565      * string may be searched. If it is greater than the length of this
1566      * string, it has the same effect as if it were equal to the length of
1567      * this string: <code>-1</code> is returned.
1568      *
1569      * <p>All indices are specified in <code>char</code> values
1570      * (Unicode code units).
1571      *
1572      * @param   ch          a character (Unicode code point).
1573      * @param   fromIndex   the index to start the search from.
1574      * @return  the index of the first occurrence of the character in the
1575      *          character sequence represented by this object that is greater
1576      *          than or equal to <code>fromIndex</code>, or <code>-1</code>
1577      *          if the character does not occur.
1578      */
1579     public int indexOf(int ch, int fromIndex) {
1580         if (fromIndex < 0) {
1581             fromIndex = 0;
1582         } else if (fromIndex >= count) {
1583             // Note: fromIndex might be near -1>>>1.
1584             return -1;
1585         }
1586 
1587         if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) {
1588             // handle most cases here (ch is a BMP code point or a
1589             // negative value (invalid code point))
1590             final char[] value = this.value;
1591             final int offset = this.offset;
1592             final int max = offset + count;
1593             for (int i = offset + fromIndex; i < max ; i++) {
1594                 if (value[i] == ch) {
1595                     return i - offset;
1596                 }


1605      * Handles (rare) calls of indexOf with a supplementary character.
1606      */
1607     private int indexOfSupplementary(int ch, int fromIndex) {
1608         if (Character.isValidCodePoint(ch)) {
1609             final char[] value = this.value;
1610             final int offset = this.offset;
1611             final char hi = Character.highSurrogate(ch);
1612             final char lo = Character.lowSurrogate(ch);
1613             final int max = offset + count - 1;
1614             for (int i = offset + fromIndex; i < max; i++) {
1615                 if (value[i] == hi && value[i+1] == lo) {
1616                     return i - offset;
1617                 }
1618             }
1619         }
1620         return -1;
1621     }
1622 
1623     /**
1624      * Returns the index within this string of the last occurrence of
1625      * the specified character. For values of <code>ch</code> in the
1626      * range from 0 to 0xFFFF (inclusive), the index (in Unicode code
1627      * units) returned is the largest value <i>k</i> such that:
1628      * <blockquote><pre>
1629      * this.charAt(<i>k</i>) == ch
1630      * </pre></blockquote>
1631      * is true. For other values of <code>ch</code>, it is the
1632      * largest value <i>k</i> such that:
1633      * <blockquote><pre>
1634      * this.codePointAt(<i>k</i>) == ch
1635      * </pre></blockquote>
1636      * is true.  In either case, if no such character occurs in this
1637      * string, then <code>-1</code> is returned.  The
1638      * <code>String</code> is searched backwards starting at the last
1639      * character.
1640      *
1641      * @param   ch   a character (Unicode code point).
1642      * @return  the index of the last occurrence of the character in the
1643      *          character sequence represented by this object, or
1644      *          <code>-1</code> if the character does not occur.
1645      */
1646     public int lastIndexOf(int ch) {
1647         return lastIndexOf(ch, count - 1);
1648     }
1649 
1650     /**
1651      * Returns the index within this string of the last occurrence of
1652      * the specified character, searching backward starting at the
1653      * specified index. For values of <code>ch</code> in the range
1654      * from 0 to 0xFFFF (inclusive), the index returned is the largest
1655      * value <i>k</i> such that:
1656      * <blockquote><pre>
1657      * (this.charAt(<i>k</i>) == ch) && (<i>k</i> &lt;= fromIndex)
1658      * </pre></blockquote>
1659      * is true. For other values of <code>ch</code>, it is the
1660      * largest value <i>k</i> such that:
1661      * <blockquote><pre>
1662      * (this.codePointAt(<i>k</i>) == ch) && (<i>k</i> &lt;= fromIndex)
1663      * </pre></blockquote>
1664      * is true. In either case, if no such character occurs in this
1665      * string at or before position <code>fromIndex</code>, then
1666      * <code>-1</code> is returned.
1667      *
1668      * <p>All indices are specified in <code>char</code> values
1669      * (Unicode code units).
1670      *
1671      * @param   ch          a character (Unicode code point).
1672      * @param   fromIndex   the index to start the search from. There is no
1673      *          restriction on the value of <code>fromIndex</code>. If it is
1674      *          greater than or equal to the length of this string, it has
1675      *          the same effect as if it were equal to one less than the
1676      *          length of this string: this entire string may be searched.
1677      *          If it is negative, it has the same effect as if it were -1:
1678      *          -1 is returned.
1679      * @return  the index of the last occurrence of the character in the
1680      *          character sequence represented by this object that is less
1681      *          than or equal to <code>fromIndex</code>, or <code>-1</code>
1682      *          if the character does not occur before that point.
1683      */
1684     public int lastIndexOf(int ch, int fromIndex) {
1685         if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) {
1686             // handle most cases here (ch is a BMP code point or a
1687             // negative value (invalid code point))
1688             final char[] value = this.value;
1689             final int offset = this.offset;
1690             int i = offset + Math.min(fromIndex, count - 1);
1691             for (; i >= offset ; i--) {
1692                 if (value[i] == ch) {
1693                     return i - offset;
1694                 }
1695             }
1696             return -1;
1697         } else {
1698             return lastIndexOfSupplementary(ch, fromIndex);
1699         }
1700     }
1701 


1904                 }
1905             }
1906             return start - sourceOffset + 1;
1907         }
1908     }
1909 
1910     /**
1911      * Returns a new string that is a substring of this string. The
1912      * substring begins with the character at the specified index and
1913      * extends to the end of this string. <p>
1914      * Examples:
1915      * <blockquote><pre>
1916      * "unhappy".substring(2) returns "happy"
1917      * "Harbison".substring(3) returns "bison"
1918      * "emptiness".substring(9) returns "" (an empty string)
1919      * </pre></blockquote>
1920      *
1921      * @param      beginIndex   the beginning index, inclusive.
1922      * @return     the specified substring.
1923      * @exception  IndexOutOfBoundsException  if
1924      *             <code>beginIndex</code> is negative or larger than the
1925      *             length of this <code>String</code> object.
1926      */
1927     public String substring(int beginIndex) {
1928         return substring(beginIndex, count);
1929     }
1930 
1931     /**
1932      * Returns a new string that is a substring of this string. The
1933      * substring begins at the specified <code>beginIndex</code> and
1934      * extends to the character at index <code>endIndex - 1</code>.
1935      * Thus the length of the substring is <code>endIndex-beginIndex</code>.
1936      * <p>
1937      * Examples:
1938      * <blockquote><pre>
1939      * "hamburger".substring(4, 8) returns "urge"
1940      * "smiles".substring(1, 5) returns "mile"
1941      * </pre></blockquote>
1942      *
1943      * @param      beginIndex   the beginning index, inclusive.
1944      * @param      endIndex     the ending index, exclusive.
1945      * @return     the specified substring.
1946      * @exception  IndexOutOfBoundsException  if the
1947      *             <code>beginIndex</code> is negative, or
1948      *             <code>endIndex</code> is larger than the length of
1949      *             this <code>String</code> object, or
1950      *             <code>beginIndex</code> is larger than
1951      *             <code>endIndex</code>.
1952      */
1953     public String substring(int beginIndex, int endIndex) {
1954         if (beginIndex < 0) {
1955             throw new StringIndexOutOfBoundsException(beginIndex);
1956         }
1957         if (endIndex > count) {
1958             throw new StringIndexOutOfBoundsException(endIndex);
1959         }
1960         if (beginIndex > endIndex) {
1961             throw new StringIndexOutOfBoundsException(endIndex - beginIndex);
1962         }
1963         return ((beginIndex == 0) && (endIndex == count)) ? this :
1964             new String(offset + beginIndex, endIndex - beginIndex, value);
1965     }
1966 
1967     /**
1968      * Returns a new character sequence that is a subsequence of this sequence.
1969      *
1970      * <p> An invocation of this method of the form
1971      *


1982      *
1983      * @param      beginIndex   the begin index, inclusive.
1984      * @param      endIndex     the end index, exclusive.
1985      * @return     the specified subsequence.
1986      *
1987      * @throws  IndexOutOfBoundsException
1988      *          if <tt>beginIndex</tt> or <tt>endIndex</tt> are negative,
1989      *          if <tt>endIndex</tt> is greater than <tt>length()</tt>,
1990      *          or if <tt>beginIndex</tt> is greater than <tt>startIndex</tt>
1991      *
1992      * @since 1.4
1993      * @spec JSR-51
1994      */
1995     public CharSequence subSequence(int beginIndex, int endIndex) {
1996         return this.substring(beginIndex, endIndex);
1997     }
1998 
1999     /**
2000      * Concatenates the specified string to the end of this string.
2001      * <p>
2002      * If the length of the argument string is <code>0</code>, then this
2003      * <code>String</code> object is returned. Otherwise, a new
2004      * <code>String</code> object is created, representing a character
2005      * sequence that is the concatenation of the character sequence
2006      * represented by this <code>String</code> object and the character
2007      * sequence represented by the argument string.<p>
2008      * Examples:
2009      * <blockquote><pre>
2010      * "cares".concat("s") returns "caress"
2011      * "to".concat("get").concat("her") returns "together"
2012      * </pre></blockquote>
2013      *
2014      * @param   str   the <code>String</code> that is concatenated to the end
2015      *                of this <code>String</code>.
2016      * @return  a string that represents the concatenation of this object's
2017      *          characters followed by the string argument's characters.
2018      */
2019     public String concat(String str) {
2020         int otherLen = str.length();
2021         if (otherLen == 0) {
2022             return this;
2023         }
2024         char buf[] = new char[count + otherLen];
2025         getChars(0, count, buf, 0);
2026         str.getChars(0, otherLen, buf, count);
2027         return new String(0, count + otherLen, buf);
2028     }
2029 
2030     /**
2031      * Returns a new string resulting from replacing all occurrences of
2032      * <code>oldChar</code> in this string with <code>newChar</code>.
2033      * <p>
2034      * If the character <code>oldChar</code> does not occur in the
2035      * character sequence represented by this <code>String</code> object,
2036      * then a reference to this <code>String</code> object is returned.
2037      * Otherwise, a new <code>String</code> object is created that
2038      * represents a character sequence identical to the character sequence
2039      * represented by this <code>String</code> object, except that every
2040      * occurrence of <code>oldChar</code> is replaced by an occurrence
2041      * of <code>newChar</code>.
2042      * <p>
2043      * Examples:
2044      * <blockquote><pre>
2045      * "mesquite in your cellar".replace('e', 'o')
2046      *         returns "mosquito in your collar"
2047      * "the war of baronets".replace('r', 'y')
2048      *         returns "the way of bayonets"
2049      * "sparring with a purple porpoise".replace('p', 't')
2050      *         returns "starring with a turtle tortoise"
2051      * "JonL".replace('q', 'x') returns "JonL" (no change)
2052      * </pre></blockquote>
2053      *
2054      * @param   oldChar   the old character.
2055      * @param   newChar   the new character.
2056      * @return  a string derived from this string by replacing every
2057      *          occurrence of <code>oldChar</code> with <code>newChar</code>.
2058      */
2059     public String replace(char oldChar, char newChar) {
2060         if (oldChar != newChar) {
2061             int len = count;
2062             int i = -1;
2063             char[] val = value; /* avoid getfield opcode */
2064             int off = offset;   /* avoid getfield opcode */
2065 
2066             while (++i < len) {
2067                 if (val[off + i] == oldChar) {
2068                     break;
2069                 }
2070             }
2071             if (i < len) {
2072                 char buf[] = new char[len];
2073                 for (int j = 0 ; j < i ; j++) {
2074                     buf[j] = val[off+j];
2075                 }
2076                 while (i < len) {
2077                     char c = val[off + i];


2102      * @return  <tt>true</tt> if, and only if, this string matches the
2103      *          given regular expression
2104      *
2105      * @throws  PatternSyntaxException
2106      *          if the regular expression's syntax is invalid
2107      *
2108      * @see java.util.regex.Pattern
2109      *
2110      * @since 1.4
2111      * @spec JSR-51
2112      */
2113     public boolean matches(String regex) {
2114         return Pattern.matches(regex, this);
2115     }
2116 
2117     /**
2118      * Returns true if and only if this string contains the specified
2119      * sequence of char values.
2120      *
2121      * @param s the sequence to search for
2122      * @return true if this string contains <code>s</code>, false otherwise
2123      * @throws NullPointerException if <code>s</code> is <code>null</code>
2124      * @since 1.5
2125      */
2126     public boolean contains(CharSequence s) {
2127         return indexOf(s.toString()) > -1;
2128     }
2129 
2130     /**
2131      * Replaces the first substring of this string that matches the given <a
2132      * href="../util/regex/Pattern.html#sum">regular expression</a> with the
2133      * given replacement.
2134      *
2135      * <p> An invocation of this method of the form
2136      * <i>str</i><tt>.replaceFirst(</tt><i>regex</i><tt>,</tt> <i>repl</i><tt>)</tt>
2137      * yields exactly the same result as the expression
2138      *
2139      * <blockquote><tt>
2140      * {@link java.util.regex.Pattern}.{@link java.util.regex.Pattern#compile
2141      * compile}(</tt><i>regex</i><tt>).{@link
2142      * java.util.regex.Pattern#matcher(java.lang.CharSequence)
2143      * matcher}(</tt><i>str</i><tt>).{@link java.util.regex.Matcher#replaceFirst


2206      *
2207      * @see java.util.regex.Pattern
2208      *
2209      * @since 1.4
2210      * @spec JSR-51
2211      */
2212     public String replaceAll(String regex, String replacement) {
2213         return Pattern.compile(regex).matcher(this).replaceAll(replacement);
2214     }
2215 
2216     /**
2217      * Replaces each substring of this string that matches the literal target
2218      * sequence with the specified literal replacement sequence. The
2219      * replacement proceeds from the beginning of the string to the end, for
2220      * example, replacing "aa" with "b" in the string "aaa" will result in
2221      * "ba" rather than "ab".
2222      *
2223      * @param  target The sequence of char values to be replaced
2224      * @param  replacement The replacement sequence of char values
2225      * @return  The resulting string
2226      * @throws NullPointerException if <code>target</code> or
2227      *         <code>replacement</code> is <code>null</code>.
2228      * @since 1.5
2229      */
2230     public String replace(CharSequence target, CharSequence replacement) {
2231         return Pattern.compile(target.toString(), Pattern.LITERAL).matcher(
2232             this).replaceAll(Matcher.quoteReplacement(replacement.toString()));
2233     }
2234 
2235     /**
2236      * Splits this string around matches of the given
2237      * <a href="../util/regex/Pattern.html#sum">regular expression</a>.
2238      *
2239      * <p> The array returned by this method contains each substring of this
2240      * string that is terminated by another substring that matches the given
2241      * expression or is terminated by the end of the string.  The substrings in
2242      * the array are in the order in which they occur in this string.  If the
2243      * expression does not match any part of the input then the resulting array
2244      * has just one element, namely this string.
2245      *
2246      * <p> The <tt>limit</tt> parameter controls the number of times the
2247      * pattern is applied and therefore affects the length of the resulting


2390      *
2391      * @param  regex
2392      *         the delimiting regular expression
2393      *
2394      * @return  the array of strings computed by splitting this string
2395      *          around matches of the given regular expression
2396      *
2397      * @throws  PatternSyntaxException
2398      *          if the regular expression's syntax is invalid
2399      *
2400      * @see java.util.regex.Pattern
2401      *
2402      * @since 1.4
2403      * @spec JSR-51
2404      */
2405     public String[] split(String regex) {
2406         return split(regex, 0);
2407     }
2408 
2409     /**
2410      * Converts all of the characters in this <code>String</code> to lower
2411      * case using the rules of the given <code>Locale</code>.  Case mapping is based
2412      * on the Unicode Standard version specified by the {@link java.lang.Character Character}
2413      * class. Since case mappings are not always 1:1 char mappings, the resulting
2414      * <code>String</code> may be a different length than the original <code>String</code>.
2415      * <p>
2416      * Examples of lowercase  mappings are in the following table:
2417      * <table border="1" summary="Lowercase mapping examples showing language code of locale, upper case, lower case, and description">
2418      * <tr>
2419      *   <th>Language Code of Locale</th>
2420      *   <th>Upper Case</th>
2421      *   <th>Lower Case</th>
2422      *   <th>Description</th>
2423      * </tr>
2424      * <tr>
2425      *   <td>tr (Turkish)</td>
2426      *   <td>&#92;u0130</td>
2427      *   <td>&#92;u0069</td>
2428      *   <td>capital letter I with dot above -&gt; small letter i</td>
2429      * </tr>
2430      * <tr>
2431      *   <td>tr (Turkish)</td>
2432      *   <td>&#92;u0049</td>
2433      *   <td>&#92;u0131</td>
2434      *   <td>capital letter I -&gt; small letter dotless i </td>
2435      * </tr>
2436      * <tr>
2437      *   <td>(all)</td>
2438      *   <td>French Fries</td>
2439      *   <td>french fries</td>
2440      *   <td>lowercased all chars in String</td>
2441      * </tr>
2442      * <tr>
2443      *   <td>(all)</td>
2444      *   <td><img src="doc-files/capiota.gif" alt="capiota"><img src="doc-files/capchi.gif" alt="capchi">
2445      *       <img src="doc-files/captheta.gif" alt="captheta"><img src="doc-files/capupsil.gif" alt="capupsil">
2446      *       <img src="doc-files/capsigma.gif" alt="capsigma"></td>
2447      *   <td><img src="doc-files/iota.gif" alt="iota"><img src="doc-files/chi.gif" alt="chi">
2448      *       <img src="doc-files/theta.gif" alt="theta"><img src="doc-files/upsilon.gif" alt="upsilon">
2449      *       <img src="doc-files/sigma1.gif" alt="sigma"></td>
2450      *   <td>lowercased all chars in String</td>
2451      * </tr>
2452      * </table>
2453      *
2454      * @param locale use the case transformation rules for this locale
2455      * @return the <code>String</code>, converted to lowercase.
2456      * @see     java.lang.String#toLowerCase()
2457      * @see     java.lang.String#toUpperCase()
2458      * @see     java.lang.String#toUpperCase(Locale)
2459      * @since   1.1
2460      */
2461     public String toLowerCase(Locale locale) {
2462         if (locale == null) {
2463             throw new NullPointerException();
2464         }
2465 
2466         int     firstUpper;
2467 
2468         /* Now check if there are any characters that need to be changed. */
2469         scan: {
2470             for (firstUpper = 0 ; firstUpper < count; ) {
2471                 char c = value[offset+firstUpper];
2472                 if ((c >= Character.MIN_HIGH_SURROGATE) &&
2473                     (c <= Character.MAX_HIGH_SURROGATE)) {
2474                     int supplChar = codePointAt(firstUpper);
2475                     if (supplChar != Character.toLowerCase(supplChar)) {


2536                 /* Grow result if needed */
2537                 int mapLen = lowerCharArray.length;
2538                 if (mapLen > srcCount) {
2539                     char[] result2 = new char[result.length + mapLen - srcCount];
2540                     System.arraycopy(result, 0, result2, 0,
2541                         i + resultOffset);
2542                     result = result2;
2543                 }
2544                 for (int x=0; x<mapLen; ++x) {
2545                     result[i+resultOffset+x] = lowerCharArray[x];
2546                 }
2547                 resultOffset += (mapLen - srcCount);
2548             } else {
2549                 result[i+resultOffset] = (char)lowerChar;
2550             }
2551         }
2552         return new String(0, count+resultOffset, result);
2553     }
2554 
2555     /**
2556      * Converts all of the characters in this <code>String</code> to lower
2557      * case using the rules of the default locale. This is equivalent to calling
2558      * <code>toLowerCase(Locale.getDefault())</code>.
2559      * <p>
2560      * <b>Note:</b> This method is locale sensitive, and may produce unexpected
2561      * results if used for strings that are intended to be interpreted locale
2562      * independently.
2563      * Examples are programming language identifiers, protocol keys, and HTML
2564      * tags.
2565      * For instance, <code>"TITLE".toLowerCase()</code> in a Turkish locale
2566      * returns <code>"t\u005Cu0131tle"</code>, where '\u005Cu0131' is the
2567      * LATIN SMALL LETTER DOTLESS I character.
2568      * To obtain correct results for locale insensitive strings, use
2569      * <code>toLowerCase(Locale.ENGLISH)</code>.
2570      * <p>
2571      * @return  the <code>String</code>, converted to lowercase.
2572      * @see     java.lang.String#toLowerCase(Locale)
2573      */
2574     public String toLowerCase() {
2575         return toLowerCase(Locale.getDefault());
2576     }
2577 
2578     /**
2579      * Converts all of the characters in this <code>String</code> to upper
2580      * case using the rules of the given <code>Locale</code>. Case mapping is based
2581      * on the Unicode Standard version specified by the {@link java.lang.Character Character}
2582      * class. Since case mappings are not always 1:1 char mappings, the resulting
2583      * <code>String</code> may be a different length than the original <code>String</code>.
2584      * <p>
2585      * Examples of locale-sensitive and 1:M case mappings are in the following table.
2586      * <p>
2587      * <table border="1" summary="Examples of locale-sensitive and 1:M case mappings. Shows Language code of locale, lower case, upper case, and description.">
2588      * <tr>
2589      *   <th>Language Code of Locale</th>
2590      *   <th>Lower Case</th>
2591      *   <th>Upper Case</th>
2592      *   <th>Description</th>
2593      * </tr>
2594      * <tr>
2595      *   <td>tr (Turkish)</td>
2596      *   <td>&#92;u0069</td>
2597      *   <td>&#92;u0130</td>
2598      *   <td>small letter i -&gt; capital letter I with dot above</td>
2599      * </tr>
2600      * <tr>
2601      *   <td>tr (Turkish)</td>
2602      *   <td>&#92;u0131</td>
2603      *   <td>&#92;u0049</td>
2604      *   <td>small letter dotless i -&gt; capital letter I</td>
2605      * </tr>
2606      * <tr>
2607      *   <td>(all)</td>
2608      *   <td>&#92;u00df</td>
2609      *   <td>&#92;u0053 &#92;u0053</td>
2610      *   <td>small letter sharp s -&gt; two letters: SS</td>
2611      * </tr>
2612      * <tr>
2613      *   <td>(all)</td>
2614      *   <td>Fahrvergn&uuml;gen</td>
2615      *   <td>FAHRVERGN&Uuml;GEN</td>
2616      *   <td></td>
2617      * </tr>
2618      * </table>
2619      * @param locale use the case transformation rules for this locale
2620      * @return the <code>String</code>, converted to uppercase.
2621      * @see     java.lang.String#toUpperCase()
2622      * @see     java.lang.String#toLowerCase()
2623      * @see     java.lang.String#toLowerCase(Locale)
2624      * @since   1.1
2625      */
2626     public String toUpperCase(Locale locale) {
2627         if (locale == null) {
2628             throw new NullPointerException();
2629         }
2630 
2631         int     firstLower;
2632 
2633         /* Now check if there are any characters that need to be changed. */
2634         scan: {
2635             for (firstLower = 0 ; firstLower < count; ) {
2636                 int c = (int)value[offset+firstLower];
2637                 int srcCount;
2638                 if ((c >= Character.MIN_HIGH_SURROGATE) &&
2639                     (c <= Character.MAX_HIGH_SURROGATE)) {
2640                     c = codePointAt(firstLower);


2699                 /* Grow result if needed */
2700                 int mapLen = upperCharArray.length;
2701                 if (mapLen > srcCount) {
2702                     char[] result2 = new char[result.length + mapLen - srcCount];
2703                     System.arraycopy(result, 0, result2, 0,
2704                         i + resultOffset);
2705                     result = result2;
2706                 }
2707                 for (int x=0; x<mapLen; ++x) {
2708                     result[i+resultOffset+x] = upperCharArray[x];
2709                 }
2710                 resultOffset += (mapLen - srcCount);
2711             } else {
2712                 result[i+resultOffset] = (char)upperChar;
2713             }
2714         }
2715         return new String(0, count+resultOffset, result);
2716     }
2717 
2718     /**
2719      * Converts all of the characters in this <code>String</code> to upper
2720      * case using the rules of the default locale. This method is equivalent to
2721      * <code>toUpperCase(Locale.getDefault())</code>.
2722      * <p>
2723      * <b>Note:</b> This method is locale sensitive, and may produce unexpected
2724      * results if used for strings that are intended to be interpreted locale
2725      * independently.
2726      * Examples are programming language identifiers, protocol keys, and HTML
2727      * tags.
2728      * For instance, <code>"title".toUpperCase()</code> in a Turkish locale
2729      * returns <code>"T\u005Cu0130TLE"</code>, where '\u005Cu0130' is the
2730      * LATIN CAPITAL LETTER I WITH DOT ABOVE character.
2731      * To obtain correct results for locale insensitive strings, use
2732      * <code>toUpperCase(Locale.ENGLISH)</code>.
2733      * <p>
2734      * @return  the <code>String</code>, converted to uppercase.
2735      * @see     java.lang.String#toUpperCase(Locale)
2736      */
2737     public String toUpperCase() {
2738         return toUpperCase(Locale.getDefault());
2739     }
2740 
2741     /**
2742      * Returns a copy of the string, with leading and trailing whitespace
2743      * omitted.
2744      * <p>
2745      * If this <code>String</code> object represents an empty character
2746      * sequence, or the first and last characters of character sequence
2747      * represented by this <code>String</code> object both have codes
2748      * greater than <code>'&#92;u0020'</code> (the space character), then a
2749      * reference to this <code>String</code> object is returned.
2750      * <p>
2751      * Otherwise, if there is no character with a code greater than
2752      * <code>'&#92;u0020'</code> in the string, then a new
2753      * <code>String</code> object representing an empty string is created
2754      * and returned.
2755      * <p>
2756      * Otherwise, let <i>k</i> be the index of the first character in the
2757      * string whose code is greater than <code>'&#92;u0020'</code>, and let
2758      * <i>m</i> be the index of the last character in the string whose code
2759      * is greater than <code>'&#92;u0020'</code>. A new <code>String</code>
2760      * object is created, representing the substring of this string that
2761      * begins with the character at index <i>k</i> and ends with the
2762      * character at index <i>m</i>-that is, the result of
2763      * <code>this.substring(<i>k</i>,&nbsp;<i>m</i>+1)</code>.
2764      * <p>
2765      * This method may be used to trim whitespace (as defined above) from
2766      * the beginning and end of a string.
2767      *
2768      * @return  A copy of this string with leading and trailing white
2769      *          space removed, or this string if it has no leading or
2770      *          trailing white space.
2771      */
2772     public String trim() {
2773         int len = count;
2774         int st = 0;
2775         int off = offset;      /* avoid getfield opcode */
2776         char[] val = value;    /* avoid getfield opcode */
2777 
2778         while ((st < len) && (val[off + st] <= ' ')) {
2779             st++;


2876      *          specifier that is incompatible with the given arguments,
2877      *          insufficient arguments given the format string, or other
2878      *          illegal conditions.  For specification of all possible
2879      *          formatting errors, see the <a
2880      *          href="../util/Formatter.html#detail">Details</a> section of the
2881      *          formatter class specification
2882      *
2883      * @throws  NullPointerException
2884      *          If the <tt>format</tt> is <tt>null</tt>
2885      *
2886      * @return  A formatted string
2887      *
2888      * @see  java.util.Formatter
2889      * @since  1.5
2890      */
2891     public static String format(Locale l, String format, Object ... args) {
2892         return new Formatter(l).format(format, args).toString();
2893     }
2894 
2895     /**
2896      * Returns the string representation of the <code>Object</code> argument.
2897      *
2898      * @param   obj   an <code>Object</code>.
2899      * @return  if the argument is <code>null</code>, then a string equal to
2900      *          <code>"null"</code>; otherwise, the value of
2901      *          <code>obj.toString()</code> is returned.
2902      * @see     java.lang.Object#toString()
2903      */
2904     public static String valueOf(Object obj) {
2905         return (obj == null) ? "null" : obj.toString();
2906     }
2907 
2908     /**
2909      * Returns the string representation of the <code>char</code> array
2910      * argument. The contents of the character array are copied; subsequent
2911      * modification of the character array does not affect the newly
2912      * created string.
2913      *
2914      * @param   data   a <code>char</code> array.
2915      * @return  a newly allocated string representing the same sequence of
2916      *          characters contained in the character array argument.
2917      */
2918     public static String valueOf(char data[]) {
2919         return new String(data);
2920     }
2921 
2922     /**
2923      * Returns the string representation of a specific subarray of the
2924      * <code>char</code> array argument.
2925      * <p>
2926      * The <code>offset</code> argument is the index of the first
2927      * character of the subarray. The <code>count</code> argument
2928      * specifies the length of the subarray. The contents of the subarray
2929      * are copied; subsequent modification of the character array does not
2930      * affect the newly created string.
2931      *
2932      * @param   data     the character array.
2933      * @param   offset   the initial offset into the value of the
2934      *                  <code>String</code>.
2935      * @param   count    the length of the value of the <code>String</code>.
2936      * @return  a string representing the sequence of characters contained
2937      *          in the subarray of the character array argument.
2938      * @exception IndexOutOfBoundsException if <code>offset</code> is
2939      *          negative, or <code>count</code> is negative, or
2940      *          <code>offset+count</code> is larger than
2941      *          <code>data.length</code>.
2942      */
2943     public static String valueOf(char data[], int offset, int count) {
2944         return new String(data, offset, count);
2945     }
2946 
2947     /**
2948      * Returns a String that represents the character sequence in the
2949      * array specified.
2950      *
2951      * @param   data     the character array.
2952      * @param   offset   initial offset of the subarray.
2953      * @param   count    length of the subarray.
2954      * @return  a <code>String</code> that contains the characters of the
2955      *          specified subarray of the character array.
2956      */
2957     public static String copyValueOf(char data[], int offset, int count) {
2958         // All public String constructors now copy the data.
2959         return new String(data, offset, count);
2960     }
2961 
2962     /**
2963      * Returns a String that represents the character sequence in the
2964      * array specified.
2965      *
2966      * @param   data   the character array.
2967      * @return  a <code>String</code> that contains the characters of the
2968      *          character array.
2969      */
2970     public static String copyValueOf(char data[]) {
2971         return copyValueOf(data, 0, data.length);
2972     }
2973 
2974     /**
2975      * Returns the string representation of the <code>boolean</code> argument.
2976      *
2977      * @param   b   a <code>boolean</code>.
2978      * @return  if the argument is <code>true</code>, a string equal to
2979      *          <code>"true"</code> is returned; otherwise, a string equal to
2980      *          <code>"false"</code> is returned.
2981      */
2982     public static String valueOf(boolean b) {
2983         return b ? "true" : "false";
2984     }
2985 
2986     /**
2987      * Returns the string representation of the <code>char</code>
2988      * argument.
2989      *
2990      * @param   c   a <code>char</code>.
2991      * @return  a string of length <code>1</code> containing
2992      *          as its single character the argument <code>c</code>.
2993      */
2994     public static String valueOf(char c) {
2995         char data[] = {c};
2996         return new String(0, 1, data);
2997     }
2998 
2999     /**
3000      * Returns the string representation of the <code>int</code> argument.
3001      * <p>
3002      * The representation is exactly the one returned by the
3003      * <code>Integer.toString</code> method of one argument.
3004      *
3005      * @param   i   an <code>int</code>.
3006      * @return  a string representation of the <code>int</code> argument.
3007      * @see     java.lang.Integer#toString(int, int)
3008      */
3009     public static String valueOf(int i) {
3010         return Integer.toString(i);
3011     }
3012 
3013     /**
3014      * Returns the string representation of the <code>long</code> argument.
3015      * <p>
3016      * The representation is exactly the one returned by the
3017      * <code>Long.toString</code> method of one argument.
3018      *
3019      * @param   l   a <code>long</code>.
3020      * @return  a string representation of the <code>long</code> argument.
3021      * @see     java.lang.Long#toString(long)
3022      */
3023     public static String valueOf(long l) {
3024         return Long.toString(l);
3025     }
3026 
3027     /**
3028      * Returns the string representation of the <code>float</code> argument.
3029      * <p>
3030      * The representation is exactly the one returned by the
3031      * <code>Float.toString</code> method of one argument.
3032      *
3033      * @param   f   a <code>float</code>.
3034      * @return  a string representation of the <code>float</code> argument.
3035      * @see     java.lang.Float#toString(float)
3036      */
3037     public static String valueOf(float f) {
3038         return Float.toString(f);
3039     }
3040 
3041     /**
3042      * Returns the string representation of the <code>double</code> argument.
3043      * <p>
3044      * The representation is exactly the one returned by the
3045      * <code>Double.toString</code> method of one argument.
3046      *
3047      * @param   d   a <code>double</code>.
3048      * @return  a  string representation of the <code>double</code> argument.
3049      * @see     java.lang.Double#toString(double)
3050      */
3051     public static String valueOf(double d) {
3052         return Double.toString(d);
3053     }
3054 
3055     /**
3056      * Returns a canonical representation for the string object.
3057      * <p>
3058      * A pool of strings, initially empty, is maintained privately by the
3059      * class <code>String</code>.
3060      * <p>
3061      * When the intern method is invoked, if the pool already contains a
3062      * string equal to this <code>String</code> object as determined by
3063      * the {@link #equals(Object)} method, then the string from the pool is
3064      * returned. Otherwise, this <code>String</code> object is added to the
3065      * pool and a reference to this <code>String</code> object is returned.
3066      * <p>
3067      * It follows that for any two strings <code>s</code> and <code>t</code>,
3068      * <code>s.intern()&nbsp;==&nbsp;t.intern()</code> is <code>true</code>
3069      * if and only if <code>s.equals(t)</code> is <code>true</code>.
3070      * <p>
3071      * All literal strings and string-valued constant expressions are
3072      * interned. String literals are defined in section 3.10.5 of the
3073      * <cite>The Java&trade; Language Specification</cite>.
3074      *
3075      * @return  a string that has the same contents as this string, but is
3076      *          guaranteed to be from a pool of unique strings.
3077      */
3078     public native String intern();
3079 
3080 }


  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang;
  27 
  28 import java.io.ObjectStreamClass;
  29 import java.io.ObjectStreamField;
  30 import java.io.UnsupportedEncodingException;
  31 import java.nio.charset.Charset;
  32 import java.util.ArrayList;
  33 import java.util.Arrays;
  34 import java.util.Comparator;
  35 import java.util.Formatter;
  36 import java.util.Locale;
  37 import java.util.regex.Matcher;
  38 import java.util.regex.Pattern;
  39 import java.util.regex.PatternSyntaxException;
  40 
  41 /**
  42  * The {@code String} class represents character strings. All
  43  * string literals in Java programs, such as {@code "abc"}, are
  44  * implemented as instances of this class.
  45  * <p>
  46  * Strings are constant; their values cannot be changed after they
  47  * are created. String buffers support mutable strings.
  48  * Because String objects are immutable they can be shared. For example:
  49  * <p><blockquote><pre>
  50  *     String str = "abc";
  51  * </pre></blockquote><p>
  52  * is equivalent to:
  53  * <p><blockquote><pre>
  54  *     char data[] = {'a', 'b', 'c'};
  55  *     String str = new String(data);
  56  * </pre></blockquote><p>
  57  * Here are some more examples of how strings can be used:
  58  * <p><blockquote><pre>
  59  *     System.out.println("abc");
  60  *     String cde = "cde";
  61  *     System.out.println("abc" + cde);
  62  *     String c = "abc".substring(2,3);
  63  *     String d = cde.substring(1, 2);
  64  * </pre></blockquote>
  65  * <p>
  66  * The class {@code String} includes methods for examining
  67  * individual characters of the sequence, for comparing strings, for
  68  * searching strings, for extracting substrings, and for creating a
  69  * copy of a string with all characters translated to uppercase or to
  70  * lowercase. Case mapping is based on the Unicode Standard version
  71  * specified by the {@link java.lang.Character Character} class.
  72  * <p>
  73  * The Java language provides special support for the string
  74  * concatenation operator (&nbsp;+&nbsp;), and for conversion of
  75  * other objects to strings. String concatenation is implemented
  76  * through the {@code StringBuilder}(or {@code StringBuffer})
  77  * class and its {@code append} method.
  78  * String conversions are implemented through the method
  79  * {@code toString}, defined by {@code Object} and
  80  * inherited by all classes in Java. For additional information on
  81  * string concatenation and conversion, see Gosling, Joy, and Steele,
  82  * <i>The Java Language Specification</i>.
  83  *
  84  * <p> Unless otherwise noted, passing a <tt>null</tt> argument to a constructor
  85  * or method in this class will cause a {@link NullPointerException} to be
  86  * thrown.
  87  *
  88  * <p>A {@code String} represents a string in the UTF-16 format
  89  * in which <em>supplementary characters</em> are represented by <em>surrogate
  90  * pairs</em> (see the section <a href="Character.html#unicode">Unicode
  91  * Character Representations</a> in the {@code Character} class for
  92  * more information).
  93  * Index values refer to {@code char} code units, so a supplementary
  94  * character uses two positions in a {@code String}.
  95  * <p>The {@code String} class provides methods for dealing with
  96  * Unicode code points (i.e., characters), in addition to those for
  97  * dealing with Unicode code units (i.e., {@code char} values).
  98  *
  99  * @author  Lee Boynton
 100  * @author  Arthur van Hoff
 101  * @author  Martin Buchholz
 102  * @author  Ulf Zibis
 103  * @see     java.lang.Object#toString()
 104  * @see     java.lang.StringBuffer
 105  * @see     java.lang.StringBuilder
 106  * @see     java.nio.charset.Charset
 107  * @since   JDK1.0
 108  */
 109 
 110 public final class String
 111     implements java.io.Serializable, Comparable<String>, CharSequence
 112 {
 113     /** The value is used for character storage. */
 114     private final char value[];
 115 
 116     /** The offset is the first index of the storage that is used. */
 117     private final int offset;
 118 
 119     /** The count is the number of characters in the String. */
 120     private final int count;
 121 
 122     /** Cache the hash code for the string */
 123     private int hash; // Default to 0
 124 
 125     /** use serialVersionUID from JDK 1.0.2 for interoperability */
 126     private static final long serialVersionUID = -6849794470754667710L;
 127 
 128     /**
 129      * Class String is special cased within the Serialization Stream Protocol.
 130      *
 131      * A String instance is written initially into an ObjectOutputStream in the
 132      * following format:
 133      * <pre>
 134      *      {@code TC_STRING} (utf String)
 135      * </pre>
 136      * The String is written by method {@code DataOutput.writeUTF}.
 137      * A new handle is generated to  refer to all future references to the
 138      * string instance within the stream.
 139      */
 140     private static final ObjectStreamField[] serialPersistentFields =
 141         new ObjectStreamField[0];
 142 
 143     /**
 144      * Initializes a newly created {@code String} object so that it represents
 145      * an empty character sequence.  Note that use of this constructor is
 146      * unnecessary since Strings are immutable.
 147      */
 148     public String() {
 149         this.offset = 0;
 150         this.count = 0;
 151         this.value = new char[0];
 152     }
 153 
 154     /**
 155      * Initializes a newly created {@code String} object so that it represents
 156      * the same sequence of characters as the argument; in other words, the


 656      * @return  the length of the sequence of characters represented by this
 657      *          object.
 658      */
 659     public int length() {
 660         return count;
 661     }
 662 
 663     /**
 664      * Returns <tt>true</tt> if, and only if, {@link #length()} is <tt>0</tt>.
 665      *
 666      * @return <tt>true</tt> if {@link #length()} is <tt>0</tt>, otherwise
 667      * <tt>false</tt>
 668      *
 669      * @since 1.6
 670      */
 671     public boolean isEmpty() {
 672         return count == 0;
 673     }
 674 
 675     /**
 676      * Returns the {@code char} value at the
 677      * specified index. An index ranges from {@code 0} to
 678      * {@code length() - 1}. The first {@code char} value of the sequence
 679      * is at index {@code 0}, the next at index {@code 1},
 680      * and so on, as for array indexing.
 681      *
 682      * <p>If the {@code char} value specified by the index is a
 683      * <a href="Character.html#unicode">surrogate</a>, the surrogate
 684      * value is returned.
 685      *
 686      * @param      index   the index of the {@code char} value.
 687      * @return     the {@code char} value at the specified index of this string.
 688      *             The first {@code char} value is at index {@code 0}.
 689      * @exception  IndexOutOfBoundsException  if the {@code index}
 690      *             argument is negative or not less than the length of this
 691      *             string.
 692      */
 693     public char charAt(int index) {
 694         if ((index < 0) || (index >= count)) {
 695             throw new StringIndexOutOfBoundsException(index);
 696         }
 697         return value[index + offset];
 698     }
 699 
 700     /**
 701      * Returns the character (Unicode code point) at the specified
 702      * index. The index refers to {@code char} values
 703      * (Unicode code units) and ranges from {@code 0} to
 704      * {@link #length()}{@code  - 1}.
 705      *
 706      * <p> If the {@code char} value specified at the given index
 707      * is in the high-surrogate range, the following index is less
 708      * than the length of this {@code String}, and the
 709      * {@code char} value at the following index is in the
 710      * low-surrogate range, then the supplementary code point
 711      * corresponding to this surrogate pair is returned. Otherwise,
 712      * the {@code char} value at the given index is returned.
 713      *
 714      * @param      index the index to the {@code char} values
 715      * @return     the code point value of the character at the
 716      *             {@code index}
 717      * @exception  IndexOutOfBoundsException  if the {@code index}
 718      *             argument is negative or not less than the length of this
 719      *             string.
 720      * @since      1.5
 721      */
 722     public int codePointAt(int index) {
 723         if ((index < 0) || (index >= count)) {
 724             throw new StringIndexOutOfBoundsException(index);
 725         }
 726         return Character.codePointAtImpl(value, offset + index, offset + count);
 727     }
 728 
 729     /**
 730      * Returns the character (Unicode code point) before the specified
 731      * index. The index refers to {@code char} values
 732      * (Unicode code units) and ranges from {@code 1} to {@link
 733      * CharSequence#length() length}.
 734      *
 735      * <p> If the {@code char} value at {@code (index - 1)}
 736      * is in the low-surrogate range, {@code (index - 2)} is not
 737      * negative, and the {@code char} value at {@code (index -
 738      * 2)} is in the high-surrogate range, then the
 739      * supplementary code point value of the surrogate pair is
 740      * returned. If the {@code char} value at {@code index -
 741      * 1} is an unpaired low-surrogate or a high-surrogate, the
 742      * surrogate value is returned.
 743      *
 744      * @param     index the index following the code point that should be returned
 745      * @return    the Unicode code point value before the given index.
 746      * @exception IndexOutOfBoundsException if the {@code index}
 747      *            argument is less than 1 or greater than the length
 748      *            of this string.
 749      * @since     1.5
 750      */
 751     public int codePointBefore(int index) {
 752         int i = index - 1;
 753         if ((i < 0) || (i >= count)) {
 754             throw new StringIndexOutOfBoundsException(index);
 755         }
 756         return Character.codePointBeforeImpl(value, offset + index, offset);
 757     }
 758 
 759     /**
 760      * Returns the number of Unicode code points in the specified text
 761      * range of this {@code String}. The text range begins at the
 762      * specified {@code beginIndex} and extends to the
 763      * {@code char} at index {@code endIndex - 1}. Thus the
 764      * length (in {@code char}s) of the text range is
 765      * {@code endIndex-beginIndex}. Unpaired surrogates within
 766      * the text range count as one code point each.
 767      *
 768      * @param beginIndex the index to the first {@code char} of
 769      * the text range.
 770      * @param endIndex the index after the last {@code char} of
 771      * the text range.
 772      * @return the number of Unicode code points in the specified text
 773      * range
 774      * @exception IndexOutOfBoundsException if the
 775      * {@code beginIndex} is negative, or {@code endIndex}
 776      * is larger than the length of this {@code String}, or
 777      * {@code beginIndex} is larger than {@code endIndex}.
 778      * @since  1.5
 779      */
 780     public int codePointCount(int beginIndex, int endIndex) {
 781         if (beginIndex < 0 || endIndex > count || beginIndex > endIndex) {
 782             throw new IndexOutOfBoundsException();
 783         }
 784         return Character.codePointCountImpl(value, offset+beginIndex, endIndex-beginIndex);
 785     }
 786 
 787     /**
 788      * Returns the index within this {@code String} that is
 789      * offset from the given {@code index} by
 790      * {@code codePointOffset} code points. Unpaired surrogates
 791      * within the text range given by {@code index} and
 792      * {@code codePointOffset} count as one code point each.
 793      *
 794      * @param index the index to be offset
 795      * @param codePointOffset the offset in code points
 796      * @return the index within this {@code String}
 797      * @exception IndexOutOfBoundsException if {@code index}
 798      *   is negative or larger then the length of this
 799      *   {@code String}, or if {@code codePointOffset} is positive
 800      *   and the substring starting with {@code index} has fewer
 801      *   than {@code codePointOffset} code points,
 802      *   or if {@code codePointOffset} is negative and the substring
 803      *   before {@code index} has fewer than the absolute value
 804      *   of {@code codePointOffset} code points.
 805      * @since 1.5
 806      */
 807     public int offsetByCodePoints(int index, int codePointOffset) {
 808         if (index < 0 || index > count) {
 809             throw new IndexOutOfBoundsException();
 810         }
 811         return Character.offsetByCodePointsImpl(value, offset, count,
 812                                                 offset+index, codePointOffset) - offset;
 813     }
 814 
 815     /**
 816      * Copy characters from this string into dst starting at dstBegin.
 817      * This method doesn't perform any range checking.
 818      */
 819     void getChars(char dst[], int dstBegin) {
 820         System.arraycopy(value, offset, dst, dstBegin, count);
 821     }
 822 
 823     /**
 824      * Copies characters from this string into the destination character
 825      * array.
 826      * <p>
 827      * The first character to be copied is at index {@code srcBegin};
 828      * the last character to be copied is at index {@code srcEnd-1}
 829      * (thus the total number of characters to be copied is
 830      * {@code srcEnd-srcBegin}). The characters are copied into the
 831      * subarray of {@code dst} starting at index {@code dstBegin}
 832      * and ending at index:
 833      * <p><blockquote><pre>
 834      *     dstbegin + (srcEnd-srcBegin) - 1
 835      * </pre></blockquote>
 836      *
 837      * @param      srcBegin   index of the first character in the string
 838      *                        to copy.
 839      * @param      srcEnd     index after the last character in the string
 840      *                        to copy.
 841      * @param      dst        the destination array.
 842      * @param      dstBegin   the start offset in the destination array.
 843      * @exception IndexOutOfBoundsException If any of the following
 844      *            is true:
 845      *            <ul><li>{@code srcBegin} is negative.
 846      *            <li>{@code srcBegin} is greater than {@code srcEnd}
 847      *            <li>{@code srcEnd} is greater than the length of this
 848      *                string
 849      *            <li>{@code dstBegin} is negative
 850      *            <li>{@code dstBegin+(srcEnd-srcBegin)} is larger than
 851      *                {@code dst.length}</ul>
 852      */
 853     public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {
 854         if (srcBegin < 0) {
 855             throw new StringIndexOutOfBoundsException(srcBegin);
 856         }
 857         if (srcEnd > count) {
 858             throw new StringIndexOutOfBoundsException(srcEnd);
 859         }
 860         if (srcBegin > srcEnd) {
 861             throw new StringIndexOutOfBoundsException(srcEnd - srcBegin);
 862         }
 863         System.arraycopy(value, offset + srcBegin, dst, dstBegin,
 864              srcEnd - srcBegin);
 865     }
 866 
 867     /**
 868      * Copies characters from this string into the destination byte array. Each
 869      * byte receives the 8 low-order bits of the corresponding character. The
 870      * eight high-order bits of each character are not copied and do not
 871      * participate in the transfer in any way.


1118      *
1119      * @param  anotherString
1120      *         The {@code String} to compare this {@code String} against
1121      *
1122      * @return  {@code true} if the argument is not {@code null} and it
1123      *          represents an equivalent {@code String} ignoring case; {@code
1124      *          false} otherwise
1125      *
1126      * @see  #equals(Object)
1127      */
1128     public boolean equalsIgnoreCase(String anotherString) {
1129         return (this == anotherString) ? true :
1130                (anotherString != null) && (anotherString.count == count) &&
1131                regionMatches(true, 0, anotherString, 0, count);
1132     }
1133 
1134     /**
1135      * Compares two strings lexicographically.
1136      * The comparison is based on the Unicode value of each character in
1137      * the strings. The character sequence represented by this
1138      * {@code String} object is compared lexicographically to the
1139      * character sequence represented by the argument string. The result is
1140      * a negative integer if this {@code String} object
1141      * lexicographically precedes the argument string. The result is a
1142      * positive integer if this {@code String} object lexicographically
1143      * follows the argument string. The result is zero if the strings
1144      * are equal; {@code compareTo} returns {@code 0} exactly when
1145      * the {@link #equals(Object)} method would return {@code true}.
1146      * <p>
1147      * This is the definition of lexicographic ordering. If two strings are
1148      * different, then either they have different characters at some index
1149      * that is a valid index for both strings, or their lengths are different,
1150      * or both. If they have different characters at one or more index
1151      * positions, let <i>k</i> be the smallest such index; then the string
1152      * whose character at position <i>k</i> has the smaller value, as
1153      * determined by using the &lt; operator, lexicographically precedes the
1154      * other string. In this case, {@code compareTo} returns the
1155      * difference of the two character values at position {@code k} in
1156      * the two string -- that is, the value:
1157      * <blockquote><pre>
1158      * this.charAt(k)-anotherString.charAt(k)
1159      * </pre></blockquote>
1160      * If there is no index position at which they differ, then the shorter
1161      * string lexicographically precedes the longer string. In this case,
1162      * {@code compareTo} returns the difference of the lengths of the
1163      * strings -- that is, the value:
1164      * <blockquote><pre>
1165      * this.length()-anotherString.length()
1166      * </pre></blockquote>
1167      *
1168      * @param   anotherString   the {@code String} to be compared.
1169      * @return  the value {@code 0} if the argument string is equal to
1170      *          this string; a value less than {@code 0} if this string
1171      *          is lexicographically less than the string argument; and a
1172      *          value greater than {@code 0} if this string is
1173      *          lexicographically greater than the string argument.
1174      */
1175     public int compareTo(String anotherString) {
1176         int len1 = count;
1177         int len2 = anotherString.count;
1178         int n = Math.min(len1, len2);
1179         char v1[] = value;
1180         char v2[] = anotherString.value;
1181         int i = offset;
1182         int j = anotherString.offset;
1183 
1184         if (i == j) {
1185             int k = i;
1186             int lim = n + i;
1187             while (k < lim) {
1188                 char c1 = v1[k];
1189                 char c2 = v2[k];
1190                 if (c1 != c2) {
1191                     return c1 - c2;
1192                 }
1193                 k++;
1194             }
1195         } else {
1196             while (n-- != 0) {
1197                 char c1 = v1[i++];
1198                 char c2 = v2[j++];
1199                 if (c1 != c2) {
1200                     return c1 - c2;
1201                 }
1202             }
1203         }
1204         return len1 - len2;
1205     }
1206 
1207     /**
1208      * A Comparator that orders {@code String} objects as by
1209      * {@code compareToIgnoreCase}. This comparator is serializable.
1210      * <p>
1211      * Note that this Comparator does <em>not</em> take locale into account,
1212      * and will result in an unsatisfactory ordering for certain locales.
1213      * The java.text package provides <em>Collators</em> to allow
1214      * locale-sensitive ordering.
1215      *
1216      * @see     java.text.Collator#compare(String, String)
1217      * @since   1.2
1218      */
1219     public static final Comparator<String> CASE_INSENSITIVE_ORDER
1220                                          = new CaseInsensitiveComparator();
1221     private static class CaseInsensitiveComparator
1222                          implements Comparator<String>, java.io.Serializable {
1223         // use serialVersionUID from JDK 1.2.2 for interoperability
1224         private static final long serialVersionUID = 8575799808933029326L;
1225 
1226         public int compare(String s1, String s2) {
1227             int n1 = s1.length();
1228             int n2 = s2.length();
1229             int min = Math.min(n1, n2);


1236                     if (c1 != c2) {
1237                         c1 = Character.toLowerCase(c1);
1238                         c2 = Character.toLowerCase(c2);
1239                         if (c1 != c2) {
1240                             // No overflow because of numeric promotion
1241                             return c1 - c2;
1242                         }
1243                     }
1244                 }
1245             }
1246             return n1 - n2;
1247         }
1248 
1249         /** Replaces the de-serialized object. */
1250         private Object readResolve() { return CASE_INSENSITIVE_ORDER; }
1251     }
1252 
1253     /**
1254      * Compares two strings lexicographically, ignoring case
1255      * differences. This method returns an integer whose sign is that of
1256      * calling {@code compareTo} with normalized versions of the strings
1257      * where case differences have been eliminated by calling
1258      * {@code Character.toLowerCase(Character.toUpperCase(character))} on
1259      * each character.
1260      * <p>
1261      * Note that this method does <em>not</em> take locale into account,
1262      * and will result in an unsatisfactory ordering for certain locales.
1263      * The java.text package provides <em>collators</em> to allow
1264      * locale-sensitive ordering.
1265      *
1266      * @param   str   the {@code String} to be compared.
1267      * @return  a negative integer, zero, or a positive integer as the
1268      *          specified String is greater than, equal to, or less
1269      *          than this String, ignoring case considerations.
1270      * @see     java.text.Collator#compare(String, String)
1271      * @since   1.2
1272      */
1273     public int compareToIgnoreCase(String str) {
1274         return CASE_INSENSITIVE_ORDER.compare(this, str);
1275     }
1276 
1277     /**
1278      * Tests if two string regions are equal.
1279      * <p>
1280      * A substring of this <tt>String</tt> object is compared to a substring
1281      * of the argument other. The result is true if these substrings
1282      * represent identical character sequences. The substring of this
1283      * <tt>String</tt> object to be compared begins at index <tt>toffset</tt>
1284      * and has length <tt>len</tt>. The substring of other to be compared
1285      * begins at index <tt>ooffset</tt> and has length <tt>len</tt>. The
1286      * result is <tt>false</tt> if and only if at least one of the following
1287      * is true:
1288      * <ul><li><tt>toffset</tt> is negative.
1289      * <li><tt>ooffset</tt> is negative.
1290      * <li><tt>toffset+len</tt> is greater than the length of this
1291      * <tt>String</tt> object.
1292      * <li><tt>ooffset+len</tt> is greater than the length of the other
1293      * argument.
1294      * <li>There is some nonnegative integer <i>k</i> less than <tt>len</tt>
1295      * such that:
1296      * <tt>this.charAt(toffset+<i>k</i>)&nbsp;!=&nbsp;other.charAt(ooffset+<i>k</i>)</tt>
1297      * </ul>
1298      *
1299      * @param   toffset   the starting offset of the subregion in this string.
1300      * @param   other     the string argument.
1301      * @param   ooffset   the starting offset of the subregion in the string
1302      *                    argument.
1303      * @param   len       the number of characters to compare.
1304      * @return  {@code true} if the specified subregion of this string
1305      *          exactly matches the specified subregion of the string argument;
1306      *          {@code false} otherwise.
1307      */
1308     public boolean regionMatches(int toffset, String other, int ooffset,
1309                                  int len) {
1310         char ta[] = value;
1311         int to = offset + toffset;
1312         char pa[] = other.value;
1313         int po = other.offset + ooffset;
1314         // Note: toffset, ooffset, or len might be near -1>>>1.
1315         if ((ooffset < 0) || (toffset < 0) || (toffset > (long)count - len)
1316             || (ooffset > (long)other.count - len)) {
1317             return false;
1318         }
1319         while (len-- > 0) {
1320             if (ta[to++] != pa[po++]) {
1321                 return false;
1322             }
1323         }
1324         return true;
1325     }
1326 


1343      * <li><tt>ooffset+len</tt> is greater than the length of the other
1344      * argument.
1345      * <li><tt>ignoreCase</tt> is <tt>false</tt> and there is some nonnegative
1346      * integer <i>k</i> less than <tt>len</tt> such that:
1347      * <blockquote><pre>
1348      * this.charAt(toffset+k) != other.charAt(ooffset+k)
1349      * </pre></blockquote>
1350      * <li><tt>ignoreCase</tt> is <tt>true</tt> and there is some nonnegative
1351      * integer <i>k</i> less than <tt>len</tt> such that:
1352      * <blockquote><pre>
1353      * Character.toLowerCase(this.charAt(toffset+k)) !=
1354                Character.toLowerCase(other.charAt(ooffset+k))
1355      * </pre></blockquote>
1356      * and:
1357      * <blockquote><pre>
1358      * Character.toUpperCase(this.charAt(toffset+k)) !=
1359      *         Character.toUpperCase(other.charAt(ooffset+k))
1360      * </pre></blockquote>
1361      * </ul>
1362      *
1363      * @param   ignoreCase   if {@code true}, ignore case when comparing
1364      *                       characters.
1365      * @param   toffset      the starting offset of the subregion in this
1366      *                       string.
1367      * @param   other        the string argument.
1368      * @param   ooffset      the starting offset of the subregion in the string
1369      *                       argument.
1370      * @param   len          the number of characters to compare.
1371      * @return  {@code true} if the specified subregion of this string
1372      *          matches the specified subregion of the string argument;
1373      *          {@code false} otherwise. Whether the matching is exact
1374      *          or case insensitive depends on the {@code ignoreCase}
1375      *          argument.
1376      */
1377     public boolean regionMatches(boolean ignoreCase, int toffset,
1378                            String other, int ooffset, int len) {
1379         char ta[] = value;
1380         int to = offset + toffset;
1381         char pa[] = other.value;
1382         int po = other.offset + ooffset;
1383         // Note: toffset, ooffset, or len might be near -1>>>1.
1384         if ((ooffset < 0) || (toffset < 0) || (toffset > (long)count - len) ||
1385                 (ooffset > (long)other.count - len)) {
1386             return false;
1387         }
1388         while (len-- > 0) {
1389             char c1 = ta[to++];
1390             char c2 = pa[po++];
1391             if (c1 == c2) {
1392                 continue;
1393             }
1394             if (ignoreCase) {


1403                 }
1404                 // Unfortunately, conversion to uppercase does not work properly
1405                 // for the Georgian alphabet, which has strange rules about case
1406                 // conversion.  So we need to make one last check before
1407                 // exiting.
1408                 if (Character.toLowerCase(u1) == Character.toLowerCase(u2)) {
1409                     continue;
1410                 }
1411             }
1412             return false;
1413         }
1414         return true;
1415     }
1416 
1417     /**
1418      * Tests if the substring of this string beginning at the
1419      * specified index starts with the specified prefix.
1420      *
1421      * @param   prefix    the prefix.
1422      * @param   toffset   where to begin looking in this string.
1423      * @return  {@code true} if the character sequence represented by the
1424      *          argument is a prefix of the substring of this object starting
1425      *          at index {@code toffset}; {@code false} otherwise.
1426      *          The result is {@code false} if {@code toffset} is
1427      *          negative or greater than the length of this
1428      *          {@code String} object; otherwise the result is the same
1429      *          as the result of the expression
1430      *          <pre>
1431      *          this.substring(toffset).startsWith(prefix)
1432      *          </pre>
1433      */
1434     public boolean startsWith(String prefix, int toffset) {
1435         char ta[] = value;
1436         int to = offset + toffset;
1437         char pa[] = prefix.value;
1438         int po = prefix.offset;
1439         int pc = prefix.count;
1440         // Note: toffset might be near -1>>>1.
1441         if ((toffset < 0) || (toffset > count - pc)) {
1442             return false;
1443         }
1444         while (--pc >= 0) {
1445             if (ta[to++] != pa[po++]) {
1446                 return false;
1447             }
1448         }
1449         return true;
1450     }
1451 
1452     /**
1453      * Tests if this string starts with the specified prefix.
1454      *
1455      * @param   prefix   the prefix.
1456      * @return  {@code true} if the character sequence represented by the
1457      *          argument is a prefix of the character sequence represented by
1458      *          this string; {@code false} otherwise.
1459      *          Note also that {@code true} will be returned if the
1460      *          argument is an empty string or is equal to this
1461      *          {@code String} object as determined by the
1462      *          {@link #equals(Object)} method.
1463      * @since   1. 0
1464      */
1465     public boolean startsWith(String prefix) {
1466         return startsWith(prefix, 0);
1467     }
1468 
1469     /**
1470      * Tests if this string ends with the specified suffix.
1471      *
1472      * @param   suffix   the suffix.
1473      * @return  {@code true} if the character sequence represented by the
1474      *          argument is a suffix of the character sequence represented by
1475      *          this object; {@code false} otherwise. Note that the
1476      *          result will be {@code true} if the argument is the
1477      *          empty string or is equal to this {@code String} object
1478      *          as determined by the {@link #equals(Object)} method.
1479      */
1480     public boolean endsWith(String suffix) {
1481         return startsWith(suffix, count - suffix.count);
1482     }
1483 
1484     /**
1485      * Returns a hash code for this string. The hash code for a
1486      * {@code String} object is computed as
1487      * <blockquote><pre>
1488      * s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
1489      * </pre></blockquote>
1490      * using {@code int} arithmetic, where {@code s[i]} is the
1491      * <i>i</i>th character of the string, {@code n} is the length of
1492      * the string, and {@code ^} indicates exponentiation.
1493      * (The hash value of the empty string is zero.)
1494      *
1495      * @return  a hash code value for this object.
1496      */
1497     public int hashCode() {
1498         int h = hash;
1499         if (h == 0 && count > 0) {
1500             int off = offset;
1501             char val[] = value;
1502             int len = count;
1503 
1504             for (int i = 0; i < len; i++) {
1505                 h = 31*h + val[off++];
1506             }
1507             hash = h;
1508         }
1509         return h;
1510     }
1511 
1512     /**
1513      * Returns the index within this string of the first occurrence of
1514      * the specified character. If a character with value
1515      * {@code ch} occurs in the character sequence represented by
1516      * this {@code String} object, then the index (in Unicode
1517      * code units) of the first such occurrence is returned. For
1518      * values of {@code ch} in the range from 0 to 0xFFFF
1519      * (inclusive), this is the smallest value <i>k</i> such that:
1520      * <blockquote><pre>
1521      * this.charAt(<i>k</i>) == ch
1522      * </pre></blockquote>
1523      * is true. For other values of {@code ch}, it is the
1524      * smallest value <i>k</i> such that:
1525      * <blockquote><pre>
1526      * this.codePointAt(<i>k</i>) == ch
1527      * </pre></blockquote>
1528      * is true. In either case, if no such character occurs in this
1529      * string, then {@code -1} is returned.
1530      *
1531      * @param   ch   a character (Unicode code point).
1532      * @return  the index of the first occurrence of the character in the
1533      *          character sequence represented by this object, or
1534      *          {@code -1} if the character does not occur.
1535      */
1536     public int indexOf(int ch) {
1537         return indexOf(ch, 0);
1538     }
1539 
1540     /**
1541      * Returns the index within this string of the first occurrence of the
1542      * specified character, starting the search at the specified index.
1543      * <p>
1544      * If a character with value {@code ch} occurs in the
1545      * character sequence represented by this {@code String}
1546      * object at an index no smaller than {@code fromIndex}, then
1547      * the index of the first such occurrence is returned. For values
1548      * of {@code ch} in the range from 0 to 0xFFFF (inclusive),
1549      * this is the smallest value <i>k</i> such that:
1550      * <blockquote><pre>
1551      * (this.charAt(<i>k</i>) == ch) && (<i>k</i> &gt;= fromIndex)
1552      * </pre></blockquote>
1553      * is true. For other values of {@code ch}, it is the
1554      * smallest value <i>k</i> such that:
1555      * <blockquote><pre>
1556      * (this.codePointAt(<i>k</i>) == ch) && (<i>k</i> &gt;= fromIndex)
1557      * </pre></blockquote>
1558      * is true. In either case, if no such character occurs in this
1559      * string at or after position {@code fromIndex}, then
1560      * {@code -1} is returned.
1561      *
1562      * <p>
1563      * There is no restriction on the value of {@code fromIndex}. If it
1564      * is negative, it has the same effect as if it were zero: this entire
1565      * string may be searched. If it is greater than the length of this
1566      * string, it has the same effect as if it were equal to the length of
1567      * this string: {@code -1} is returned.
1568      *
1569      * <p>All indices are specified in {@code char} values
1570      * (Unicode code units).
1571      *
1572      * @param   ch          a character (Unicode code point).
1573      * @param   fromIndex   the index to start the search from.
1574      * @return  the index of the first occurrence of the character in the
1575      *          character sequence represented by this object that is greater
1576      *          than or equal to {@code fromIndex}, or {@code -1}
1577      *          if the character does not occur.
1578      */
1579     public int indexOf(int ch, int fromIndex) {
1580         if (fromIndex < 0) {
1581             fromIndex = 0;
1582         } else if (fromIndex >= count) {
1583             // Note: fromIndex might be near -1>>>1.
1584             return -1;
1585         }
1586 
1587         if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) {
1588             // handle most cases here (ch is a BMP code point or a
1589             // negative value (invalid code point))
1590             final char[] value = this.value;
1591             final int offset = this.offset;
1592             final int max = offset + count;
1593             for (int i = offset + fromIndex; i < max ; i++) {
1594                 if (value[i] == ch) {
1595                     return i - offset;
1596                 }


1605      * Handles (rare) calls of indexOf with a supplementary character.
1606      */
1607     private int indexOfSupplementary(int ch, int fromIndex) {
1608         if (Character.isValidCodePoint(ch)) {
1609             final char[] value = this.value;
1610             final int offset = this.offset;
1611             final char hi = Character.highSurrogate(ch);
1612             final char lo = Character.lowSurrogate(ch);
1613             final int max = offset + count - 1;
1614             for (int i = offset + fromIndex; i < max; i++) {
1615                 if (value[i] == hi && value[i+1] == lo) {
1616                     return i - offset;
1617                 }
1618             }
1619         }
1620         return -1;
1621     }
1622 
1623     /**
1624      * Returns the index within this string of the last occurrence of
1625      * the specified character. For values of {@code ch} in the
1626      * range from 0 to 0xFFFF (inclusive), the index (in Unicode code
1627      * units) returned is the largest value <i>k</i> such that:
1628      * <blockquote><pre>
1629      * this.charAt(<i>k</i>) == ch
1630      * </pre></blockquote>
1631      * is true. For other values of {@code ch}, it is the
1632      * largest value <i>k</i> such that:
1633      * <blockquote><pre>
1634      * this.codePointAt(<i>k</i>) == ch
1635      * </pre></blockquote>
1636      * is true.  In either case, if no such character occurs in this
1637      * string, then {@code -1} is returned.  The
1638      * {@code String} is searched backwards starting at the last
1639      * character.
1640      *
1641      * @param   ch   a character (Unicode code point).
1642      * @return  the index of the last occurrence of the character in the
1643      *          character sequence represented by this object, or
1644      *          {@code -1} if the character does not occur.
1645      */
1646     public int lastIndexOf(int ch) {
1647         return lastIndexOf(ch, count - 1);
1648     }
1649 
1650     /**
1651      * Returns the index within this string of the last occurrence of
1652      * the specified character, searching backward starting at the
1653      * specified index. For values of {@code ch} in the range
1654      * from 0 to 0xFFFF (inclusive), the index returned is the largest
1655      * value <i>k</i> such that:
1656      * <blockquote><pre>
1657      * (this.charAt(<i>k</i>) == ch) && (<i>k</i> &lt;= fromIndex)
1658      * </pre></blockquote>
1659      * is true. For other values of {@code ch}, it is the
1660      * largest value <i>k</i> such that:
1661      * <blockquote><pre>
1662      * (this.codePointAt(<i>k</i>) == ch) && (<i>k</i> &lt;= fromIndex)
1663      * </pre></blockquote>
1664      * is true. In either case, if no such character occurs in this
1665      * string at or before position {@code fromIndex}, then
1666      * {@code -1} is returned.
1667      *
1668      * <p>All indices are specified in {@code char} values
1669      * (Unicode code units).
1670      *
1671      * @param   ch          a character (Unicode code point).
1672      * @param   fromIndex   the index to start the search from. There is no
1673      *          restriction on the value of {@code fromIndex}. If it is
1674      *          greater than or equal to the length of this string, it has
1675      *          the same effect as if it were equal to one less than the
1676      *          length of this string: this entire string may be searched.
1677      *          If it is negative, it has the same effect as if it were -1:
1678      *          -1 is returned.
1679      * @return  the index of the last occurrence of the character in the
1680      *          character sequence represented by this object that is less
1681      *          than or equal to {@code fromIndex}, or {@code -1}
1682      *          if the character does not occur before that point.
1683      */
1684     public int lastIndexOf(int ch, int fromIndex) {
1685         if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) {
1686             // handle most cases here (ch is a BMP code point or a
1687             // negative value (invalid code point))
1688             final char[] value = this.value;
1689             final int offset = this.offset;
1690             int i = offset + Math.min(fromIndex, count - 1);
1691             for (; i >= offset ; i--) {
1692                 if (value[i] == ch) {
1693                     return i - offset;
1694                 }
1695             }
1696             return -1;
1697         } else {
1698             return lastIndexOfSupplementary(ch, fromIndex);
1699         }
1700     }
1701 


1904                 }
1905             }
1906             return start - sourceOffset + 1;
1907         }
1908     }
1909 
1910     /**
1911      * Returns a new string that is a substring of this string. The
1912      * substring begins with the character at the specified index and
1913      * extends to the end of this string. <p>
1914      * Examples:
1915      * <blockquote><pre>
1916      * "unhappy".substring(2) returns "happy"
1917      * "Harbison".substring(3) returns "bison"
1918      * "emptiness".substring(9) returns "" (an empty string)
1919      * </pre></blockquote>
1920      *
1921      * @param      beginIndex   the beginning index, inclusive.
1922      * @return     the specified substring.
1923      * @exception  IndexOutOfBoundsException  if
1924      *             {@code beginIndex} is negative or larger than the
1925      *             length of this {@code String} object.
1926      */
1927     public String substring(int beginIndex) {
1928         return substring(beginIndex, count);
1929     }
1930 
1931     /**
1932      * Returns a new string that is a substring of this string. The
1933      * substring begins at the specified {@code beginIndex} and
1934      * extends to the character at index {@code endIndex - 1}.
1935      * Thus the length of the substring is {@code endIndex-beginIndex}.
1936      * <p>
1937      * Examples:
1938      * <blockquote><pre>
1939      * "hamburger".substring(4, 8) returns "urge"
1940      * "smiles".substring(1, 5) returns "mile"
1941      * </pre></blockquote>
1942      *
1943      * @param      beginIndex   the beginning index, inclusive.
1944      * @param      endIndex     the ending index, exclusive.
1945      * @return     the specified substring.
1946      * @exception  IndexOutOfBoundsException  if the
1947      *             {@code beginIndex} is negative, or
1948      *             {@code endIndex} is larger than the length of
1949      *             this {@code String} object, or
1950      *             {@code beginIndex} is larger than
1951      *             {@code endIndex}.
1952      */
1953     public String substring(int beginIndex, int endIndex) {
1954         if (beginIndex < 0) {
1955             throw new StringIndexOutOfBoundsException(beginIndex);
1956         }
1957         if (endIndex > count) {
1958             throw new StringIndexOutOfBoundsException(endIndex);
1959         }
1960         if (beginIndex > endIndex) {
1961             throw new StringIndexOutOfBoundsException(endIndex - beginIndex);
1962         }
1963         return ((beginIndex == 0) && (endIndex == count)) ? this :
1964             new String(offset + beginIndex, endIndex - beginIndex, value);
1965     }
1966 
1967     /**
1968      * Returns a new character sequence that is a subsequence of this sequence.
1969      *
1970      * <p> An invocation of this method of the form
1971      *


1982      *
1983      * @param      beginIndex   the begin index, inclusive.
1984      * @param      endIndex     the end index, exclusive.
1985      * @return     the specified subsequence.
1986      *
1987      * @throws  IndexOutOfBoundsException
1988      *          if <tt>beginIndex</tt> or <tt>endIndex</tt> are negative,
1989      *          if <tt>endIndex</tt> is greater than <tt>length()</tt>,
1990      *          or if <tt>beginIndex</tt> is greater than <tt>startIndex</tt>
1991      *
1992      * @since 1.4
1993      * @spec JSR-51
1994      */
1995     public CharSequence subSequence(int beginIndex, int endIndex) {
1996         return this.substring(beginIndex, endIndex);
1997     }
1998 
1999     /**
2000      * Concatenates the specified string to the end of this string.
2001      * <p>
2002      * If the length of the argument string is {@code 0}, then this
2003      * {@code String} object is returned. Otherwise, a new
2004      * {@code String} object is created, representing a character
2005      * sequence that is the concatenation of the character sequence
2006      * represented by this {@code String} object and the character
2007      * sequence represented by the argument string.<p>
2008      * Examples:
2009      * <blockquote><pre>
2010      * "cares".concat("s") returns "caress"
2011      * "to".concat("get").concat("her") returns "together"
2012      * </pre></blockquote>
2013      *
2014      * @param   str   the {@code String} that is concatenated to the end
2015      *                of this {@code String}.
2016      * @return  a string that represents the concatenation of this object's
2017      *          characters followed by the string argument's characters.
2018      */
2019     public String concat(String str) {
2020         int otherLen = str.length();
2021         if (otherLen == 0) {
2022             return this;
2023         }
2024         char buf[] = new char[count + otherLen];
2025         getChars(0, count, buf, 0);
2026         str.getChars(0, otherLen, buf, count);
2027         return new String(0, count + otherLen, buf);
2028     }
2029 
2030     /**
2031      * Returns a new string resulting from replacing all occurrences of
2032      * {@code oldChar} in this string with {@code newChar}.
2033      * <p>
2034      * If the character {@code oldChar} does not occur in the
2035      * character sequence represented by this {@code String} object,
2036      * then a reference to this {@code String} object is returned.
2037      * Otherwise, a new {@code String} object is created that
2038      * represents a character sequence identical to the character sequence
2039      * represented by this {@code String} object, except that every
2040      * occurrence of {@code oldChar} is replaced by an occurrence
2041      * of {@code newChar}.
2042      * <p>
2043      * Examples:
2044      * <blockquote><pre>
2045      * "mesquite in your cellar".replace('e', 'o')
2046      *         returns "mosquito in your collar"
2047      * "the war of baronets".replace('r', 'y')
2048      *         returns "the way of bayonets"
2049      * "sparring with a purple porpoise".replace('p', 't')
2050      *         returns "starring with a turtle tortoise"
2051      * "JonL".replace('q', 'x') returns "JonL" (no change)
2052      * </pre></blockquote>
2053      *
2054      * @param   oldChar   the old character.
2055      * @param   newChar   the new character.
2056      * @return  a string derived from this string by replacing every
2057      *          occurrence of {@code oldChar} with {@code newChar}.
2058      */
2059     public String replace(char oldChar, char newChar) {
2060         if (oldChar != newChar) {
2061             int len = count;
2062             int i = -1;
2063             char[] val = value; /* avoid getfield opcode */
2064             int off = offset;   /* avoid getfield opcode */
2065 
2066             while (++i < len) {
2067                 if (val[off + i] == oldChar) {
2068                     break;
2069                 }
2070             }
2071             if (i < len) {
2072                 char buf[] = new char[len];
2073                 for (int j = 0 ; j < i ; j++) {
2074                     buf[j] = val[off+j];
2075                 }
2076                 while (i < len) {
2077                     char c = val[off + i];


2102      * @return  <tt>true</tt> if, and only if, this string matches the
2103      *          given regular expression
2104      *
2105      * @throws  PatternSyntaxException
2106      *          if the regular expression's syntax is invalid
2107      *
2108      * @see java.util.regex.Pattern
2109      *
2110      * @since 1.4
2111      * @spec JSR-51
2112      */
2113     public boolean matches(String regex) {
2114         return Pattern.matches(regex, this);
2115     }
2116 
2117     /**
2118      * Returns true if and only if this string contains the specified
2119      * sequence of char values.
2120      *
2121      * @param s the sequence to search for
2122      * @return true if this string contains {@code s}, false otherwise
2123      * @throws NullPointerException if {@code s} is {@code null}
2124      * @since 1.5
2125      */
2126     public boolean contains(CharSequence s) {
2127         return indexOf(s.toString()) > -1;
2128     }
2129 
2130     /**
2131      * Replaces the first substring of this string that matches the given <a
2132      * href="../util/regex/Pattern.html#sum">regular expression</a> with the
2133      * given replacement.
2134      *
2135      * <p> An invocation of this method of the form
2136      * <i>str</i><tt>.replaceFirst(</tt><i>regex</i><tt>,</tt> <i>repl</i><tt>)</tt>
2137      * yields exactly the same result as the expression
2138      *
2139      * <blockquote><tt>
2140      * {@link java.util.regex.Pattern}.{@link java.util.regex.Pattern#compile
2141      * compile}(</tt><i>regex</i><tt>).{@link
2142      * java.util.regex.Pattern#matcher(java.lang.CharSequence)
2143      * matcher}(</tt><i>str</i><tt>).{@link java.util.regex.Matcher#replaceFirst


2206      *
2207      * @see java.util.regex.Pattern
2208      *
2209      * @since 1.4
2210      * @spec JSR-51
2211      */
2212     public String replaceAll(String regex, String replacement) {
2213         return Pattern.compile(regex).matcher(this).replaceAll(replacement);
2214     }
2215 
2216     /**
2217      * Replaces each substring of this string that matches the literal target
2218      * sequence with the specified literal replacement sequence. The
2219      * replacement proceeds from the beginning of the string to the end, for
2220      * example, replacing "aa" with "b" in the string "aaa" will result in
2221      * "ba" rather than "ab".
2222      *
2223      * @param  target The sequence of char values to be replaced
2224      * @param  replacement The replacement sequence of char values
2225      * @return  The resulting string
2226      * @throws NullPointerException if {@code target} or
2227      *         {@code replacement} is {@code null}.
2228      * @since 1.5
2229      */
2230     public String replace(CharSequence target, CharSequence replacement) {
2231         return Pattern.compile(target.toString(), Pattern.LITERAL).matcher(
2232             this).replaceAll(Matcher.quoteReplacement(replacement.toString()));
2233     }
2234 
2235     /**
2236      * Splits this string around matches of the given
2237      * <a href="../util/regex/Pattern.html#sum">regular expression</a>.
2238      *
2239      * <p> The array returned by this method contains each substring of this
2240      * string that is terminated by another substring that matches the given
2241      * expression or is terminated by the end of the string.  The substrings in
2242      * the array are in the order in which they occur in this string.  If the
2243      * expression does not match any part of the input then the resulting array
2244      * has just one element, namely this string.
2245      *
2246      * <p> The <tt>limit</tt> parameter controls the number of times the
2247      * pattern is applied and therefore affects the length of the resulting


2390      *
2391      * @param  regex
2392      *         the delimiting regular expression
2393      *
2394      * @return  the array of strings computed by splitting this string
2395      *          around matches of the given regular expression
2396      *
2397      * @throws  PatternSyntaxException
2398      *          if the regular expression's syntax is invalid
2399      *
2400      * @see java.util.regex.Pattern
2401      *
2402      * @since 1.4
2403      * @spec JSR-51
2404      */
2405     public String[] split(String regex) {
2406         return split(regex, 0);
2407     }
2408 
2409     /**
2410      * Converts all of the characters in this {@code String} to lower
2411      * case using the rules of the given {@code Locale}.  Case mapping is based
2412      * on the Unicode Standard version specified by the {@link java.lang.Character Character}
2413      * class. Since case mappings are not always 1:1 char mappings, the resulting
2414      * {@code String} may be a different length than the original {@code String}.
2415      * <p>
2416      * Examples of lowercase  mappings are in the following table:
2417      * <table border="1" summary="Lowercase mapping examples showing language code of locale, upper case, lower case, and description">
2418      * <tr>
2419      *   <th>Language Code of Locale</th>
2420      *   <th>Upper Case</th>
2421      *   <th>Lower Case</th>
2422      *   <th>Description</th>
2423      * </tr>
2424      * <tr>
2425      *   <td>tr (Turkish)</td>
2426      *   <td>&#92;u0130</td>
2427      *   <td>&#92;u0069</td>
2428      *   <td>capital letter I with dot above -&gt; small letter i</td>
2429      * </tr>
2430      * <tr>
2431      *   <td>tr (Turkish)</td>
2432      *   <td>&#92;u0049</td>
2433      *   <td>&#92;u0131</td>
2434      *   <td>capital letter I -&gt; small letter dotless i </td>
2435      * </tr>
2436      * <tr>
2437      *   <td>(all)</td>
2438      *   <td>French Fries</td>
2439      *   <td>french fries</td>
2440      *   <td>lowercased all chars in String</td>
2441      * </tr>
2442      * <tr>
2443      *   <td>(all)</td>
2444      *   <td><img src="doc-files/capiota.gif" alt="capiota"><img src="doc-files/capchi.gif" alt="capchi">
2445      *       <img src="doc-files/captheta.gif" alt="captheta"><img src="doc-files/capupsil.gif" alt="capupsil">
2446      *       <img src="doc-files/capsigma.gif" alt="capsigma"></td>
2447      *   <td><img src="doc-files/iota.gif" alt="iota"><img src="doc-files/chi.gif" alt="chi">
2448      *       <img src="doc-files/theta.gif" alt="theta"><img src="doc-files/upsilon.gif" alt="upsilon">
2449      *       <img src="doc-files/sigma1.gif" alt="sigma"></td>
2450      *   <td>lowercased all chars in String</td>
2451      * </tr>
2452      * </table>
2453      *
2454      * @param locale use the case transformation rules for this locale
2455      * @return the {@code String}, converted to lowercase.
2456      * @see     java.lang.String#toLowerCase()
2457      * @see     java.lang.String#toUpperCase()
2458      * @see     java.lang.String#toUpperCase(Locale)
2459      * @since   1.1
2460      */
2461     public String toLowerCase(Locale locale) {
2462         if (locale == null) {
2463             throw new NullPointerException();
2464         }
2465 
2466         int     firstUpper;
2467 
2468         /* Now check if there are any characters that need to be changed. */
2469         scan: {
2470             for (firstUpper = 0 ; firstUpper < count; ) {
2471                 char c = value[offset+firstUpper];
2472                 if ((c >= Character.MIN_HIGH_SURROGATE) &&
2473                     (c <= Character.MAX_HIGH_SURROGATE)) {
2474                     int supplChar = codePointAt(firstUpper);
2475                     if (supplChar != Character.toLowerCase(supplChar)) {


2536                 /* Grow result if needed */
2537                 int mapLen = lowerCharArray.length;
2538                 if (mapLen > srcCount) {
2539                     char[] result2 = new char[result.length + mapLen - srcCount];
2540                     System.arraycopy(result, 0, result2, 0,
2541                         i + resultOffset);
2542                     result = result2;
2543                 }
2544                 for (int x=0; x<mapLen; ++x) {
2545                     result[i+resultOffset+x] = lowerCharArray[x];
2546                 }
2547                 resultOffset += (mapLen - srcCount);
2548             } else {
2549                 result[i+resultOffset] = (char)lowerChar;
2550             }
2551         }
2552         return new String(0, count+resultOffset, result);
2553     }
2554 
2555     /**
2556      * Converts all of the characters in this {@code String} to lower
2557      * case using the rules of the default locale. This is equivalent to calling
2558      * {@code toLowerCase(Locale.getDefault())}.
2559      * <p>
2560      * <b>Note:</b> This method is locale sensitive, and may produce unexpected
2561      * results if used for strings that are intended to be interpreted locale
2562      * independently.
2563      * Examples are programming language identifiers, protocol keys, and HTML
2564      * tags.
2565      * For instance, {@code "TITLE".toLowerCase()} in a Turkish locale
2566      * returns {@code "t\u005Cu0131tle"}, where '\u005Cu0131' is the
2567      * LATIN SMALL LETTER DOTLESS I character.
2568      * To obtain correct results for locale insensitive strings, use
2569      * {@code toLowerCase(Locale.ENGLISH)}.
2570      * <p>
2571      * @return  the {@code String}, converted to lowercase.
2572      * @see     java.lang.String#toLowerCase(Locale)
2573      */
2574     public String toLowerCase() {
2575         return toLowerCase(Locale.getDefault());
2576     }
2577 
2578     /**
2579      * Converts all of the characters in this {@code String} to upper
2580      * case using the rules of the given {@code Locale}. Case mapping is based
2581      * on the Unicode Standard version specified by the {@link java.lang.Character Character}
2582      * class. Since case mappings are not always 1:1 char mappings, the resulting
2583      * {@code String} may be a different length than the original {@code String}.
2584      * <p>
2585      * Examples of locale-sensitive and 1:M case mappings are in the following table.
2586      * <p>
2587      * <table border="1" summary="Examples of locale-sensitive and 1:M case mappings. Shows Language code of locale, lower case, upper case, and description.">
2588      * <tr>
2589      *   <th>Language Code of Locale</th>
2590      *   <th>Lower Case</th>
2591      *   <th>Upper Case</th>
2592      *   <th>Description</th>
2593      * </tr>
2594      * <tr>
2595      *   <td>tr (Turkish)</td>
2596      *   <td>&#92;u0069</td>
2597      *   <td>&#92;u0130</td>
2598      *   <td>small letter i -&gt; capital letter I with dot above</td>
2599      * </tr>
2600      * <tr>
2601      *   <td>tr (Turkish)</td>
2602      *   <td>&#92;u0131</td>
2603      *   <td>&#92;u0049</td>
2604      *   <td>small letter dotless i -&gt; capital letter I</td>
2605      * </tr>
2606      * <tr>
2607      *   <td>(all)</td>
2608      *   <td>&#92;u00df</td>
2609      *   <td>&#92;u0053 &#92;u0053</td>
2610      *   <td>small letter sharp s -&gt; two letters: SS</td>
2611      * </tr>
2612      * <tr>
2613      *   <td>(all)</td>
2614      *   <td>Fahrvergn&uuml;gen</td>
2615      *   <td>FAHRVERGN&Uuml;GEN</td>
2616      *   <td></td>
2617      * </tr>
2618      * </table>
2619      * @param locale use the case transformation rules for this locale
2620      * @return the {@code String}, converted to uppercase.
2621      * @see     java.lang.String#toUpperCase()
2622      * @see     java.lang.String#toLowerCase()
2623      * @see     java.lang.String#toLowerCase(Locale)
2624      * @since   1.1
2625      */
2626     public String toUpperCase(Locale locale) {
2627         if (locale == null) {
2628             throw new NullPointerException();
2629         }
2630 
2631         int     firstLower;
2632 
2633         /* Now check if there are any characters that need to be changed. */
2634         scan: {
2635             for (firstLower = 0 ; firstLower < count; ) {
2636                 int c = (int)value[offset+firstLower];
2637                 int srcCount;
2638                 if ((c >= Character.MIN_HIGH_SURROGATE) &&
2639                     (c <= Character.MAX_HIGH_SURROGATE)) {
2640                     c = codePointAt(firstLower);


2699                 /* Grow result if needed */
2700                 int mapLen = upperCharArray.length;
2701                 if (mapLen > srcCount) {
2702                     char[] result2 = new char[result.length + mapLen - srcCount];
2703                     System.arraycopy(result, 0, result2, 0,
2704                         i + resultOffset);
2705                     result = result2;
2706                 }
2707                 for (int x=0; x<mapLen; ++x) {
2708                     result[i+resultOffset+x] = upperCharArray[x];
2709                 }
2710                 resultOffset += (mapLen - srcCount);
2711             } else {
2712                 result[i+resultOffset] = (char)upperChar;
2713             }
2714         }
2715         return new String(0, count+resultOffset, result);
2716     }
2717 
2718     /**
2719      * Converts all of the characters in this {@code String} to upper
2720      * case using the rules of the default locale. This method is equivalent to
2721      * {@code toUpperCase(Locale.getDefault())}.
2722      * <p>
2723      * <b>Note:</b> This method is locale sensitive, and may produce unexpected
2724      * results if used for strings that are intended to be interpreted locale
2725      * independently.
2726      * Examples are programming language identifiers, protocol keys, and HTML
2727      * tags.
2728      * For instance, {@code "title".toUpperCase()} in a Turkish locale
2729      * returns {@code "T\u005Cu0130TLE"}, where '\u005Cu0130' is the
2730      * LATIN CAPITAL LETTER I WITH DOT ABOVE character.
2731      * To obtain correct results for locale insensitive strings, use
2732      * {@code toUpperCase(Locale.ENGLISH)}.
2733      * <p>
2734      * @return  the {@code String}, converted to uppercase.
2735      * @see     java.lang.String#toUpperCase(Locale)
2736      */
2737     public String toUpperCase() {
2738         return toUpperCase(Locale.getDefault());
2739     }
2740 
2741     /**
2742      * Returns a copy of the string, with leading and trailing whitespace
2743      * omitted.
2744      * <p>
2745      * If this {@code String} object represents an empty character
2746      * sequence, or the first and last characters of character sequence
2747      * represented by this {@code String} object both have codes
2748      * greater than {@code '\u005Cu0020'} (the space character), then a
2749      * reference to this {@code String} object is returned.
2750      * <p>
2751      * Otherwise, if there is no character with a code greater than
2752      * {@code '\u005Cu0020'} in the string, then a new
2753      * {@code String} object representing an empty string is created
2754      * and returned.
2755      * <p>
2756      * Otherwise, let <i>k</i> be the index of the first character in the
2757      * string whose code is greater than {@code '\u005Cu0020'}, and let
2758      * <i>m</i> be the index of the last character in the string whose code
2759      * is greater than {@code '\u005Cu0020'}. A new {@code String}
2760      * object is created, representing the substring of this string that
2761      * begins with the character at index <i>k</i> and ends with the
2762      * character at index <i>m</i>-that is, the result of
2763      * <code>this.substring(<i>k</i>,&nbsp;<i>m</i>+1)</code>.
2764      * <p>
2765      * This method may be used to trim whitespace (as defined above) from
2766      * the beginning and end of a string.
2767      *
2768      * @return  A copy of this string with leading and trailing white
2769      *          space removed, or this string if it has no leading or
2770      *          trailing white space.
2771      */
2772     public String trim() {
2773         int len = count;
2774         int st = 0;
2775         int off = offset;      /* avoid getfield opcode */
2776         char[] val = value;    /* avoid getfield opcode */
2777 
2778         while ((st < len) && (val[off + st] <= ' ')) {
2779             st++;


2876      *          specifier that is incompatible with the given arguments,
2877      *          insufficient arguments given the format string, or other
2878      *          illegal conditions.  For specification of all possible
2879      *          formatting errors, see the <a
2880      *          href="../util/Formatter.html#detail">Details</a> section of the
2881      *          formatter class specification
2882      *
2883      * @throws  NullPointerException
2884      *          If the <tt>format</tt> is <tt>null</tt>
2885      *
2886      * @return  A formatted string
2887      *
2888      * @see  java.util.Formatter
2889      * @since  1.5
2890      */
2891     public static String format(Locale l, String format, Object ... args) {
2892         return new Formatter(l).format(format, args).toString();
2893     }
2894 
2895     /**
2896      * Returns the string representation of the {@code Object} argument.
2897      *
2898      * @param   obj   an {@code Object}.
2899      * @return  if the argument is {@code null}, then a string equal to
2900      *          {@code "null"}; otherwise, the value of
2901      *          {@code obj.toString()} is returned.
2902      * @see     java.lang.Object#toString()
2903      */
2904     public static String valueOf(Object obj) {
2905         return (obj == null) ? "null" : obj.toString();
2906     }
2907 
2908     /**
2909      * Returns the string representation of the {@code char} array
2910      * argument. The contents of the character array are copied; subsequent
2911      * modification of the character array does not affect the newly
2912      * created string.
2913      *
2914      * @param   data   a {@code char} array.
2915      * @return  a newly allocated string representing the same sequence of
2916      *          characters contained in the character array argument.
2917      */
2918     public static String valueOf(char data[]) {
2919         return new String(data);
2920     }
2921 
2922     /**
2923      * Returns the string representation of a specific subarray of the
2924      * {@code char} array argument.
2925      * <p>
2926      * The {@code offset} argument is the index of the first
2927      * character of the subarray. The {@code count} argument
2928      * specifies the length of the subarray. The contents of the subarray
2929      * are copied; subsequent modification of the character array does not
2930      * affect the newly created string.
2931      *
2932      * @param   data     the character array.
2933      * @param   offset   the initial offset into the value of the
2934      *                  {@code String}.
2935      * @param   count    the length of the value of the {@code String}.
2936      * @return  a string representing the sequence of characters contained
2937      *          in the subarray of the character array argument.
2938      * @exception IndexOutOfBoundsException if {@code offset} is
2939      *          negative, or {@code count} is negative, or
2940      *          {@code offset+count} is larger than
2941      *          {@code data.length}.
2942      */
2943     public static String valueOf(char data[], int offset, int count) {
2944         return new String(data, offset, count);
2945     }
2946 
2947     /**
2948      * Returns a String that represents the character sequence in the
2949      * array specified.
2950      *
2951      * @param   data     the character array.
2952      * @param   offset   initial offset of the subarray.
2953      * @param   count    length of the subarray.
2954      * @return  a {@code String} that contains the characters of the
2955      *          specified subarray of the character array.
2956      */
2957     public static String copyValueOf(char data[], int offset, int count) {
2958         // All public String constructors now copy the data.
2959         return new String(data, offset, count);
2960     }
2961 
2962     /**
2963      * Returns a String that represents the character sequence in the
2964      * array specified.
2965      *
2966      * @param   data   the character array.
2967      * @return  a {@code String} that contains the characters of the
2968      *          character array.
2969      */
2970     public static String copyValueOf(char data[]) {
2971         return copyValueOf(data, 0, data.length);
2972     }
2973 
2974     /**
2975      * Returns the string representation of the {@code boolean} argument.
2976      *
2977      * @param   b   a {@code boolean}.
2978      * @return  if the argument is {@code true}, a string equal to
2979      *          {@code "true"} is returned; otherwise, a string equal to
2980      *          {@code "false"} is returned.
2981      */
2982     public static String valueOf(boolean b) {
2983         return b ? "true" : "false";
2984     }
2985 
2986     /**
2987      * Returns the string representation of the {@code char}
2988      * argument.
2989      *
2990      * @param   c   a {@code char}.
2991      * @return  a string of length {@code 1} containing
2992      *          as its single character the argument {@code c}.
2993      */
2994     public static String valueOf(char c) {
2995         char data[] = {c};
2996         return new String(0, 1, data);
2997     }
2998 
2999     /**
3000      * Returns the string representation of the {@code int} argument.
3001      * <p>
3002      * The representation is exactly the one returned by the
3003      * {@code Integer.toString} method of one argument.
3004      *
3005      * @param   i   an {@code int}.
3006      * @return  a string representation of the {@code int} argument.
3007      * @see     java.lang.Integer#toString(int, int)
3008      */
3009     public static String valueOf(int i) {
3010         return Integer.toString(i);
3011     }
3012 
3013     /**
3014      * Returns the string representation of the {@code long} argument.
3015      * <p>
3016      * The representation is exactly the one returned by the
3017      * {@code Long.toString} method of one argument.
3018      *
3019      * @param   l   a {@code long}.
3020      * @return  a string representation of the {@code long} argument.
3021      * @see     java.lang.Long#toString(long)
3022      */
3023     public static String valueOf(long l) {
3024         return Long.toString(l);
3025     }
3026 
3027     /**
3028      * Returns the string representation of the {@code float} argument.
3029      * <p>
3030      * The representation is exactly the one returned by the
3031      * {@code Float.toString} method of one argument.
3032      *
3033      * @param   f   a {@code float}.
3034      * @return  a string representation of the {@code float} argument.
3035      * @see     java.lang.Float#toString(float)
3036      */
3037     public static String valueOf(float f) {
3038         return Float.toString(f);
3039     }
3040 
3041     /**
3042      * Returns the string representation of the {@code double} argument.
3043      * <p>
3044      * The representation is exactly the one returned by the
3045      * {@code Double.toString} method of one argument.
3046      *
3047      * @param   d   a {@code double}.
3048      * @return  a  string representation of the {@code double} argument.
3049      * @see     java.lang.Double#toString(double)
3050      */
3051     public static String valueOf(double d) {
3052         return Double.toString(d);
3053     }
3054 
3055     /**
3056      * Returns a canonical representation for the string object.
3057      * <p>
3058      * A pool of strings, initially empty, is maintained privately by the
3059      * class {@code String}.
3060      * <p>
3061      * When the intern method is invoked, if the pool already contains a
3062      * string equal to this {@code String} object as determined by
3063      * the {@link #equals(Object)} method, then the string from the pool is
3064      * returned. Otherwise, this {@code String} object is added to the
3065      * pool and a reference to this {@code String} object is returned.
3066      * <p>
3067      * It follows that for any two strings {@code s} and {@code t},
3068      * {@code s.intern() == t.intern()} is {@code true}
3069      * if and only if {@code s.equals(t)} is {@code true}.
3070      * <p>
3071      * All literal strings and string-valued constant expressions are
3072      * interned. String literals are defined in section 3.10.5 of the
3073      * <cite>The Java&trade; Language Specification</cite>.
3074      *
3075      * @return  a string that has the same contents as this string, but is
3076      *          guaranteed to be from a pool of unique strings.
3077      */
3078     public native String intern();
3079 
3080 }