KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > lang > String


1 /*
2  * @(#)String.java 1.189 05/10/21
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.lang;
9
10 import java.io.ObjectStreamClass JavaDoc;
11 import java.io.ObjectStreamField JavaDoc;
12 import java.io.UnsupportedEncodingException JavaDoc;
13 import java.util.ArrayList JavaDoc;
14 import java.util.Comparator JavaDoc;
15 import java.util.Formatter JavaDoc;
16 import java.util.Locale JavaDoc;
17 import java.util.regex.Matcher JavaDoc;
18 import java.util.regex.Pattern JavaDoc;
19 import java.util.regex.PatternSyntaxException JavaDoc;
20
21
22 /**
23  * The <code>String</code> class represents character strings. All
24  * string literals in Java programs, such as <code>"abc"</code>, are
25  * implemented as instances of this class.
26  * <p>
27  * Strings are constant; their values cannot be changed after they
28  * are created. String buffers support mutable strings.
29  * Because String objects are immutable they can be shared. For example:
30  * <p><blockquote><pre>
31  * String str = "abc";
32  * </pre></blockquote><p>
33  * is equivalent to:
34  * <p><blockquote><pre>
35  * char data[] = {'a', 'b', 'c'};
36  * String str = new String(data);
37  * </pre></blockquote><p>
38  * Here are some more examples of how strings can be used:
39  * <p><blockquote><pre>
40  * System.out.println("abc");
41  * String cde = "cde";
42  * System.out.println("abc" + cde);
43  * String c = "abc".substring(2,3);
44  * String d = cde.substring(1, 2);
45  * </pre></blockquote>
46  * <p>
47  * The class <code>String</code> includes methods for examining
48  * individual characters of the sequence, for comparing strings, for
49  * searching strings, for extracting substrings, and for creating a
50  * copy of a string with all characters translated to uppercase or to
51  * lowercase. Case mapping is based on the Unicode Standard version
52  * specified by the {@link java.lang.Character Character} class.
53  * <p>
54  * The Java language provides special support for the string
55  * concatenation operator (&nbsp;+&nbsp;), and for conversion of
56  * other objects to strings. String concatenation is implemented
57  * through the <code>StringBuilder</code>(or <code>StringBuffer</code>)
58  * class and its <code>append</code> method.
59  * String conversions are implemented through the method
60  * <code>toString</code>, defined by <code>Object</code> and
61  * inherited by all classes in Java. For additional information on
62  * string concatenation and conversion, see Gosling, Joy, and Steele,
63  * <i>The Java Language Specification</i>.
64  *
65  * <p> Unless otherwise noted, passing a <tt>null</tt> argument to a constructor
66  * or method in this class will cause a {@link NullPointerException} to be
67  * thrown.
68  *
69  * <p>A <code>String</code> represents a string in the UTF-16 format
70  * in which <em>supplementary characters</em> are represented by <em>surrogate
71  * pairs</em> (see the section <a HREF="Character.html#unicode">Unicode
72  * Character Representations</a> in the <code>Character</code> class for
73  * more information).
74  * Index values refer to <code>char</code> code units, so a supplementary
75  * character uses two positions in a <code>String</code>.
76  * <p>The <code>String</code> class provides methods for dealing with
77  * Unicode code points (i.e., characters), in addition to those for
78  * dealing with Unicode code units (i.e., <code>char</code> values).
79  *
80  * @author Lee Boynton
81  * @author Arthur van Hoff
82  * @version 1.189, 10/21/05
83  * @see java.lang.Object#toString()
84  * @see java.lang.StringBuffer
85  * @see java.lang.StringBuilder
86  * @see java.nio.charset.Charset
87  * @since JDK1.0
88  */

89
90 public final class String
91     implements java.io.Serializable JavaDoc, Comparable JavaDoc<String JavaDoc>, CharSequence JavaDoc
92 {
93     /** The value is used for character storage. */
94     private final char value[];
95
96     /** The offset is the first index of the storage that is used. */
97     private final int offset;
98
99     /** The count is the number of characters in the String. */
100     private final int count;
101
102     /** Cache the hash code for the string */
103     private int hash; // Default to 0
104

105     /** use serialVersionUID from JDK 1.0.2 for interoperability */
106     private static final long serialVersionUID = -6849794470754667710L;
107
108     /**
109      * Class String is special cased within the Serialization Stream Protocol.
110      *
111      * A String instance is written initially into an ObjectOutputStream in the
112      * following format:
113      * <pre>
114      * <code>TC_STRING</code> (utf String)
115      * </pre>
116      * The String is written by method <code>DataOutput.writeUTF</code>.
117      * A new handle is generated to refer to all future references to the
118      * string instance within the stream.
119      */

120     private static final ObjectStreamField JavaDoc[] serialPersistentFields =
121         new ObjectStreamField JavaDoc[0];
122
123     /**
124      * Initializes a newly created <code>String</code> object so that it
125      * represents an empty character sequence. Note that use of this
126      * constructor is unnecessary since Strings are immutable.
127      */

128     public String() {
129     this.offset = 0;
130     this.count = 0;
131     this.value = new char[0];
132     }
133
134     /**
135      * Initializes a newly created <code>String</code> object so that it
136      * represents the same sequence of characters as the argument; in other
137      * words, the newly created string is a copy of the argument string. Unless
138      * an explicit copy of <code>original</code> is needed, use of this
139      * constructor is unnecessary since Strings are immutable.
140      *
141      * @param original a <code>String</code>.
142      */

143     public String(String JavaDoc original) {
144     int size = original.count;
145     char[] originalValue = original.value;
146     char[] v;
147     if (originalValue.length > size) {
148         // The array representing the String is bigger than the new
149
// String itself. Perhaps this constructor is being called
150
// in order to trim the baggage, so make a copy of the array.
151
v = new char[size];
152         System.arraycopy(originalValue, original.offset, v, 0, size);
153     } else {
154         // The array representing the String is the same
155
// size as the String, so no point in making a copy.
156
v = originalValue;
157     }
158     this.offset = 0;
159     this.count = size;
160     this.value = v;
161     }
162
163     /**
164      * Allocates a new <code>String</code> so that it represents the
165      * sequence of characters currently contained in the character array
166      * argument. The contents of the character array are copied; subsequent
167      * modification of the character array does not affect the newly created
168      * string.
169      *
170      * @param value the initial value of the string.
171      */

172     public String(char value[]) {
173     int size = value.length;
174     char[] v = new char[size];
175     System.arraycopy(value, 0, v, 0, size);
176     this.offset = 0;
177     this.count = size;
178     this.value = v;
179     }
180
181     /**
182      * Allocates a new <code>String</code> that contains characters from
183      * a subarray of the character array argument. The <code>offset</code>
184      * argument is the index of the first character of the subarray and
185      * the <code>count</code> argument specifies the length of the
186      * subarray. The contents of the subarray are copied; subsequent
187      * modification of the character array does not affect the newly
188      * created string.
189      *
190      * @param value array that is the source of characters.
191      * @param offset the initial offset.
192      * @param count the length.
193      * @exception IndexOutOfBoundsException if the <code>offset</code>
194      * and <code>count</code> arguments index characters outside
195      * the bounds of the <code>value</code> array.
196      */

197     public String(char value[], int offset, int count) {
198         if (offset < 0) {
199             throw new StringIndexOutOfBoundsException JavaDoc(offset);
200         }
201         if (count < 0) {
202             throw new StringIndexOutOfBoundsException JavaDoc(count);
203         }
204         // Note: offset or count might be near -1>>>1.
205
if (offset > value.length - count) {
206             throw new StringIndexOutOfBoundsException JavaDoc(offset + count);
207         }
208         char[] v = new char[count];
209         System.arraycopy(value, offset, v, 0, count);
210         this.offset = 0;
211         this.count = count;
212         this.value = v;
213     }
214
215     /**
216      * Allocates a new <code>String</code> that contains characters
217      * from a subarray of the Unicode code point array argument. The
218      * <code>offset</code> argument is the index of the first code
219      * point of the subarray and the <code>count</code> argument
220      * specifies the length of the subarray. The contents of the
221      * subarray are converted to <code>char</code>s; subsequent
222      * modification of the <code>int</code> array does not affect the
223      * newly created string.
224      *
225      * @param codePoints array that is the source of Unicode code points.
226      * @param offset the initial offset.
227      * @param count the length.
228      * @exception IllegalArgumentException if any invalid Unicode code point
229      * is found in <code>codePoints</code>
230      * @exception IndexOutOfBoundsException if the <code>offset</code>
231      * and <code>count</code> arguments index characters outside
232      * the bounds of the <code>codePoints</code> array.
233      * @since 1.5
234      */

235     public String(int[] codePoints, int offset, int count) {
236         if (offset < 0) {
237             throw new StringIndexOutOfBoundsException JavaDoc(offset);
238         }
239         if (count < 0) {
240             throw new StringIndexOutOfBoundsException JavaDoc(count);
241         }
242         // Note: offset or count might be near -1>>>1.
243
if (offset > codePoints.length - count) {
244             throw new StringIndexOutOfBoundsException JavaDoc(offset + count);
245         }
246
247     int expansion = 0;
248     int margin = 1;
249     char[] v = new char[count + margin];
250     int x = offset;
251     int j = 0;
252     for (int i = 0; i < count; i++) {
253         int c = codePoints[x++];
254         if (c < 0) {
255         throw new IllegalArgumentException JavaDoc();
256         }
257         if (margin <= 0 && (j+1) >= v.length) {
258         if (expansion == 0) {
259             expansion = (((-margin + 1) * count) << 10) / i;
260             expansion >>= 10;
261             if (expansion <= 0) {
262             expansion = 1;
263             }
264         } else {
265             expansion *= 2;
266         }
267         char[] tmp = new char[Math.min(v.length+expansion, count*2)];
268         margin = (tmp.length - v.length) - (count - i);
269         System.arraycopy(v, 0, tmp, 0, j);
270         v = tmp;
271         }
272         if (c < Character.MIN_SUPPLEMENTARY_CODE_POINT) {
273         v[j++] = (char) c;
274         } else if (c <= Character.MAX_CODE_POINT) {
275         Character.toSurrogates(c, v, j);
276         j += 2;
277         margin--;
278         } else {
279         throw new IllegalArgumentException JavaDoc();
280         }
281     }
282     this.offset = 0;
283     this.value = v;
284     this.count = j;
285     }
286
287     /**
288      * Allocates a new <code>String</code> constructed from a subarray
289      * of an array of 8-bit integer values.
290      * <p>
291      * The <code>offset</code> argument is the index of the first byte
292      * of the subarray, and the <code>count</code> argument specifies the
293      * length of the subarray.
294      * <p>
295      * Each <code>byte</code> in the subarray is converted to a
296      * <code>char</code> as specified in the method above.
297      *
298      * @deprecated This method does not properly convert bytes into characters.
299      * As of JDK&nbsp;1.1, the preferred way to do this is via the
300      * <code>String</code> constructors that take a charset name or that use
301      * the platform's default charset.
302      *
303      * @param ascii the bytes to be converted to characters.
304      * @param hibyte the top 8 bits of each 16-bit Unicode character.
305      * @param offset the initial offset.
306      * @param count the length.
307      * @exception IndexOutOfBoundsException if the <code>offset</code>
308      * or <code>count</code> argument is invalid.
309      * @see java.lang.String#String(byte[], int)
310      * @see java.lang.String#String(byte[], int, int, java.lang.String)
311      * @see java.lang.String#String(byte[], int, int)
312      * @see java.lang.String#String(byte[], java.lang.String)
313      * @see java.lang.String#String(byte[])
314      */

315     @Deprecated JavaDoc
316     public String(byte ascii[], int hibyte, int offset, int count) {
317     checkBounds(ascii, offset, count);
318         char value[] = new char[count];
319
320         if (hibyte == 0) {
321             for (int i = count ; i-- > 0 ;) {
322                 value[i] = (char) (ascii[i + offset] & 0xff);
323             }
324         } else {
325             hibyte <<= 8;
326             for (int i = count ; i-- > 0 ;) {
327                 value[i] = (char) (hibyte | (ascii[i + offset] & 0xff));
328             }
329         }
330     this.offset = 0;
331     this.count = count;
332     this.value = value;
333     }
334
335     /**
336      * Allocates a new <code>String</code> containing characters
337      * constructed from an array of 8-bit integer values. Each character
338      * <i>c</i>in the resulting string is constructed from the
339      * corresponding component <i>b</i> in the byte array such that:
340      * <p><blockquote><pre>
341      * <b><i>c</i></b> == (char)(((hibyte &amp; 0xff) &lt;&lt; 8)
342      * | (<b><i>b</i></b> &amp; 0xff))
343      * </pre></blockquote>
344      *
345      * @deprecated This method does not properly convert bytes into characters.
346      * As of JDK&nbsp;1.1, the preferred way to do this is via the
347      * <code>String</code> constructors that take a charset name or
348      * that use the platform's default charset.
349      *
350      * @param ascii the bytes to be converted to characters.
351      * @param hibyte the top 8 bits of each 16-bit Unicode character.
352      * @see java.lang.String#String(byte[], int, int, java.lang.String)
353      * @see java.lang.String#String(byte[], int, int)
354      * @see java.lang.String#String(byte[], java.lang.String)
355      * @see java.lang.String#String(byte[])
356      */

357     @Deprecated JavaDoc
358     public String(byte ascii[], int hibyte) {
359         this(ascii, hibyte, 0, ascii.length);
360     }
361
362     /* Common private utility method used to bounds check the byte array
363      * and requested offset & length values used by the String(byte[],..)
364      * constructors.
365      */

366     private static void checkBounds(byte[] bytes, int offset, int length) {
367     if (length < 0)
368         throw new StringIndexOutOfBoundsException JavaDoc(length);
369     if (offset < 0)
370         throw new StringIndexOutOfBoundsException JavaDoc(offset);
371     if (offset > bytes.length - length)
372         throw new StringIndexOutOfBoundsException JavaDoc(offset + length);
373     }
374
375     /**
376      * Constructs a new <tt>String</tt> by decoding the specified subarray of
377      * bytes using the specified charset. The length of the new
378      * <tt>String</tt> is a function of the charset, and hence may not be equal
379      * to the length of the subarray.
380      *
381      * <p> The behavior of this constructor when the given bytes are not valid
382      * in the given charset is unspecified. The {@link
383      * java.nio.charset.CharsetDecoder} class should be used when more control
384      * over the decoding process is required.
385      *
386      * @param bytes the bytes to be decoded into characters
387      * @param offset the index of the first byte to decode
388      * @param length the number of bytes to decode
389      * @param charsetName the name of a supported
390      * {@link java.nio.charset.Charset </code>charset<code>}
391      * @throws UnsupportedEncodingException
392      * if the named charset is not supported
393      * @throws IndexOutOfBoundsException
394      * if the <tt>offset</tt> and <tt>length</tt> arguments
395      * index characters outside the bounds of the <tt>bytes</tt>
396      * array
397      * @since JDK1.1
398      */

399     public String(byte bytes[], int offset, int length, String JavaDoc charsetName)
400     throws UnsupportedEncodingException JavaDoc
401     {
402     if (charsetName == null)
403         throw new NullPointerException JavaDoc("charsetName");
404     checkBounds(bytes, offset, length);
405     char[] v = StringCoding.decode(charsetName, bytes, offset, length);
406     this.offset = 0;
407     this.count = v.length;
408     this.value = v;
409     }
410
411     /**
412      * Constructs a new <tt>String</tt> by decoding the specified array of
413      * bytes using the specified charset. The length of the new
414      * <tt>String</tt> is a function of the charset, and hence may not be equal
415      * to the length of the byte array.
416      *
417      * <p> The behavior of this constructor when the given bytes are not valid
418      * in the given charset is unspecified. The {@link
419      * java.nio.charset.CharsetDecoder} class should be used when more control
420      * over the decoding process is required.
421      *
422      * @param bytes the bytes to be decoded into characters
423      * @param charsetName the name of a supported
424      * {@link java.nio.charset.Charset </code>charset<code>}
425      *
426      * @exception UnsupportedEncodingException
427      * If the named charset is not supported
428      * @since JDK1.1
429      */

430     public String(byte bytes[], String JavaDoc charsetName)
431     throws UnsupportedEncodingException JavaDoc
432     {
433     this(bytes, 0, bytes.length, charsetName);
434     }
435
436     /**
437      * Constructs a new <tt>String</tt> by decoding the specified subarray of
438      * bytes using the platform's default charset. The length of the new
439      * <tt>String</tt> is a function of the charset, and hence may not be equal
440      * to the length of the subarray.
441      *
442      * <p> The behavior of this constructor when the given bytes are not valid
443      * in the default charset is unspecified. The {@link
444      * java.nio.charset.CharsetDecoder} class should be used when more control
445      * over the decoding process is required.
446      *
447      * @param bytes the bytes to be decoded into characters
448      * @param offset the index of the first byte to decode
449      * @param length the number of bytes to decode
450      * @throws IndexOutOfBoundsException
451      * if the <code>offset</code> and the <code>length</code>
452      * arguments index characters outside the bounds of the
453      * <code>bytes</code> array
454      * @since JDK1.1
455      */

456     public String(byte bytes[], int offset, int length) {
457     checkBounds(bytes, offset, length);
458     char[] v = StringCoding.decode(bytes, offset, length);
459     this.offset = 0;
460     this.count = v.length;
461     this.value = v;
462     }
463
464     /**
465      * Constructs a new <tt>String</tt> by decoding the specified array of
466      * bytes using the platform's default charset. The length of the new
467      * <tt>String</tt> is a function of the charset, and hence may not be equal
468      * to the length of the byte array.
469      *
470      * <p> The behavior of this constructor when the given bytes are not valid
471      * in the default charset is unspecified. The {@link
472      * java.nio.charset.CharsetDecoder} class should be used when more control
473      * over the decoding process is required.
474      *
475      * @param bytes the bytes to be decoded into characters
476      * @since JDK1.1
477      */

478     public String(byte bytes[]) {
479     this(bytes, 0, bytes.length);
480     }
481
482     /**
483      * Allocates a new string that contains the sequence of characters
484      * currently contained in the string buffer argument. The contents of
485      * the string buffer are copied; subsequent modification of the string
486      * buffer does not affect the newly created string.
487      *
488      * @param buffer a <code>StringBuffer</code>.
489      */

490     public String(StringBuffer JavaDoc buffer) {
491         String JavaDoc result = buffer.toString();
492         this.value = result.value;
493         this.count = result.count;
494         this.offset = result.offset;
495     }
496
497     /**
498      * Allocates a new string that contains the sequence of characters
499      * currently contained in the string builder argument. The contents of
500      * the string builder are copied; subsequent modification of the string
501      * builder does not affect the newly created string.
502      *
503      * <p>This constructor is provided to ease migration to
504      * <code>StringBuilder</code>. Obtaining a string from a string builder
505      * via the <code>toString</code> method is likely to run faster and is
506      * generally preferred.
507      *
508      * @param builder a <code>StringBuilder</code>
509      * @since 1.5
510      */

511     public String(StringBuilder JavaDoc builder) {
512         String JavaDoc result = builder.toString();
513         this.value = result.value;
514         this.count = result.count;
515         this.offset = result.offset;
516     }
517
518
519     // Package private constructor which shares value array for speed.
520
String(int offset, int count, char value[]) {
521     this.value = value;
522     this.offset = offset;
523     this.count = count;
524     }
525
526     /**
527      * Returns the length of this string.
528      * The length is equal to the number of 16-bit
529      * Unicode characters in the string.
530      *
531      * @return the length of the sequence of characters represented by this
532      * object.
533      */

534     public int length() {
535         return count;
536     }
537
538     /**
539      * Returns the <code>char</code> value at the
540      * specified index. An index ranges from <code>0</code> to
541      * <code>length() - 1</code>. The first <code>char</code> value of the sequence
542      * is at index <code>0</code>, the next at index <code>1</code>,
543      * and so on, as for array indexing.
544      *
545      * <p>If the <code>char</code> value specified by the index is a
546      * <a HREF="Character.html#unicode">surrogate</a>, the surrogate
547      * value is returned.
548      *
549      * @param index the index of the <code>char</code> value.
550      * @return the <code>char</code> value at the specified index of this string.
551      * The first <code>char</code> value is at index <code>0</code>.
552      * @exception IndexOutOfBoundsException if the <code>index</code>
553      * argument is negative or not less than the length of this
554      * string.
555      */

556     public char charAt(int index) {
557         if ((index < 0) || (index >= count)) {
558             throw new StringIndexOutOfBoundsException JavaDoc(index);
559         }
560         return value[index + offset];
561     }
562
563     /**
564      * Returns the character (Unicode code point) at the specified
565      * index. The index refers to <code>char</code> values
566      * (Unicode code units) and ranges from <code>0</code> to
567      * {@link #length()}<code> - 1</code>.
568      *
569      * <p> If the <code>char</code> value specified at the given index
570      * is in the high-surrogate range, the following index is less
571      * than the length of this <code>String</code>, and the
572      * <code>char</code> value at the following index is in the
573      * low-surrogate range, then the supplementary code point
574      * corresponding to this surrogate pair is returned. Otherwise,
575      * the <code>char</code> value at the given index is returned.
576      *
577      * @param index the index to the <code>char</code> values
578      * @return the code point value of the character at the
579      * <code>index</code>
580      * @exception IndexOutOfBoundsException if the <code>index</code>
581      * argument is negative or not less than the length of this
582      * string.
583      * @since 1.5
584      */

585     public int codePointAt(int index) {
586         if ((index < 0) || (index >= count)) {
587             throw new StringIndexOutOfBoundsException JavaDoc(index);
588         }
589         return Character.codePointAtImpl(value, offset + index, offset + count);
590     }
591
592     /**
593      * Returns the character (Unicode code point) before the specified
594      * index. The index refers to <code>char</code> values
595      * (Unicode code units) and ranges from <code>1</code> to {@link
596      * CharSequence#length() length}.
597      *
598      * <p> If the <code>char</code> value at <code>(index - 1)</code>
599      * is in the low-surrogate range, <code>(index - 2)</code> is not
600      * negative, and the <code>char</code> value at <code>(index -
601      * 2)</code> is in the high-surrogate range, then the
602      * supplementary code point value of the surrogate pair is
603      * returned. If the <code>char</code> value at <code>index -
604      * 1</code> is an unpaired low-surrogate or a high-surrogate, the
605      * surrogate value is returned.
606      *
607      * @param index the index following the code point that should be returned
608      * @return the Unicode code point value before the given index.
609      * @exception IndexOutOfBoundsException if the <code>index</code>
610      * argument is less than 1 or greater than the length
611      * of this string.
612      * @since 1.5
613      */

614     public int codePointBefore(int index) {
615     int i = index - 1;
616         if ((i < 0) || (i >= count)) {
617             throw new StringIndexOutOfBoundsException JavaDoc(index);
618         }
619         return Character.codePointBeforeImpl(value, offset + index, offset);
620     }
621
622     /**
623      * Returns the number of Unicode code points in the specified text
624      * range of this <code>String</code>. The text range begins at the
625      * specified <code>beginIndex</code> and extends to the
626      * <code>char</code> at index <code>endIndex - 1</code>. Thus the
627      * length (in <code>char</code>s) of the text range is
628      * <code>endIndex-beginIndex</code>. Unpaired surrogates within
629      * the text range count as one code point each.
630      *
631      * @param beginIndex the index to the first <code>char</code> of
632      * the text range.
633      * @param endIndex the index after the last <code>char</code> of
634      * the text range.
635      * @return the number of Unicode code points in the specified text
636      * range
637      * @exception IndexOutOfBoundsException if the
638      * <code>beginIndex</code> is negative, or <code>endIndex</code>
639      * is larger than the length of this <code>String</code>, or
640      * <code>beginIndex</code> is larger than <code>endIndex</code>.
641      * @since 1.5
642      */

643     public int codePointCount(int beginIndex, int endIndex) {
644     if (beginIndex < 0 || endIndex > count || beginIndex > endIndex) {
645         throw new IndexOutOfBoundsException JavaDoc();
646     }
647     return Character.codePointCountImpl(value, offset+beginIndex, endIndex-beginIndex);
648     }
649
650     /**
651      * Returns the index within this <code>String</code> that is
652      * offset from the given <code>index</code> by
653      * <code>codePointOffset</code> code points. Unpaired surrogates
654      * within the text range given by <code>index</code> and
655      * <code>codePointOffset</code> count as one code point each.
656      *
657      * @param index the index to be offset
658      * @param codePointOffset the offset in code points
659      * @return the index within this <code>String</code>
660      * @exception IndexOutOfBoundsException if <code>index</code>
661      * is negative or larger then the length of this
662      * <code>String</code>, or if <code>codePointOffset</code> is positive
663      * and the substring starting with <code>index</code> has fewer
664      * than <code>codePointOffset</code> code points,
665      * or if <code>codePointOffset</code> is negative and the substring
666      * before <code>index</code> has fewer than the absolute value
667      * of <code>codePointOffset</code> code points.
668      * @since 1.5
669      */

670     public int offsetByCodePoints(int index, int codePointOffset) {
671     if (index < 0 || index > count) {
672         throw new IndexOutOfBoundsException JavaDoc();
673     }
674     return Character.offsetByCodePointsImpl(value, offset, count,
675                         offset+index, codePointOffset);
676     }
677
678     /**
679      * Copy characters from this string into dst starting at dstBegin.
680      * This method doesn't perform any range checking.
681      */

682     void getChars(char dst[], int dstBegin) {
683         System.arraycopy(value, offset, dst, dstBegin, count);
684     }
685
686     /**
687      * Copies characters from this string into the destination character
688      * array.
689      * <p>
690      * The first character to be copied is at index <code>srcBegin</code>;
691      * the last character to be copied is at index <code>srcEnd-1</code>
692      * (thus the total number of characters to be copied is
693      * <code>srcEnd-srcBegin</code>). The characters are copied into the
694      * subarray of <code>dst</code> starting at index <code>dstBegin</code>
695      * and ending at index:
696      * <p><blockquote><pre>
697      * dstbegin + (srcEnd-srcBegin) - 1
698      * </pre></blockquote>
699      *
700      * @param srcBegin index of the first character in the string
701      * to copy.
702      * @param srcEnd index after the last character in the string
703      * to copy.
704      * @param dst the destination array.
705      * @param dstBegin the start offset in the destination array.
706      * @exception IndexOutOfBoundsException If any of the following
707      * is true:
708      * <ul><li><code>srcBegin</code> is negative.
709      * <li><code>srcBegin</code> is greater than <code>srcEnd</code>
710      * <li><code>srcEnd</code> is greater than the length of this
711      * string
712      * <li><code>dstBegin</code> is negative
713      * <li><code>dstBegin+(srcEnd-srcBegin)</code> is larger than
714      * <code>dst.length</code></ul>
715      */

716     public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {
717         if (srcBegin < 0) {
718             throw new StringIndexOutOfBoundsException JavaDoc(srcBegin);
719         }
720         if (srcEnd > count) {
721             throw new StringIndexOutOfBoundsException JavaDoc(srcEnd);
722         }
723         if (srcBegin > srcEnd) {
724             throw new StringIndexOutOfBoundsException JavaDoc(srcEnd - srcBegin);
725         }
726         System.arraycopy(value, offset + srcBegin, dst, dstBegin,
727              srcEnd - srcBegin);
728     }
729
730     /**
731      * Copies characters from this string into the destination byte
732      * array. Each byte receives the 8 low-order bits of the
733      * corresponding character. The eight high-order bits of each character
734      * are not copied and do not participate in the transfer in any way.
735      * <p>
736      * The first character to be copied is at index <code>srcBegin</code>;
737      * the last character to be copied is at index <code>srcEnd-1</code>.
738      * The total number of characters to be copied is
739      * <code>srcEnd-srcBegin</code>. The characters, converted to bytes,
740      * are copied into the subarray of <code>dst</code> starting at index
741      * <code>dstBegin</code> and ending at index:
742      * <p><blockquote><pre>
743      * dstbegin + (srcEnd-srcBegin) - 1
744      * </pre></blockquote>
745      *
746      * @deprecated This method does not properly convert characters into bytes.
747      * As of JDK&nbsp;1.1, the preferred way to do this is via the
748      * <code>getBytes()</code> method, which uses the platform's default
749      * charset.
750      *
751      * @param srcBegin index of the first character in the string
752      * to copy.
753      * @param srcEnd index after the last character in the string
754      * to copy.
755      * @param dst the destination array.
756      * @param dstBegin the start offset in the destination array.
757      * @exception IndexOutOfBoundsException if any of the following
758      * is true:
759      * <ul><li><code>srcBegin</code> is negative
760      * <li><code>srcBegin</code> is greater than <code>srcEnd</code>
761      * <li><code>srcEnd</code> is greater than the length of this
762      * String
763      * <li><code>dstBegin</code> is negative
764      * <li><code>dstBegin+(srcEnd-srcBegin)</code> is larger than
765      * <code>dst.length</code></ul>
766      */

767     @Deprecated JavaDoc
768     public void getBytes(int srcBegin, int srcEnd, byte dst[], int dstBegin) {
769         if (srcBegin < 0) {
770             throw new StringIndexOutOfBoundsException JavaDoc(srcBegin);
771         }
772         if (srcEnd > count) {
773             throw new StringIndexOutOfBoundsException JavaDoc(srcEnd);
774         }
775         if (srcBegin > srcEnd) {
776             throw new StringIndexOutOfBoundsException JavaDoc(srcEnd - srcBegin);
777         }
778         int j = dstBegin;
779         int n = offset + srcEnd;
780         int i = offset + srcBegin;
781         char[] val = value; /* avoid getfield opcode */
782
783         while (i < n) {
784             dst[j++] = (byte)val[i++];
785         }
786     }
787
788     /**
789      * Encodes this <tt>String</tt> into a sequence of bytes using the
790      * named charset, storing the result into a new byte array.
791      *
792      * <p> The behavior of this method when this string cannot be encoded in
793      * the given charset is unspecified. The {@link
794      * java.nio.charset.CharsetEncoder} class should be used when more control
795      * over the encoding process is required.
796      *
797      * @param charsetName
798      * the name of a supported
799      * {@link java.nio.charset.Charset </code>charset<code>}
800      *
801      * @return The resultant byte array
802      *
803      * @exception UnsupportedEncodingException
804      * If the named charset is not supported
805      *
806      * @since JDK1.1
807      */

808     public byte[] getBytes(String JavaDoc charsetName)
809     throws UnsupportedEncodingException JavaDoc
810     {
811     if (charsetName == null) throw new NullPointerException JavaDoc();
812     return StringCoding.encode(charsetName, value, offset, count);
813     }
814
815     /**
816      * Encodes this <tt>String</tt> into a sequence of bytes using the
817      * platform's default charset, storing the result into a new byte array.
818      *
819      * <p> The behavior of this method when this string cannot be encoded in
820      * the default charset is unspecified. The {@link
821      * java.nio.charset.CharsetEncoder} class should be used when more control
822      * over the encoding process is required.
823      *
824      * @return The resultant byte array
825      *
826      * @since JDK1.1
827      */

828     public byte[] getBytes() {
829     return StringCoding.encode(value, offset, count);
830     }
831
832     /**
833      * Compares this string to the specified object.
834      * The result is <code>true</code> if and only if the argument is not
835      * <code>null</code> and is a <code>String</code> object that represents
836      * the same sequence of characters as this object.
837      *
838      * @param anObject the object to compare this <code>String</code>
839      * against.
840      * @return <code>true</code> if the <code>String </code>are equal;
841      * <code>false</code> otherwise.
842      * @see java.lang.String#compareTo(java.lang.String)
843      * @see java.lang.String#equalsIgnoreCase(java.lang.String)
844      */

845     public boolean equals(Object JavaDoc anObject) {
846     if (this == anObject) {
847         return true;
848     }
849     if (anObject instanceof String JavaDoc) {
850         String JavaDoc anotherString = (String JavaDoc)anObject;
851         int n = count;
852         if (n == anotherString.count) {
853         char v1[] = value;
854         char v2[] = anotherString.value;
855         int i = offset;
856         int j = anotherString.offset;
857         while (n-- != 0) {
858             if (v1[i++] != v2[j++])
859             return false;
860         }
861         return true;
862         }
863     }
864     return false;
865     }
866
867     /**
868      * Returns <tt>true</tt> if and only if this <tt>String</tt> represents
869      * the same sequence of characters as the specified <tt>StringBuffer</tt>.
870      *
871      * @param sb the <tt>StringBuffer</tt> to compare to.
872      * @return <tt>true</tt> if and only if this <tt>String</tt> represents
873      * the same sequence of characters as the specified
874      * <tt>StringBuffer</tt>, otherwise <tt>false</tt>.
875      * @throws NullPointerException if <code>sb</code> is <code>null</code>
876      * @since 1.4
877      */

878     public boolean contentEquals(StringBuffer JavaDoc sb) {
879         synchronized(sb) {
880             return contentEquals((CharSequence JavaDoc)sb);
881         }
882     }
883
884     /**
885      * Returns <tt>true</tt> if and only if this <tt>String</tt> represents
886      * the same sequence of char values as the specified sequence.
887      *
888      * @param cs the sequence to compare to.
889      * @return <tt>true</tt> if and only if this <tt>String</tt> represents
890      * the same sequence of char values as the specified
891      * sequence, otherwise <tt>false</tt>.
892      * @throws NullPointerException if <code>cs</code> is <code>null</code>
893      * @since 1.5
894      */

895     public boolean contentEquals(CharSequence JavaDoc cs) {
896         if (count != cs.length())
897             return false;
898         // Argument is a StringBuffer, StringBuilder
899
if (cs instanceof AbstractStringBuilder JavaDoc) {
900             char v1[] = value;
901             char v2[] = ((AbstractStringBuilder JavaDoc)cs).getValue();
902             int i = offset;
903             int j = 0;
904             int n = count;
905             while (n-- != 0) {
906                 if (v1[i++] != v2[j++])
907                     return false;
908             }
909         }
910         // Argument is a String
911
if (cs.equals(this))
912             return true;
913         // Argument is a generic CharSequence
914
char v1[] = value;
915         int i = offset;
916         int j = 0;
917         int n = count;
918         while (n-- != 0) {
919             if (v1[i++] != cs.charAt(j++))
920                 return false;
921         }
922         return true;
923     }
924
925     /**
926      * Compares this <code>String</code> to another <code>String</code>,
927      * ignoring case considerations. Two strings are considered equal
928      * ignoring case if they are of the same length, and corresponding
929      * characters in the two strings are equal ignoring case.
930      * <p>
931      * Two characters <code>c1</code> and <code>c2</code> are considered
932      * the same, ignoring case if at least one of the following is true:
933      * <ul><li>The two characters are the same (as compared by the
934      * <code>==</code> operator).
935      * <li>Applying the method {@link java.lang.Character#toUpperCase(char)}
936      * to each character produces the same result.
937      * <li>Applying the method {@link java.lang.Character#toLowerCase(char)}
938      * to each character produces the same result.</ul>
939      *
940      * @param anotherString the <code>String</code> to compare this
941      * <code>String</code> against.
942      * @return <code>true</code> if the argument is not <code>null</code>
943      * and the <code>String</code>s are equal,
944      * ignoring case; <code>false</code> otherwise.
945      * @see #equals(Object)
946      * @see java.lang.Character#toLowerCase(char)
947      * @see java.lang.Character#toUpperCase(char)
948      */

949     public boolean equalsIgnoreCase(String JavaDoc anotherString) {
950         return (this == anotherString) ? true :
951                (anotherString != null) && (anotherString.count == count) &&
952            regionMatches(true, 0, anotherString, 0, count);
953     }
954
955     /**
956      * Compares two strings lexicographically.
957      * The comparison is based on the Unicode value of each character in
958      * the strings. The character sequence represented by this
959      * <code>String</code> object is compared lexicographically to the
960      * character sequence represented by the argument string. The result is
961      * a negative integer if this <code>String</code> object
962      * lexicographically precedes the argument string. The result is a
963      * positive integer if this <code>String</code> object lexicographically
964      * follows the argument string. The result is zero if the strings
965      * are equal; <code>compareTo</code> returns <code>0</code> exactly when
966      * the {@link #equals(Object)} method would return <code>true</code>.
967      * <p>
968      * This is the definition of lexicographic ordering. If two strings are
969      * different, then either they have different characters at some index
970      * that is a valid index for both strings, or their lengths are different,
971      * or both. If they have different characters at one or more index
972      * positions, let <i>k</i> be the smallest such index; then the string
973      * whose character at position <i>k</i> has the smaller value, as
974      * determined by using the &lt; operator, lexicographically precedes the
975      * other string. In this case, <code>compareTo</code> returns the
976      * difference of the two character values at position <code>k</code> in
977      * the two string -- that is, the value:
978      * <blockquote><pre>
979      * this.charAt(k)-anotherString.charAt(k)
980      * </pre></blockquote>
981      * If there is no index position at which they differ, then the shorter
982      * string lexicographically precedes the longer string. In this case,
983      * <code>compareTo</code> returns the difference of the lengths of the
984      * strings -- that is, the value:
985      * <blockquote><pre>
986      * this.length()-anotherString.length()
987      * </pre></blockquote>
988      *
989      * @param anotherString the <code>String</code> to be compared.
990      * @return the value <code>0</code> if the argument string is equal to
991      * this string; a value less than <code>0</code> if this string
992      * is lexicographically less than the string argument; and a
993      * value greater than <code>0</code> if this string is
994      * lexicographically greater than the string argument.
995      */

996     public int compareTo(String JavaDoc anotherString) {
997     int len1 = count;
998     int len2 = anotherString.count;
999     int n = Math.min(len1, len2);
1000    char v1[] = value;
1001    char v2[] = anotherString.value;
1002    int i = offset;
1003    int j = anotherString.offset;
1004
1005    if (i == j) {
1006        int k = i;
1007        int lim = n + i;
1008        while (k < lim) {
1009        char c1 = v1[k];
1010        char c2 = v2[k];
1011        if (c1 != c2) {
1012            return c1 - c2;
1013        }
1014        k++;
1015        }
1016    } else {
1017        while (n-- != 0) {
1018        char c1 = v1[i++];
1019        char c2 = v2[j++];
1020        if (c1 != c2) {
1021            return c1 - c2;
1022        }
1023        }
1024    }
1025    return len1 - len2;
1026    }
1027
1028    /**
1029     * A Comparator that orders <code>String</code> objects as by
1030     * <code>compareToIgnoreCase</code>. This comparator is serializable.
1031     * <p>
1032     * Note that this Comparator does <em>not</em> take locale into account,
1033     * and will result in an unsatisfactory ordering for certain locales.
1034     * The java.text package provides <em>Collators</em> to allow
1035     * locale-sensitive ordering.
1036     *
1037     * @see java.text.Collator#compare(String, String)
1038     * @since 1.2
1039     */

1040    public static final Comparator JavaDoc<String JavaDoc> CASE_INSENSITIVE_ORDER
1041                                         = new CaseInsensitiveComparator();
1042    private static class CaseInsensitiveComparator
1043                         implements Comparator JavaDoc<String JavaDoc>, java.io.Serializable JavaDoc {
1044    // use serialVersionUID from JDK 1.2.2 for interoperability
1045
private static final long serialVersionUID = 8575799808933029326L;
1046
1047        public int compare(String JavaDoc s1, String JavaDoc s2) {
1048            int n1=s1.length(), n2=s2.length();
1049            for (int i1=0, i2=0; i1<n1 && i2<n2; i1++, i2++) {
1050                char c1 = s1.charAt(i1);
1051                char c2 = s2.charAt(i2);
1052                if (c1 != c2) {
1053                    c1 = Character.toUpperCase(c1);
1054                    c2 = Character.toUpperCase(c2);
1055                    if (c1 != c2) {
1056                        c1 = Character.toLowerCase(c1);
1057                        c2 = Character.toLowerCase(c2);
1058                        if (c1 != c2) {
1059                            return c1 - c2;
1060                        }
1061                    }
1062                }
1063            }
1064            return n1 - n2;
1065        }
1066    }
1067
1068    /**
1069     * Compares two strings lexicographically, ignoring case
1070     * differences. This method returns an integer whose sign is that of
1071     * calling <code>compareTo</code> with normalized versions of the strings
1072     * where case differences have been eliminated by calling
1073     * <code>Character.toLowerCase(Character.toUpperCase(character))</code> on
1074     * each character.
1075     * <p>
1076     * Note that this method does <em>not</em> take locale into account,
1077     * and will result in an unsatisfactory ordering for certain locales.
1078     * The java.text package provides <em>collators</em> to allow
1079     * locale-sensitive ordering.
1080     *
1081     * @param str the <code>String</code> to be compared.
1082     * @return a negative integer, zero, or a positive integer as the
1083     * specified String is greater than, equal to, or less
1084     * than this String, ignoring case considerations.
1085     * @see java.text.Collator#compare(String, String)
1086     * @since 1.2
1087     */

1088    public int compareToIgnoreCase(String JavaDoc str) {
1089        return CASE_INSENSITIVE_ORDER.compare(this, str);
1090    }
1091
1092    /**
1093     * Tests if two string regions are equal.
1094     * <p>
1095     * A substring of this <tt>String</tt> object is compared to a substring
1096     * of the argument other. The result is true if these substrings
1097     * represent identical character sequences. The substring of this
1098     * <tt>String</tt> object to be compared begins at index <tt>toffset</tt>
1099     * and has length <tt>len</tt>. The substring of other to be compared
1100     * begins at index <tt>ooffset</tt> and has length <tt>len</tt>. The
1101     * result is <tt>false</tt> if and only if at least one of the following
1102     * is true:
1103     * <ul><li><tt>toffset</tt> is negative.
1104     * <li><tt>ooffset</tt> is negative.
1105     * <li><tt>toffset+len</tt> is greater than the length of this
1106     * <tt>String</tt> object.
1107     * <li><tt>ooffset+len</tt> is greater than the length of the other
1108     * argument.
1109     * <li>There is some nonnegative integer <i>k</i> less than <tt>len</tt>
1110     * such that:
1111     * <tt>this.charAt(toffset+<i>k</i>)&nbsp;!=&nbsp;other.charAt(ooffset+<i>k</i>)</tt>
1112     * </ul>
1113     *
1114     * @param toffset the starting offset of the subregion in this string.
1115     * @param other the string argument.
1116     * @param ooffset the starting offset of the subregion in the string
1117     * argument.
1118     * @param len the number of characters to compare.
1119     * @return <code>true</code> if the specified subregion of this string
1120     * exactly matches the specified subregion of the string argument;
1121     * <code>false</code> otherwise.
1122     */

1123    public boolean regionMatches(int toffset, String JavaDoc other, int ooffset,
1124                 int len) {
1125    char ta[] = value;
1126    int to = offset + toffset;
1127    char pa[] = other.value;
1128    int po = other.offset + ooffset;
1129    // Note: toffset, ooffset, or len might be near -1>>>1.
1130
if ((ooffset < 0) || (toffset < 0) || (toffset > (long)count - len)
1131        || (ooffset > (long)other.count - len)) {
1132        return false;
1133    }
1134    while (len-- > 0) {
1135        if (ta[to++] != pa[po++]) {
1136            return false;
1137        }
1138    }
1139    return true;
1140    }
1141
1142    /**
1143     * Tests if two string regions are equal.
1144     * <p>
1145     * A substring of this <tt>String</tt> object is compared to a substring
1146     * of the argument <tt>other</tt>. The result is <tt>true</tt> if these
1147     * substrings represent character sequences that are the same, ignoring
1148     * case if and only if <tt>ignoreCase</tt> is true. The substring of
1149     * this <tt>String</tt> object to be compared begins at index
1150     * <tt>toffset</tt> and has length <tt>len</tt>. The substring of
1151     * <tt>other</tt> to be compared begins at index <tt>ooffset</tt> and
1152     * has length <tt>len</tt>. The result is <tt>false</tt> if and only if
1153     * at least one of the following is true:
1154     * <ul><li><tt>toffset</tt> is negative.
1155     * <li><tt>ooffset</tt> is negative.
1156     * <li><tt>toffset+len</tt> is greater than the length of this
1157     * <tt>String</tt> object.
1158     * <li><tt>ooffset+len</tt> is greater than the length of the other
1159     * argument.
1160     * <li><tt>ignoreCase</tt> is <tt>false</tt> and there is some nonnegative
1161     * integer <i>k</i> less than <tt>len</tt> such that:
1162     * <blockquote><pre>
1163     * this.charAt(toffset+k) != other.charAt(ooffset+k)
1164     * </pre></blockquote>
1165     * <li><tt>ignoreCase</tt> is <tt>true</tt> and there is some nonnegative
1166     * integer <i>k</i> less than <tt>len</tt> such that:
1167     * <blockquote><pre>
1168     * Character.toLowerCase(this.charAt(toffset+k)) !=
1169               Character.toLowerCase(other.charAt(ooffset+k))
1170     * </pre></blockquote>
1171     * and:
1172     * <blockquote><pre>
1173     * Character.toUpperCase(this.charAt(toffset+k)) !=
1174     * Character.toUpperCase(other.charAt(ooffset+k))
1175     * </pre></blockquote>
1176     * </ul>
1177     *
1178     * @param ignoreCase if <code>true</code>, ignore case when comparing
1179     * characters.
1180     * @param toffset the starting offset of the subregion in this
1181     * string.
1182     * @param other the string argument.
1183     * @param ooffset the starting offset of the subregion in the string
1184     * argument.
1185     * @param len the number of characters to compare.
1186     * @return <code>true</code> if the specified subregion of this string
1187     * matches the specified subregion of the string argument;
1188     * <code>false</code> otherwise. Whether the matching is exact
1189     * or case insensitive depends on the <code>ignoreCase</code>
1190     * argument.
1191     */

1192    public boolean regionMatches(boolean ignoreCase, int toffset,
1193                           String JavaDoc other, int ooffset, int len) {
1194        char ta[] = value;
1195        int to = offset + toffset;
1196        char pa[] = other.value;
1197        int po = other.offset + ooffset;
1198        // Note: toffset, ooffset, or len might be near -1>>>1.
1199
if ((ooffset < 0) || (toffset < 0) || (toffset > (long)count - len) ||
1200                (ooffset > (long)other.count - len)) {
1201            return false;
1202        }
1203        while (len-- > 0) {
1204            char c1 = ta[to++];
1205            char c2 = pa[po++];
1206            if (c1 == c2) {
1207                continue;
1208            }
1209            if (ignoreCase) {
1210                // If characters don't match but case may be ignored,
1211
// try converting both characters to uppercase.
1212
// If the results match, then the comparison scan should
1213
// continue.
1214
char u1 = Character.toUpperCase(c1);
1215                char u2 = Character.toUpperCase(c2);
1216                if (u1 == u2) {
1217                    continue;
1218                }
1219                // Unfortunately, conversion to uppercase does not work properly
1220
// for the Georgian alphabet, which has strange rules about case
1221
// conversion. So we need to make one last check before
1222
// exiting.
1223
if (Character.toLowerCase(u1) == Character.toLowerCase(u2)) {
1224                    continue;
1225                }
1226            }
1227            return false;
1228        }
1229        return true;
1230    }
1231
1232    /**
1233     * Tests if this string starts with the specified prefix beginning
1234     * a specified index.
1235     *
1236     * @param prefix the prefix.
1237     * @param toffset where to begin looking in the string.
1238     * @return <code>true</code> if the character sequence represented by the
1239     * argument is a prefix of the substring of this object starting
1240     * at index <code>toffset</code>; <code>false</code> otherwise.
1241     * The result is <code>false</code> if <code>toffset</code> is
1242     * negative or greater than the length of this
1243     * <code>String</code> object; otherwise the result is the same
1244     * as the result of the expression
1245     * <pre>
1246     * this.substring(toffset).startsWith(prefix)
1247     * </pre>
1248     */

1249    public boolean startsWith(String JavaDoc prefix, int toffset) {
1250    char ta[] = value;
1251    int to = offset + toffset;
1252    char pa[] = prefix.value;
1253    int po = prefix.offset;
1254    int pc = prefix.count;
1255    // Note: toffset might be near -1>>>1.
1256
if ((toffset < 0) || (toffset > count - pc)) {
1257        return false;
1258    }
1259    while (--pc >= 0) {
1260        if (ta[to++] != pa[po++]) {
1261            return false;
1262        }
1263    }
1264    return true;
1265    }
1266
1267    /**
1268     * Tests if this string starts with the specified prefix.
1269     *
1270     * @param prefix the prefix.
1271     * @return <code>true</code> if the character sequence represented by the
1272     * argument is a prefix of the character sequence represented by
1273     * this string; <code>false</code> otherwise.
1274     * Note also that <code>true</code> will be returned if the
1275     * argument is an empty string or is equal to this
1276     * <code>String</code> object as determined by the
1277     * {@link #equals(Object)} method.
1278     * @since 1. 0
1279     */

1280    public boolean startsWith(String JavaDoc prefix) {
1281    return startsWith(prefix, 0);
1282    }
1283
1284    /**
1285     * Tests if this string ends with the specified suffix.
1286     *
1287     * @param suffix the suffix.
1288     * @return <code>true</code> if the character sequence represented by the
1289     * argument is a suffix of the character sequence represented by
1290     * this object; <code>false</code> otherwise. Note that the
1291     * result will be <code>true</code> if the argument is the
1292     * empty string or is equal to this <code>String</code> object
1293     * as determined by the {@link #equals(Object)} method.
1294     */

1295    public boolean endsWith(String JavaDoc suffix) {
1296    return startsWith(suffix, count - suffix.count);
1297    }
1298
1299    /**
1300     * Returns a hash code for this string. The hash code for a
1301     * <code>String</code> object is computed as
1302     * <blockquote><pre>
1303     * s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
1304     * </pre></blockquote>
1305     * using <code>int</code> arithmetic, where <code>s[i]</code> is the
1306     * <i>i</i>th character of the string, <code>n</code> is the length of
1307     * the string, and <code>^</code> indicates exponentiation.
1308     * (The hash value of the empty string is zero.)
1309     *
1310     * @return a hash code value for this object.
1311     */

1312    public int hashCode() {
1313    int h = hash;
1314    if (h == 0) {
1315        int off = offset;
1316        char val[] = value;
1317        int len = count;
1318
1319            for (int i = 0; i < len; i++) {
1320                h = 31*h + val[off++];
1321            }
1322            hash = h;
1323        }
1324        return h;
1325    }
1326
1327    /**
1328     * Returns the index within this string of the first occurrence of
1329     * the specified character. If a character with value
1330     * <code>ch</code> occurs in the character sequence represented by
1331     * this <code>String</code> object, then the index (in Unicode
1332     * code units) of the first such occurrence is returned. For
1333     * values of <code>ch</code> in the range from 0 to 0xFFFF
1334     * (inclusive), this is the smallest value <i>k</i> such that:
1335     * <blockquote><pre>
1336     * this.charAt(<i>k</i>) == ch
1337     * </pre></blockquote>
1338     * is true. For other values of <code>ch</code>, it is the
1339     * smallest value <i>k</i> such that:
1340     * <blockquote><pre>
1341     * this.codePointAt(<i>k</i>) == ch
1342     * </pre></blockquote>
1343     * is true. In either case, if no such character occurs in this
1344     * string, then <code>-1</code> is returned.
1345     *
1346     * @param ch a character (Unicode code point).
1347     * @return the index of the first occurrence of the character in the
1348     * character sequence represented by this object, or
1349     * <code>-1</code> if the character does not occur.
1350     */

1351    public int indexOf(int ch) {
1352    return indexOf(ch, 0);
1353    }
1354
1355    /**
1356     * Returns the index within this string of the first occurrence of the
1357     * specified character, starting the search at the specified index.
1358     * <p>
1359     * If a character with value <code>ch</code> occurs in the
1360     * character sequence represented by this <code>String</code>
1361     * object at an index no smaller than <code>fromIndex</code>, then
1362     * the index of the first such occurrence is returned. For values
1363     * of <code>ch</code> in the range from 0 to 0xFFFF (inclusive),
1364     * this is the smallest value <i>k</i> such that:
1365     * <blockquote><pre>
1366     * (this.charAt(<i>k</i>) == ch) && (<i>k</i> &gt;= fromIndex)
1367     * </pre></blockquote>
1368     * is true. For other values of <code>ch</code>, it is the
1369     * smallest value <i>k</i> such that:
1370     * <blockquote><pre>
1371     * (this.codePointAt(<i>k</i>) == ch) && (<i>k</i> &gt;= fromIndex)
1372     * </pre></blockquote>
1373     * is true. In either case, if no such character occurs in this
1374     * string at or after position <code>fromIndex</code>, then
1375     * <code>-1</code> is returned.
1376     *
1377     * <p>
1378     * There is no restriction on the value of <code>fromIndex</code>. If it
1379     * is negative, it has the same effect as if it were zero: this entire
1380     * string may be searched. If it is greater than the length of this
1381     * string, it has the same effect as if it were equal to the length of
1382     * this string: <code>-1</code> is returned.
1383     *
1384     * <p>All indices are specified in <code>char</code> values
1385     * (Unicode code units).
1386     *
1387     * @param ch a character (Unicode code point).
1388     * @param fromIndex the index to start the search from.
1389     * @return the index of the first occurrence of the character in the
1390     * character sequence represented by this object that is greater
1391     * than or equal to <code>fromIndex</code>, or <code>-1</code>
1392     * if the character does not occur.
1393     */

1394    public int indexOf(int ch, int fromIndex) {
1395    int max = offset + count;
1396    char v[] = value;
1397
1398    if (fromIndex < 0) {
1399        fromIndex = 0;
1400    } else if (fromIndex >= count) {
1401        // Note: fromIndex might be near -1>>>1.
1402
return -1;
1403    }
1404
1405    int i = offset + fromIndex;
1406    if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) {
1407        // handle most cases here (ch is a BMP code point or a
1408
// negative value (invalid code point))
1409
for (; i < max ; i++) {
1410        if (v[i] == ch) {
1411            return i - offset;
1412        }
1413        }
1414        return -1;
1415    }
1416
1417    if (ch <= Character.MAX_CODE_POINT) {
1418        // handle supplementary characters here
1419
char[] surrogates = Character.toChars(ch);
1420        for (; i < max; i++) {
1421        if (v[i] == surrogates[0]) {
1422            if (i + 1 == max) {
1423            break;
1424            }
1425            if (v[i+1] == surrogates[1]) {
1426            return i - offset;
1427            }
1428        }
1429        }
1430    }
1431    return -1;
1432    }
1433
1434    /**
1435     * Returns the index within this string of the last occurrence of
1436     * the specified character. For values of <code>ch</code> in the
1437     * range from 0 to 0xFFFF (inclusive), the index (in Unicode code
1438     * units) returned is the largest value <i>k</i> such that:
1439     * <blockquote><pre>
1440     * this.charAt(<i>k</i>) == ch
1441     * </pre></blockquote>
1442     * is true. For other values of <code>ch</code>, it is the
1443     * largest value <i>k</i> such that:
1444     * <blockquote><pre>
1445     * this.codePointAt(<i>k</i>) == ch
1446     * </pre></blockquote>
1447     * is true. In either case, if no such character occurs in this
1448     * string, then <code>-1</code> is returned. The
1449     * <code>String</code> is searched backwards starting at the last
1450     * character.
1451     *
1452     * @param ch a character (Unicode code point).
1453     * @return the index of the last occurrence of the character in the
1454     * character sequence represented by this object, or
1455     * <code>-1</code> if the character does not occur.
1456     */

1457    public int lastIndexOf(int ch) {
1458    return lastIndexOf(ch, count - 1);
1459    }
1460
1461    /**
1462     * Returns the index within this string of the last occurrence of
1463     * the specified character, searching backward starting at the
1464     * specified index. For values of <code>ch</code> in the range
1465     * from 0 to 0xFFFF (inclusive), the index returned is the largest
1466     * value <i>k</i> such that:
1467     * <blockquote><pre>
1468     * (this.charAt(<i>k</i>) == ch) && (<i>k</i> &lt;= fromIndex)
1469     * </pre></blockquote>
1470     * is true. For other values of <code>ch</code>, it is the
1471     * largest value <i>k</i> such that:
1472     * <blockquote><pre>
1473     * (this.codePointAt(<i>k</i>) == ch) && (<i>k</i> &lt;= fromIndex)
1474     * </pre></blockquote>
1475     * is true. In either case, if no such character occurs in this
1476     * string at or before position <code>fromIndex</code>, then
1477     * <code>-1</code> is returned.
1478     *
1479     * <p>All indices are specified in <code>char</code> values
1480     * (Unicode code units).
1481     *
1482     * @param ch a character (Unicode code point).
1483     * @param fromIndex the index to start the search from. There is no
1484     * restriction on the value of <code>fromIndex</code>. If it is
1485     * greater than or equal to the length of this string, it has
1486     * the same effect as if it were equal to one less than the
1487     * length of this string: this entire string may be searched.
1488     * If it is negative, it has the same effect as if it were -1:
1489     * -1 is returned.
1490     * @return the index of the last occurrence of the character in the
1491     * character sequence represented by this object that is less
1492     * than or equal to <code>fromIndex</code>, or <code>-1</code>
1493     * if the character does not occur before that point.
1494     */

1495    public int lastIndexOf(int ch, int fromIndex) {
1496    int min = offset;
1497    char v[] = value;
1498
1499    int i = offset + ((fromIndex >= count) ? count - 1 : fromIndex);
1500
1501    if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) {
1502        // handle most cases here (ch is a BMP code point or a
1503
// negative value (invalid code point))
1504
for (; i >= min ; i--) {
1505        if (v[i] == ch) {
1506            return i - offset;
1507        }
1508        }
1509        return -1;
1510    }
1511
1512    int max = offset + count;
1513    if (ch <= Character.MAX_CODE_POINT) {
1514        // handle supplementary characters here
1515
char[] surrogates = Character.toChars(ch);
1516        for (; i >= min; i--) {
1517        if (v[i] == surrogates[0]) {
1518            if (i + 1 == max) {
1519            break;
1520            }
1521            if (v[i+1] == surrogates[1]) {
1522            return i - offset;
1523            }
1524        }
1525        }
1526    }
1527    return -1;
1528    }
1529
1530    /**
1531     * Returns the index within this string of the first occurrence of the
1532     * specified substring. The integer returned is the smallest value
1533     * <i>k</i> such that:
1534     * <blockquote><pre>
1535     * this.startsWith(str, <i>k</i>)
1536     * </pre></blockquote>
1537     * is <code>true</code>.
1538     *
1539     * @param str any string.
1540     * @return if the string argument occurs as a substring within this
1541     * object, then the index of the first character of the first
1542     * such substring is returned; if it does not occur as a
1543     * substring, <code>-1</code> is returned.
1544     */

1545    public int indexOf(String JavaDoc str) {
1546    return indexOf(str, 0);
1547    }
1548
1549    /**
1550     * Returns the index within this string of the first occurrence of the
1551     * specified substring, starting at the specified index. The integer
1552     * returned is the smallest value <tt>k</tt> for which:
1553     * <blockquote><pre>
1554     * k &gt;= Math.min(fromIndex, str.length()) && this.startsWith(str, k)
1555     * </pre></blockquote>
1556     * If no such value of <i>k</i> exists, then -1 is returned.
1557     *
1558     * @param str the substring for which to search.
1559     * @param fromIndex the index from which to start the search.
1560     * @return the index within this string of the first occurrence of the
1561     * specified substring, starting at the specified index.
1562     */

1563    public int indexOf(String JavaDoc str, int fromIndex) {
1564        return indexOf(value, offset, count,
1565                       str.value, str.offset, str.count, fromIndex);
1566    }
1567
1568    /**
1569     * Code shared by String and StringBuffer to do searches. The
1570     * source is the character array being searched, and the target
1571     * is the string being searched for.
1572     *
1573     * @param source the characters being searched.
1574     * @param sourceOffset offset of the source string.
1575     * @param sourceCount count of the source string.
1576     * @param target the characters being searched for.
1577     * @param targetOffset offset of the target string.
1578     * @param targetCount count of the target string.
1579     * @param fromIndex the index to begin searching from.
1580     */

1581    static int indexOf(char[] source, int sourceOffset, int sourceCount,
1582                       char[] target, int targetOffset, int targetCount,
1583                       int fromIndex) {
1584    if (fromIndex >= sourceCount) {
1585            return (targetCount == 0 ? sourceCount : -1);
1586    }
1587        if (fromIndex < 0) {
1588            fromIndex = 0;
1589        }
1590    if (targetCount == 0) {
1591        return fromIndex;
1592    }
1593
1594        char first = target[targetOffset];
1595        int max = sourceOffset + (sourceCount - targetCount);
1596
1597        for (int i = sourceOffset + fromIndex; i <= max; i++) {
1598            /* Look for first character. */
1599            if (source[i] != first) {
1600                while (++i <= max && source[i] != first);
1601            }
1602
1603            /* Found first character, now look at the rest of v2 */
1604            if (i <= max) {
1605                int j = i + 1;
1606                int end = j + targetCount - 1;
1607                for (int k = targetOffset + 1; j < end && source[j] ==
1608                         target[k]; j++, k++);
1609
1610                if (j == end) {
1611                    /* Found whole string. */
1612                    return i - sourceOffset;
1613                }
1614            }
1615        }
1616        return -1;
1617    }
1618
1619    /**
1620     * Returns the index within this string of the rightmost occurrence
1621     * of the specified substring. The rightmost empty string "" is
1622     * considered to occur at the index value <code>this.length()</code>.
1623     * The returned index is the largest value <i>k</i> such that
1624     * <blockquote><pre>
1625     * this.startsWith(str, k)
1626     * </pre></blockquote>
1627     * is true.
1628     *
1629     * @param str the substring to search for.
1630     * @return if the string argument occurs one or more times as a substring
1631     * within this object, then the index of the first character of
1632     * the last such substring is returned. If it does not occur as
1633     * a substring, <code>-1</code> is returned.
1634     */

1635    public int lastIndexOf(String JavaDoc str) {
1636    return lastIndexOf(str, count);
1637    }
1638
1639    /**
1640     * Returns the index within this string of the last occurrence of the
1641     * specified substring, searching backward starting at the specified index.
1642     * The integer returned is the largest value <i>k</i> such that:
1643     * <blockquote><pre>
1644     * k &lt;= Math.min(fromIndex, str.length()) && this.startsWith(str, k)
1645     * </pre></blockquote>
1646     * If no such value of <i>k</i> exists, then -1 is returned.
1647     *
1648     * @param str the substring to search for.
1649     * @param fromIndex the index to start the search from.
1650     * @return the index within this string of the last occurrence of the
1651     * specified substring.
1652     */

1653    public int lastIndexOf(String JavaDoc str, int fromIndex) {
1654        return lastIndexOf(value, offset, count,
1655                           str.value, str.offset, str.count, fromIndex);
1656    }
1657
1658    /**
1659     * Code shared by String and StringBuffer to do searches. The
1660     * source is the character array being searched, and the target
1661     * is the string being searched for.
1662     *
1663     * @param source the characters being searched.
1664     * @param sourceOffset offset of the source string.
1665     * @param sourceCount count of the source string.
1666     * @param target the characters being searched for.
1667     * @param targetOffset offset of the target string.
1668     * @param targetCount count of the target string.
1669     * @param fromIndex the index to begin searching from.
1670     */

1671    static int lastIndexOf(char[] source, int sourceOffset, int sourceCount,
1672                           char[] target, int targetOffset, int targetCount,
1673                           int fromIndex) {
1674        /*
1675     * Check arguments; return immediately where possible. For
1676     * consistency, don't check for null str.
1677     */

1678        int rightIndex = sourceCount - targetCount;
1679    if (fromIndex < 0) {
1680        return -1;
1681    }
1682    if (fromIndex > rightIndex) {
1683        fromIndex = rightIndex;
1684    }
1685    /* Empty string always matches. */
1686    if (targetCount == 0) {
1687        return fromIndex;
1688    }
1689
1690        int strLastIndex = targetOffset + targetCount - 1;
1691    char strLastChar = target[strLastIndex];
1692    int min = sourceOffset + targetCount - 1;
1693    int i = min + fromIndex;
1694
1695    startSearchForLastChar:
1696    while (true) {
1697        while (i >= min && source[i] != strLastChar) {
1698        i--;
1699        }
1700        if (i < min) {
1701        return -1;
1702        }
1703        int j = i - 1;
1704        int start = j - (targetCount - 1);
1705        int k = strLastIndex - 1;
1706
1707        while (j > start) {
1708            if (source[j--] != target[k--]) {
1709            i--;
1710            continue startSearchForLastChar;
1711        }
1712        }
1713        return start - sourceOffset + 1;
1714    }
1715    }
1716
1717    /**
1718     * Returns a new string that is a substring of this string. The
1719     * substring begins with the character at the specified index and
1720     * extends to the end of this string. <p>
1721     * Examples:
1722     * <blockquote><pre>
1723     * "unhappy".substring(2) returns "happy"
1724     * "Harbison".substring(3) returns "bison"
1725     * "emptiness".substring(9) returns "" (an empty string)
1726     * </pre></blockquote>
1727     *
1728     * @param beginIndex the beginning index, inclusive.
1729     * @return the specified substring.
1730     * @exception IndexOutOfBoundsException if
1731     * <code>beginIndex</code> is negative or larger than the
1732     * length of this <code>String</code> object.
1733     */

1734    public String JavaDoc substring(int beginIndex) {
1735    return substring(beginIndex, count);
1736    }
1737
1738    /**
1739     * Returns a new string that is a substring of this string. The
1740     * substring begins at the specified <code>beginIndex</code> and
1741     * extends to the character at index <code>endIndex - 1</code>.
1742     * Thus the length of the substring is <code>endIndex-beginIndex</code>.
1743     * <p>
1744     * Examples:
1745     * <blockquote><pre>
1746     * "hamburger".substring(4, 8) returns "urge"
1747     * "smiles".substring(1, 5) returns "mile"
1748     * </pre></blockquote>
1749     *
1750     * @param beginIndex the beginning index, inclusive.
1751     * @param endIndex the ending index, exclusive.
1752     * @return the specified substring.
1753     * @exception IndexOutOfBoundsException if the
1754     * <code>beginIndex</code> is negative, or
1755     * <code>endIndex</code> is larger than the length of
1756     * this <code>String</code> object, or
1757     * <code>beginIndex</code> is larger than
1758     * <code>endIndex</code>.
1759     */

1760    public String JavaDoc substring(int beginIndex, int endIndex) {
1761    if (beginIndex < 0) {
1762        throw new StringIndexOutOfBoundsException JavaDoc(beginIndex);
1763    }
1764    if (endIndex > count) {
1765        throw new StringIndexOutOfBoundsException JavaDoc(endIndex);
1766    }
1767    if (beginIndex > endIndex) {
1768        throw new StringIndexOutOfBoundsException JavaDoc(endIndex - beginIndex);
1769    }
1770    return ((beginIndex == 0) && (endIndex == count)) ? this :
1771        new String JavaDoc(offset + beginIndex, endIndex - beginIndex, value);
1772    }
1773
1774    /**
1775     * Returns a new character sequence that is a subsequence of this sequence.
1776     *
1777     * <p> An invocation of this method of the form
1778     *
1779     * <blockquote><pre>
1780     * str.subSequence(begin,&nbsp;end)</pre></blockquote>
1781     *
1782     * behaves in exactly the same way as the invocation
1783     *
1784     * <blockquote><pre>
1785     * str.substring(begin,&nbsp;end)</pre></blockquote>
1786     *
1787     * This method is defined so that the <tt>String</tt> class can implement
1788     * the {@link CharSequence} interface. </p>
1789     *
1790     * @param beginIndex the begin index, inclusive.
1791     * @param endIndex the end index, exclusive.
1792     * @return the specified subsequence.
1793     *
1794     * @throws IndexOutOfBoundsException
1795     * if <tt>beginIndex</tt> or <tt>endIndex</tt> are negative,
1796     * if <tt>endIndex</tt> is greater than <tt>length()</tt>,
1797     * or if <tt>beginIndex</tt> is greater than <tt>startIndex</tt>
1798     *
1799     * @since 1.4
1800     * @spec JSR-51
1801     */

1802    public CharSequence JavaDoc subSequence(int beginIndex, int endIndex) {
1803        return this.substring(beginIndex, endIndex);
1804    }
1805
1806    /**
1807     * Concatenates the specified string to the end of this string.
1808     * <p>
1809     * If the length of the argument string is <code>0</code>, then this
1810     * <code>String</code> object is returned. Otherwise, a new
1811     * <code>String</code> object is created, representing a character
1812     * sequence that is the concatenation of the character sequence
1813     * represented by this <code>String</code> object and the character
1814     * sequence represented by the argument string.<p>
1815     * Examples:
1816     * <blockquote><pre>
1817     * "cares".concat("s") returns "caress"
1818     * "to".concat("get").concat("her") returns "together"
1819     * </pre></blockquote>
1820     *
1821     * @param str the <code>String</code> that is concatenated to the end
1822     * of this <code>String</code>.
1823     * @return a string that represents the concatenation of this object's
1824     * characters followed by the string argument's characters.
1825     */

1826    public String JavaDoc concat(String JavaDoc str) {
1827    int otherLen = str.length();
1828    if (otherLen == 0) {
1829        return this;
1830    }
1831    char buf[] = new char[count + otherLen];
1832    getChars(0, count, buf, 0);
1833    str.getChars(0, otherLen, buf, count);
1834    return new String JavaDoc(0, count + otherLen, buf);
1835    }
1836
1837    /**
1838     * Returns a new string resulting from replacing all occurrences of
1839     * <code>oldChar</code> in this string with <code>newChar</code>.
1840     * <p>
1841     * If the character <code>oldChar</code> does not occur in the
1842     * character sequence represented by this <code>String</code> object,
1843     * then a reference to this <code>String</code> object is returned.
1844     * Otherwise, a new <code>String</code> object is created that
1845     * represents a character sequence identical to the character sequence
1846     * represented by this <code>String</code> object, except that every
1847     * occurrence of <code>oldChar</code> is replaced by an occurrence
1848     * of <code>newChar</code>.
1849     * <p>
1850     * Examples:
1851     * <blockquote><pre>
1852     * "mesquite in your cellar".replace('e', 'o')
1853     * returns "mosquito in your collar"
1854     * "the war of baronets".replace('r', 'y')
1855     * returns "the way of bayonets"
1856     * "sparring with a purple porpoise".replace('p', 't')
1857     * returns "starring with a turtle tortoise"
1858     * "JonL".replace('q', 'x') returns "JonL" (no change)
1859     * </pre></blockquote>
1860     *
1861     * @param oldChar the old character.
1862     * @param newChar the new character.
1863     * @return a string derived from this string by replacing every
1864     * occurrence of <code>oldChar</code> with <code>newChar</code>.
1865     */

1866    public String JavaDoc replace(char oldChar, char newChar) {
1867    if (oldChar != newChar) {
1868        int len = count;
1869        int i = -1;
1870        char[] val = value; /* avoid getfield opcode */
1871        int off = offset; /* avoid getfield opcode */
1872
1873        while (++i < len) {
1874        if (val[off + i] == oldChar) {
1875            break;
1876        }
1877        }
1878        if (i < len) {
1879        char buf[] = new char[len];
1880        for (int j = 0 ; j < i ; j++) {
1881            buf[j] = val[off+j];
1882        }
1883        while (i < len) {
1884            char c = val[off + i];
1885            buf[i] = (c == oldChar) ? newChar : c;
1886            i++;
1887        }
1888        return new String JavaDoc(0, len, buf);
1889        }
1890    }
1891    return this;
1892    }
1893
1894    /**
1895     * Tells whether or not this string matches the given <a
1896     * HREF="../util/regex/Pattern.html#sum">regular expression</a>.
1897     *
1898     * <p> An invocation of this method of the form
1899     * <i>str</i><tt>.matches(</tt><i>regex</i><tt>)</tt> yields exactly the
1900     * same result as the expression
1901     *
1902     * <blockquote><tt> {@link java.util.regex.Pattern}.{@link
1903     * java.util.regex.Pattern#matches(String,CharSequence)
1904     * matches}(</tt><i>regex</i><tt>,</tt> <i>str</i><tt>)</tt></blockquote>
1905     *
1906     * @param regex
1907     * the regular expression to which this string is to be matched
1908     *
1909     * @return <tt>true</tt> if, and only if, this string matches the
1910     * given regular expression
1911     *
1912     * @throws PatternSyntaxException
1913     * if the regular expression's syntax is invalid
1914     *
1915     * @see java.util.regex.Pattern
1916     *
1917     * @since 1.4
1918     * @spec JSR-51
1919     */

1920    public boolean matches(String JavaDoc regex) {
1921        return Pattern.matches(regex, this);
1922    }
1923
1924    /**
1925     * Returns true if and only if this string contains the specified
1926     * sequence of char values.
1927     *
1928     * @param s the sequence to search for
1929     * @return true if this string contains <code>s</code>, false otherwise
1930     * @throws NullPointerException if <code>s</code> is <code>null</code>
1931     * @since 1.5
1932     */

1933    public boolean contains(CharSequence JavaDoc s) {
1934        return indexOf(s.toString()) > -1;
1935    }
1936
1937    /**
1938     * Replaces the first substring of this string that matches the given <a
1939     * HREF="../util/regex/Pattern.html#sum">regular expression</a> with the
1940     * given replacement.
1941     *
1942     * <p> An invocation of this method of the form
1943     * <i>str</i><tt>.replaceFirst(</tt><i>regex</i><tt>,</tt> <i>repl</i><tt>)</tt>
1944     * yields exactly the same result as the expression
1945     *
1946     * <blockquote><tt>
1947     * {@link java.util.regex.Pattern}.{@link java.util.regex.Pattern#compile
1948     * compile}(</tt><i>regex</i><tt>).{@link
1949     * java.util.regex.Pattern#matcher(java.lang.CharSequence)
1950     * matcher}(</tt><i>str</i><tt>).{@link java.util.regex.Matcher#replaceFirst
1951     * replaceFirst}(</tt><i>repl</i><tt>)</tt></blockquote>
1952     *
1953     * @param regex
1954     * the regular expression to which this string is to be matched
1955     *
1956     * @return The resulting <tt>String</tt>
1957     *
1958     * @throws PatternSyntaxException
1959     * if the regular expression's syntax is invalid
1960     *
1961     * @see java.util.regex.Pattern
1962     *
1963     * @since 1.4
1964     * @spec JSR-51
1965     */

1966    public String JavaDoc replaceFirst(String JavaDoc regex, String JavaDoc replacement) {
1967    return Pattern.compile(regex).matcher(this).replaceFirst(replacement);
1968    }
1969
1970    /**
1971     * Replaces each substring of this string that matches the given <a
1972     * HREF="../util/regex/Pattern.html#sum">regular expression</a> with the
1973     * given replacement.
1974     *
1975     * <p> An invocation of this method of the form
1976     * <i>str</i><tt>.replaceAll(</tt><i>regex</i><tt>,</tt> <i>repl</i><tt>)</tt>
1977     * yields exactly the same result as the expression
1978     *
1979     * <blockquote><tt>
1980     * {@link java.util.regex.Pattern}.{@link java.util.regex.Pattern#compile
1981     * compile}(</tt><i>regex</i><tt>).{@link
1982     * java.util.regex.Pattern#matcher(java.lang.CharSequence)
1983     * matcher}(</tt><i>str</i><tt>).{@link java.util.regex.Matcher#replaceAll
1984     * replaceAll}(</tt><i>repl</i><tt>)</tt></blockquote>
1985     *
1986     * @param regex
1987     * the regular expression to which this string is to be matched
1988     *
1989     * @return The resulting <tt>String</tt>
1990     *
1991     * @throws PatternSyntaxException
1992     * if the regular expression's syntax is invalid
1993     *
1994     * @see java.util.regex.Pattern
1995     *
1996     * @since 1.4
1997     * @spec JSR-51
1998     */

1999    public String JavaDoc replaceAll(String JavaDoc regex, String JavaDoc replacement) {
2000    return Pattern.compile(regex).matcher(this).replaceAll(replacement);
2001    }
2002
2003    /**
2004     * Replaces each substring of this string that matches the literal target
2005     * sequence with the specified literal replacement sequence. The
2006     * replacement proceeds from the beginning of the string to the end, for
2007     * example, replacing "aa" with "b" in the string "aaa" will result in
2008     * "ba" rather than "ab".
2009     *
2010     * @param target The sequence of char values to be replaced
2011     * @param replacement The replacement sequence of char values
2012     * @return The resulting string
2013     * @throws NullPointerException if <code>target</code> or
2014     * <code>replacement</code> is <code>null</code>.
2015     * @since 1.5
2016     */

2017    public String JavaDoc replace(CharSequence JavaDoc target, CharSequence JavaDoc replacement) {
2018        return Pattern.compile(target.toString(), Pattern.LITERAL).matcher(
2019            this).replaceAll(Matcher.quoteReplacement(replacement.toString()));
2020    }
2021
2022    /**
2023     * Splits this string around matches of the given
2024     * <a HREF="../util/regex/Pattern.html#sum">regular expression</a>.
2025     *
2026     * <p> The array returned by this method contains each substring of this
2027     * string that is terminated by another substring that matches the given
2028     * expression or is terminated by the end of the string. The substrings in
2029     * the array are in the order in which they occur in this string. If the
2030     * expression does not match any part of the input then the resulting array
2031     * has just one element, namely this string.
2032     *
2033     * <p> The <tt>limit</tt> parameter controls the number of times the
2034     * pattern is applied and therefore affects the length of the resulting
2035     * array. If the limit <i>n</i> is greater than zero then the pattern
2036     * will be applied at most <i>n</i>&nbsp;-&nbsp;1 times, the array's
2037     * length will be no greater than <i>n</i>, and the array's last entry
2038     * will contain all input beyond the last matched delimiter. If <i>n</i>
2039     * is non-positive then the pattern will be applied as many times as
2040     * possible and the array can have any length. If <i>n</i> is zero then
2041     * the pattern will be applied as many times as possible, the array can
2042     * have any length, and trailing empty strings will be discarded.
2043     *
2044     * <p> The string <tt>"boo:and:foo"</tt>, for example, yields the
2045     * following results with these parameters:
2046     *
2047     * <blockquote><table cellpadding=1 cellspacing=0 summary="Split example showing regex, limit, and result">
2048     * <tr>
2049     * <th>Regex</th>
2050     * <th>Limit</th>
2051     * <th>Result</th>
2052     * </tr>
2053     * <tr><td align=center>:</td>
2054     * <td align=center>2</td>
2055     * <td><tt>{ "boo", "and:foo" }</tt></td></tr>
2056     * <tr><td align=center>:</td>
2057     * <td align=center>5</td>
2058     * <td><tt>{ "boo", "and", "foo" }</tt></td></tr>
2059     * <tr><td align=center>:</td>
2060     * <td align=center>-2</td>
2061     * <td><tt>{ "boo", "and", "foo" }</tt></td></tr>
2062     * <tr><td align=center>o</td>
2063     * <td align=center>5</td>
2064     * <td><tt>{ "b", "", ":and:f", "", "" }</tt></td></tr>
2065     * <tr><td align=center>o</td>
2066     * <td align=center>-2</td>
2067     * <td><tt>{ "b", "", ":and:f", "", "" }</tt></td></tr>
2068     * <tr><td align=center>o</td>
2069     * <td align=center>0</td>
2070     * <td><tt>{ "b", "", ":and:f" }</tt></td></tr>
2071     * </table></blockquote>
2072     *
2073     * <p> An invocation of this method of the form
2074     * <i>str.</i><tt>split(</tt><i>regex</i><tt>,</tt>&nbsp;<i>n</i><tt>)</tt>
2075     * yields the same result as the expression
2076     *
2077     * <blockquote>
2078     * {@link java.util.regex.Pattern}.{@link java.util.regex.Pattern#compile
2079     * compile}<tt>(</tt><i>regex</i><tt>)</tt>.{@link
2080     * java.util.regex.Pattern#split(java.lang.CharSequence,int)
2081     * split}<tt>(</tt><i>str</i><tt>,</tt>&nbsp;<i>n</i><tt>)</tt>
2082     * </blockquote>
2083     *
2084     *
2085     * @param regex
2086     * the delimiting regular expression
2087     *
2088     * @param limit
2089     * the result threshold, as described above
2090     *
2091     * @return the array of strings computed by splitting this string
2092     * around matches of the given regular expression
2093     *
2094     * @throws PatternSyntaxException
2095     * if the regular expression's syntax is invalid
2096     *
2097     * @see java.util.regex.Pattern
2098     *
2099     * @since 1.4
2100     * @spec JSR-51
2101     */

2102    public String JavaDoc[] split(String JavaDoc regex, int limit) {
2103    return Pattern.compile(regex).split(this, limit);
2104    }
2105
2106    /**
2107     * Splits this string around matches of the given
2108     * {@linkplain java.util.regex.Pattern#sum regular expression}.
2109     *
2110     * <p> This method works as if by invoking the two-argument {@link
2111     * #split(String, int) split} method with the given expression and a limit
2112     * argument of zero. Trailing empty strings are therefore not included in
2113     * the resulting array.
2114     *
2115     * <p> The string <tt>"boo:and:foo"</tt>, for example, yields the following
2116     * results with these expressions:
2117     *
2118     * <blockquote><table cellpadding=1 cellspacing=0 summary="Split examples showing regex and result">
2119     * <tr>
2120     * <th>Regex</th>
2121     * <th>Result</th>
2122     * </tr>
2123     * <tr><td align=center>:</td>
2124     * <td><tt>{ "boo", "and", "foo" }</tt></td></tr>
2125     * <tr><td align=center>o</td>
2126     * <td><tt>{ "b", "", ":and:f" }</tt></td></tr>
2127     * </table></blockquote>
2128     *
2129     *
2130     * @param regex
2131     * the delimiting regular expression
2132     *
2133     * @return the array of strings computed by splitting this string
2134     * around matches of the given regular expression
2135     *
2136     * @throws PatternSyntaxException
2137     * if the regular expression's syntax is invalid
2138     *
2139     * @see java.util.regex.Pattern
2140     *
2141     * @since 1.4
2142     * @spec JSR-51
2143     */

2144    public String JavaDoc[] split(String JavaDoc regex) {
2145        return split(regex, 0);
2146    }
2147
2148    /**
2149     * Converts all of the characters in this <code>String</code> to lower
2150     * case using the rules of the given <code>Locale</code>. Case mapping is based
2151     * on the Unicode Standard version specified by the {@link java.lang.Character Character}
2152     * class. Since case mappings are not always 1:1 char mappings, the resulting
2153     * <code>String</code> may be a different length than the original <code>String</code>.
2154     * <p>
2155     * Examples of lowercase mappings are in the following table:
2156     * <table border="1" summary="Lowercase mapping examples showing language code of locale, upper case, lower case, and description">
2157     * <tr>
2158     * <th>Language Code of Locale</th>
2159     * <th>Upper Case</th>
2160     * <th>Lower Case</th>
2161     * <th>Description</th>
2162     * </tr>
2163     * <tr>
2164     * <td>tr (Turkish)</td>
2165     * <td>&#92;u0130</td>
2166     * <td>&#92;u0069</td>
2167     * <td>capital letter I with dot above -&gt; small letter i</td>
2168     * </tr>
2169     * <tr>
2170     * <td>tr (Turkish)</td>
2171     * <td>&#92;u0049</td>
2172     * <td>&#92;u0131</td>
2173     * <td>capital letter I -&gt; small letter dotless i </td>
2174     * </tr>
2175     * <tr>
2176     * <td>(all)</td>
2177     * <td>French Fries</td>
2178     * <td>french fries</td>
2179     * <td>lowercased all chars in String</td>
2180     * </tr>
2181     * <tr>
2182     * <td>(all)</td>
2183     * <td><img SRC="doc-files/capiota.gif" alt="capiota"><img SRC="doc-files/capchi.gif" alt="capchi">
2184     * <img SRC="doc-files/captheta.gif" alt="captheta"><img SRC="doc-files/capupsil.gif" alt="capupsil">
2185     * <img SRC="doc-files/capsigma.gif" alt="capsigma"></td>
2186     * <td><img SRC="doc-files/iota.gif" alt="iota"><img SRC="doc-files/chi.gif" alt="chi">
2187     * <img SRC="doc-files/theta.gif" alt="theta"><img SRC="doc-files/upsilon.gif" alt="upsilon">
2188     * <img SRC="doc-files/sigma1.gif" alt="sigma"></td>
2189     * <td>lowercased all chars in String</td>
2190     * </tr>
2191     * </table>
2192     *
2193     * @param locale use the case transformation rules for this locale
2194     * @return the <code>String</code>, converted to lowercase.
2195     * @see java.lang.String#toLowerCase()
2196     * @see java.lang.String#toUpperCase()
2197     * @see java.lang.String#toUpperCase(Locale)
2198     * @since 1.1
2199     */

2200    public String JavaDoc toLowerCase(Locale JavaDoc locale) {
2201    if (locale == null) {
2202        throw new NullPointerException JavaDoc();
2203        }
2204
2205        int firstUpper;
2206
2207    /* Now check if there are any characters that need to be changed. */
2208    scan: {
2209        for (firstUpper = 0 ; firstUpper < count; ) {
2210        char c = value[offset+firstUpper];
2211        if ((c >= Character.MIN_HIGH_SURROGATE) &&
2212            (c <= Character.MAX_HIGH_SURROGATE)) {
2213            int supplChar = codePointAt(firstUpper);
2214            if (supplChar != Character.toLowerCase(supplChar)) {
2215                break scan;
2216            }
2217            firstUpper += Character.charCount(supplChar);
2218        } else {
2219            if (c != Character.toLowerCase(c)) {
2220                break scan;
2221            }
2222            firstUpper++;
2223        }
2224        }
2225        return this;
2226    }
2227
2228        char[] result = new char[count];
2229    int resultOffset = 0; /* result may grow, so i+resultOffset
2230                    * is the write location in result */

2231
2232        /* Just copy the first few lowerCase characters. */
2233        System.arraycopy(value, offset, result, 0, firstUpper);
2234
2235    String JavaDoc lang = locale.getLanguage();
2236    boolean localeDependent =
2237            (lang == "tr" || lang == "az" || lang == "lt");
2238        char[] lowerCharArray;
2239        int lowerChar;
2240        int srcChar;
2241        int srcCount;
2242        for (int i = firstUpper; i < count; i += srcCount) {
2243        srcChar = (int)value[offset+i];
2244        if ((char)srcChar >= Character.MIN_HIGH_SURROGATE &&
2245            (char)srcChar <= Character.MAX_HIGH_SURROGATE) {
2246        srcChar = codePointAt(i);
2247        srcCount = Character.charCount(srcChar);
2248        } else {
2249            srcCount = 1;
2250        }
2251            if (localeDependent || srcChar == '\u03A3') { // GREEK CAPITAL LETTER SIGMA
2252
lowerChar = ConditionalSpecialCasing.toLowerCaseEx(this, i, locale);
2253            } else {
2254                lowerChar = Character.toLowerCase(srcChar);
2255            }
2256            if ((lowerChar == Character.ERROR) ||
2257                (lowerChar >= Character.MIN_SUPPLEMENTARY_CODE_POINT)) {
2258                if (lowerChar == Character.ERROR) {
2259                    lowerCharArray =
2260                        ConditionalSpecialCasing.toLowerCaseCharArray(this, i, locale);
2261                } else if (srcCount == 2) {
2262            resultOffset += Character.toChars(lowerChar, result, i + resultOffset) - srcCount;
2263            continue;
2264                } else {
2265            lowerCharArray = Character.toChars(lowerChar);
2266        }
2267
2268                /* Grow result if needed */
2269                int mapLen = lowerCharArray.length;
2270        if (mapLen > srcCount) {
2271                    char[] result2 = new char[result.length + mapLen - srcCount];
2272                    System.arraycopy(result, 0, result2, 0,
2273                        i + resultOffset);
2274                    result = result2;
2275        }
2276                for (int x=0; x<mapLen; ++x) {
2277                    result[i+resultOffset+x] = lowerCharArray[x];
2278                }
2279                resultOffset += (mapLen - srcCount);
2280            } else {
2281                result[i+resultOffset] = (char)lowerChar;
2282            }
2283        }
2284        return new String JavaDoc(0, count+resultOffset, result);
2285    }
2286
2287    /**
2288     * Converts all of the characters in this <code>String</code> to lower
2289     * case using the rules of the default locale. This is equivalent to calling
2290     * <code>toLowerCase(Locale.getDefault())</code>.
2291     * <p>
2292     * @return the <code>String</code>, converted to lowercase.
2293     * @see java.lang.String#toLowerCase(Locale)
2294     */

2295    public String JavaDoc toLowerCase() {
2296        return toLowerCase(Locale.getDefault());
2297    }
2298
2299    /**
2300     * Converts all of the characters in this <code>String</code> to upper
2301     * case using the rules of the given <code>Locale</code>. Case mapping is based
2302     * on the Unicode Standard version specified by the {@link java.lang.Character Character}
2303     * class. Since case mappings are not always 1:1 char mappings, the resulting
2304     * <code>String</code> may be a different length than the original <code>String</code>.
2305     * <p>
2306     * Examples of locale-sensitive and 1:M case mappings are in the following table.
2307     * <p>
2308     * <table border="1" summary="Examples of locale-sensitive and 1:M case mappings. Shows Language code of locale, lower case, upper case, and description.">
2309     * <tr>
2310     * <th>Language Code of Locale</th>
2311     * <th>Lower Case</th>
2312     * <th>Upper Case</th>
2313     * <th>Description</th>
2314     * </tr>
2315     * <tr>
2316     * <td>tr (Turkish)</td>
2317     * <td>&#92;u0069</td>
2318     * <td>&#92;u0130</td>
2319     * <td>small letter i -&gt; capital letter I with dot above</td>
2320     * </tr>
2321     * <tr>
2322     * <td>tr (Turkish)</td>
2323     * <td>&#92;u0131</td>
2324     * <td>&#92;u0049</td>
2325     * <td>small letter dotless i -&gt; capital letter I</td>
2326     * </tr>
2327     * <tr>
2328     * <td>(all)</td>
2329     * <td>&#92;u00df</td>
2330     * <td>&#92;u0053 &#92;u0053</td>
2331     * <td>small letter sharp s -&gt; two letters: SS</td>
2332     * </tr>
2333     * <tr>
2334     * <td>(all)</td>
2335     * <td>Fahrvergn&uuml;gen</td>
2336     * <td>FAHRVERGN&Uuml;GEN</td>
2337     * <td></td>
2338     * </tr>
2339     * </table>
2340     * @param locale use the case transformation rules for this locale
2341     * @return the <code>String</code>, converted to uppercase.
2342     * @see java.lang.String#toUpperCase()
2343     * @see java.lang.String#toLowerCase()
2344     * @see java.lang.String#toLowerCase(Locale)
2345     * @since 1.1
2346     */

2347    public String JavaDoc toUpperCase(Locale JavaDoc locale) {
2348    if (locale == null) {
2349        throw new NullPointerException JavaDoc();
2350        }
2351
2352        int firstLower;
2353
2354    /* Now check if there are any characters that need to be changed. */
2355    scan: {
2356        for (firstLower = 0 ; firstLower < count; ) {
2357        int c = (int)value[offset+firstLower];
2358        int srcCount;
2359        if ((c >= Character.MIN_HIGH_SURROGATE) &&
2360            (c <= Character.MAX_HIGH_SURROGATE)) {
2361            c = codePointAt(firstLower);
2362            srcCount = Character.charCount(c);
2363        } else {
2364            srcCount = 1;
2365        }
2366        int upperCaseChar = Character.toUpperCaseEx(c);
2367        if ((upperCaseChar == Character.ERROR) ||
2368            (c != upperCaseChar)) {
2369            break scan;
2370        }
2371        firstLower += srcCount;
2372        }
2373        return this;
2374    }
2375
2376        char[] result = new char[count]; /* may grow */
2377    int resultOffset = 0; /* result may grow, so i+resultOffset
2378                    * is the write location in result */

2379
2380    /* Just copy the first few upperCase characters. */
2381    System.arraycopy(value, offset, result, 0, firstLower);
2382
2383    String JavaDoc lang = locale.getLanguage();
2384    boolean localeDependent =
2385            (lang == "tr" || lang == "az" || lang == "lt");
2386        char[] upperCharArray;
2387        int upperChar;
2388        int srcChar;
2389        int srcCount;
2390        for (int i = firstLower; i < count; i += srcCount) {
2391        srcChar = (int)value[offset+i];
2392        if ((char)srcChar >= Character.MIN_HIGH_SURROGATE &&
2393            (char)srcChar <= Character.MAX_HIGH_SURROGATE) {
2394        srcChar = codePointAt(i);
2395        srcCount = Character.charCount(srcChar);
2396        } else {
2397            srcCount = 1;
2398        }
2399            if (localeDependent) {
2400                upperChar = ConditionalSpecialCasing.toUpperCaseEx(this, i, locale);
2401            } else {
2402                upperChar = Character.toUpperCaseEx(srcChar);
2403            }
2404            if ((upperChar == Character.ERROR) ||
2405                (upperChar >= Character.MIN_SUPPLEMENTARY_CODE_POINT)) {
2406                if (upperChar == Character.ERROR) {
2407                    if (localeDependent) {
2408                        upperCharArray =
2409                            ConditionalSpecialCasing.toUpperCaseCharArray(this, i, locale);
2410                    } else {
2411                        upperCharArray = Character.toUpperCaseCharArray(srcChar);
2412                    }
2413                } else if (srcCount == 2) {
2414            resultOffset += Character.toChars(upperChar, result, i + resultOffset) - srcCount;
2415            continue;
2416                } else {
2417                    upperCharArray = Character.toChars(upperChar);
2418        }
2419
2420                /* Grow result if needed */
2421                int mapLen = upperCharArray.length;
2422        if (mapLen > srcCount) {
2423                    char[] result2 = new char[result.length + mapLen - srcCount];
2424                    System.arraycopy(result, 0, result2, 0,
2425                        i + resultOffset);
2426                    result = result2;
2427        }
2428                for (int x=0; x<mapLen; ++x) {
2429                    result[i+resultOffset+x] = upperCharArray[x];
2430                }
2431                resultOffset += (mapLen - srcCount);
2432            } else {
2433                result[i+resultOffset] = (char)upperChar;
2434            }
2435        }
2436        return new String JavaDoc(0, count+resultOffset, result);
2437    }
2438
2439    /**
2440     * Converts all of the characters in this <code>String</code> to upper
2441     * case using the rules of the default locale. This method is equivalent to
2442     * <code>toUpperCase(Locale.getDefault())</code>.
2443     * <p>
2444     * @return the <code>String</code>, converted to uppercase.
2445     * @see java.lang.String#toUpperCase(Locale)
2446     */

2447    public String JavaDoc toUpperCase() {
2448        return toUpperCase(Locale.getDefault());
2449    }
2450
2451    /**
2452     * Returns a copy of the string, with leading and trailing whitespace
2453     * omitted.
2454     * <p>
2455     * If this <code>String</code> object represents an empty character
2456     * sequence, or the first and last characters of character sequence
2457     * represented by this <code>String</code> object both have codes
2458     * greater than <code>'&#92;u0020'</code> (the space character), then a
2459     * reference to this <code>String</code> object is returned.
2460     * <p>
2461     * Otherwise, if there is no character with a code greater than
2462     * <code>'&#92;u0020'</code> in the string, then a new
2463     * <code>String</code> object representing an empty string is created
2464     * and returned.
2465     * <p>
2466     * Otherwise, let <i>k</i> be the index of the first character in the
2467     * string whose code is greater than <code>'&#92;u0020'</code>, and let
2468     * <i>m</i> be the index of the last character in the string whose code
2469     * is greater than <code>'&#92;u0020'</code>. A new <code>String</code>
2470     * object is created, representing the substring of this string that
2471     * begins with the character at index <i>k</i> and ends with the
2472     * character at index <i>m</i>-that is, the result of
2473     * <code>this.substring(<i>k</i>,&nbsp;<i>m</i>+1)</code>.
2474     * <p>
2475     * This method may be used to trim whitespace (as defined above) from
2476     * the beginning and end of a string.
2477     *
2478     * @return A copy of this string with leading and trailing white
2479     * space removed, or this string if it has no leading or
2480     * trailing white space.
2481     */

2482    public String JavaDoc trim() {
2483    int len = count;
2484    int st = 0;
2485    int off = offset; /* avoid getfield opcode */
2486    char[] val = value; /* avoid getfield opcode */
2487
2488    while ((st < len) && (val[off + st] <= ' ')) {
2489        st++;
2490    }
2491    while ((st < len) && (val[off + len - 1] <= ' ')) {
2492        len--;
2493    }
2494    return ((st > 0) || (len < count)) ? substring(st, len) : this;
2495    }
2496
2497    /**
2498     * This object (which is already a string!) is itself returned.
2499     *
2500     * @return the string itself.
2501     */

2502    public String JavaDoc toString() {
2503    return this;
2504    }
2505
2506    /**
2507     * Converts this string to a new character array.
2508     *
2509     * @return a newly allocated character array whose length is the length
2510     * of this string and whose contents are initialized to contain
2511     * the character sequence represented by this string.
2512     */

2513    public char[] toCharArray() {
2514    char result[] = new char[count];
2515    getChars(0, count, result, 0);
2516    return result;
2517    }
2518
2519    /**
2520     * Returns a formatted string using the specified format string and
2521     * arguments.
2522     *
2523     * <p> The locale always used is the one returned by {@link
2524     * java.util.Locale#getDefault() Locale.getDefault()}.
2525     *
2526     * @param format
2527     * A <a HREF="../util/Formatter.html#syntax">format string</a>
2528     *
2529     * @param args
2530     * Arguments referenced by the format specifiers in the format
2531     * string. If there are more arguments than format specifiers, the
2532     * extra arguments are ignored. The number of arguments is
2533     * variable and may be zero. The maximum number of arguments is
2534     * limited by the maximum dimension of a Java array as defined by
2535     * the <a HREF="http://java.sun.com/docs/books/vmspec/">Java
2536     * Virtual Machine Specification</a>. The behaviour on a
2537     * <tt>null</tt> argument depends on the <a
2538     * HREF="../util/Formatter.html#syntax">conversion</a>.
2539     *
2540     * @throws IllegalFormatException
2541     * If a format string contains an illegal syntax, a format
2542     * specifier that is incompatible with the given arguments,
2543     * insufficient arguments given the format string, or other
2544     * illegal conditions. For specification of all possible
2545     * formatting errors, see the <a
2546     * HREF="../util/Formatter.html#detail">Details</a> section of the
2547     * formatter class specification.
2548     *
2549     * @throws NullPointerException
2550     * If the <tt>format</tt> is <tt>null</tt>
2551     *
2552     * @return A formatted string
2553     *
2554     * @see java.util.Formatter
2555     * @since 1.5
2556     */

2557    public static String JavaDoc format(String JavaDoc format, Object JavaDoc ... args) {
2558    return new Formatter JavaDoc().format(format, args).toString();
2559    }
2560
2561    /**
2562     * Returns a formatted string using the specified locale, format string,
2563     * and arguments.
2564     *
2565     * @param l
2566     * The {@linkplain java.util.Locale locale} to apply during
2567     * formatting. If <tt>l</tt> is <tt>null</tt> then no localization
2568     * is applied.
2569     *
2570     * @param format
2571     * A <a HREF="../util/Formatter.html#syntax">format string</a>
2572     *
2573     * @param args
2574     * Arguments referenced by the format specifiers in the format
2575     * string. If there are more arguments than format specifiers, the
2576     * extra arguments are ignored. The number of arguments is
2577     * variable and may be zero. The maximum number of arguments is
2578     * limited by the maximum dimension of a Java array as defined by
2579     * the <a HREF="http://java.sun.com/docs/books/vmspec/">Java
2580     * Virtual Machine Specification</a>. The behaviour on a
2581     * <tt>null</tt> argument depends on the <a
2582     * HREF="../util/Formatter.html#syntax">conversion</a>.
2583     *
2584     * @throws IllegalFormatException
2585     * If a format string contains an illegal syntax, a format
2586     * specifier that is incompatible with the given arguments,
2587     * insufficient arguments given the format string, or other
2588     * illegal conditions. For specification of all possible
2589     * formatting errors, see the <a
2590     * HREF="../util/Formatter.html#detail">Details</a> section of the
2591     * formatter class specification
2592     *
2593     * @throws NullPointerException
2594     * If the <tt>format</tt> is <tt>null</tt>
2595     *
2596     * @return A formatted string
2597     *
2598     * @see java.util.Formatter
2599     * @since 1.5
2600     */

2601    public static String JavaDoc format(Locale JavaDoc l, String JavaDoc format, Object JavaDoc ... args) {
2602    return new Formatter JavaDoc(l).format(format, args).toString();
2603    }
2604
2605    /**
2606     * Returns the string representation of the <code>Object</code> argument.
2607     *
2608     * @param obj an <code>Object</code>.
2609     * @return if the argument is <code>null</code>, then a string equal to
2610     * <code>"null"</code>; otherwise, the value of
2611     * <code>obj.toString()</code> is returned.
2612     * @see java.lang.Object#toString()
2613     */

2614    public static String JavaDoc valueOf(Object JavaDoc obj) {
2615    return (obj == null) ? "null" : obj.toString();
2616    }
2617
2618    /**
2619     * Returns the string representation of the <code>char</code> array
2620     * argument. The contents of the character array are copied; subsequent
2621     * modification of the character array does not affect the newly
2622     * created string.
2623     *
2624     * @param data a <code>char</code> array.
2625     * @return a newly allocated string representing the same sequence of
2626     * characters contained in the character array argument.
2627     */

2628    public static String JavaDoc valueOf(char data[]) {
2629    return new String JavaDoc(data);
2630    }
2631
2632    /**
2633     * Returns the string representation of a specific subarray of the
2634     * <code>char</code> array argument.
2635     * <p>
2636     * The <code>offset</code> argument is the index of the first
2637     * character of the subarray. The <code>count</code> argument
2638     * specifies the length of the subarray. The contents of the subarray
2639     * are copied; subsequent modification of the character array does not
2640     * affect the newly created string.
2641     *
2642     * @param data the character array.
2643     * @param offset the initial offset into the value of the
2644     * <code>String</code>.
2645     * @param count the length of the value of the <code>String</code>.
2646     * @return a string representing the sequence of characters contained
2647     * in the subarray of the character array argument.
2648     * @exception IndexOutOfBoundsException if <code>offset</code> is
2649     * negative, or <code>count</code> is negative, or
2650     * <code>offset+count</code> is larger than
2651     * <code>data.length</code>.
2652     */

2653    public static String JavaDoc valueOf(char data[], int offset, int count) {
2654    return new String JavaDoc(data, offset, count);
2655    }
2656
2657    /**
2658     * Returns a String that represents the character sequence in the
2659     * array specified.
2660     *
2661     * @param data the character array.
2662     * @param offset initial offset of the subarray.
2663     * @param count length of the subarray.
2664     * @return a <code>String</code> that contains the characters of the
2665     * specified subarray of the character array.
2666     */

2667    public static String JavaDoc copyValueOf(char data[], int offset, int count) {
2668    // All public String constructors now copy the data.
2669
return new String JavaDoc(data, offset, count);
2670    }
2671
2672    /**
2673     * Returns a String that represents the character sequence in the
2674     * array specified.
2675     *
2676     * @param data the character array.
2677     * @return a <code>String</code> that contains the characters of the
2678     * character array.
2679     */

2680    public static String JavaDoc copyValueOf(char data[]) {
2681    return copyValueOf(data, 0, data.length);
2682    }
2683
2684    /**
2685     * Returns the string representation of the <code>boolean</code> argument.
2686     *
2687     * @param b a <code>boolean</code>.
2688     * @return if the argument is <code>true</code>, a string equal to
2689     * <code>"true"</code> is returned; otherwise, a string equal to
2690     * <code>"false"</code> is returned.
2691     */

2692    public static String JavaDoc valueOf(boolean b) {
2693    return b ? "true" : "false";
2694    }
2695
2696    /**
2697     * Returns the string representation of the <code>char</code>
2698     * argument.
2699     *
2700     * @param c a <code>char</code>.
2701     * @return a string of length <code>1</code> containing
2702     * as its single character the argument <code>c</code>.
2703     */

2704    public static String JavaDoc valueOf(char c) {
2705    char data[] = {c};
2706    return new String JavaDoc(0, 1, data);
2707    }
2708
2709    /**
2710     * Returns the string representation of the <code>int</code> argument.
2711     * <p>
2712     * The representation is exactly the one returned by the
2713     * <code>Integer.toString</code> method of one argument.
2714     *
2715     * @param i an <code>int</code>.
2716     * @return a string representation of the <code>int</code> argument.
2717     * @see java.lang.Integer#toString(int, int)
2718     */

2719    public static String JavaDoc valueOf(int i) {
2720        return Integer.toString(i, 10);
2721    }
2722
2723    /**
2724     * Returns the string representation of the <code>long</code> argument.
2725     * <p>
2726     * The representation is exactly the one returned by the
2727     * <code>Long.toString</code> method of one argument.
2728     *
2729     * @param l a <code>long</code>.
2730     * @return a string representation of the <code>long</code> argument.
2731     * @see java.lang.Long#toString(long)
2732     */

2733    public static String JavaDoc valueOf(long l) {
2734        return Long.toString(l, 10);
2735    }
2736
2737    /**
2738     * Returns the string representation of the <code>float</code> argument.
2739     * <p>
2740     * The representation is exactly the one returned by the
2741     * <code>Float.toString</code> method of one argument.
2742     *
2743     * @param f a <code>float</code>.
2744     * @return a string representation of the <code>float</code> argument.
2745     * @see java.lang.Float#toString(float)
2746     */

2747    public static String JavaDoc valueOf(float f) {
2748    return Float.toString(f);
2749    }
2750
2751    /**
2752     * Returns the string representation of the <code>double</code> argument.
2753     * <p>
2754     * The representation is exactly the one returned by the
2755     * <code>Double.toString</code> method of one argument.
2756     *
2757     * @param d a <code>double</code>.
2758     * @return a string representation of the <code>double</code> argument.
2759     * @see java.lang.Double#toString(double)
2760     */

2761    public static String JavaDoc valueOf(double d) {
2762    return Double.toString(d);
2763    }
2764
2765    /**
2766     * Returns a canonical representation for the string object.
2767     * <p>
2768     * A pool of strings, initially empty, is maintained privately by the
2769     * class <code>String</code>.
2770     * <p>
2771     * When the intern method is invoked, if the pool already contains a
2772     * string equal to this <code>String</code> object as determined by
2773     * the {@link #equals(Object)} method, then the string from the pool is
2774     * returned. Otherwise, this <code>String</code> object is added to the
2775     * pool and a reference to this <code>String</code> object is returned.
2776     * <p>
2777     * It follows that for any two strings <code>s</code> and <code>t</code>,
2778     * <code>s.intern()&nbsp;==&nbsp;t.intern()</code> is <code>true</code>
2779     * if and only if <code>s.equals(t)</code> is <code>true</code>.
2780     * <p>
2781     * All literal strings and string-valued constant expressions are
2782     * interned. String literals are defined in &sect;3.10.5 of the
2783     * <a HREF="http://java.sun.com/docs/books/jls/html/">Java Language
2784     * Specification</a>
2785     *
2786     * @return a string that has the same contents as this string, but is
2787     * guaranteed to be from a pool of unique strings.
2788     */

2789    public native String JavaDoc intern();
2790
2791}
2792
Popular Tags