2 * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
28 import org.apidesign.bck2brwsr.core.ExtraJavaScript;
29 import java.util.Comparator;
32 * The <code>String</code> class represents character strings. All
33 * string literals in Java programs, such as <code>"abc"</code>, are
34 * implemented as instances of this class.
36 * Strings are constant; their values cannot be changed after they
37 * are created. String buffers support mutable strings.
38 * Because String objects are immutable they can be shared. For example:
39 * <p><blockquote><pre>
41 * </pre></blockquote><p>
43 * <p><blockquote><pre>
44 * char data[] = {'a', 'b', 'c'};
45 * String str = new String(data);
46 * </pre></blockquote><p>
47 * Here are some more examples of how strings can be used:
48 * <p><blockquote><pre>
49 * System.out.println("abc");
51 * System.out.println("abc" + cde);
52 * String c = "abc".substring(2,3);
53 * String d = cde.substring(1, 2);
56 * The class <code>String</code> includes methods for examining
57 * individual characters of the sequence, for comparing strings, for
58 * searching strings, for extracting substrings, and for creating a
59 * copy of a string with all characters translated to uppercase or to
60 * lowercase. Case mapping is based on the Unicode Standard version
61 * specified by the {@link java.lang.Character Character} class.
63 * The Java language provides special support for the string
64 * concatenation operator ( + ), and for conversion of
65 * other objects to strings. String concatenation is implemented
66 * through the <code>StringBuilder</code>(or <code>StringBuffer</code>)
67 * class and its <code>append</code> method.
68 * String conversions are implemented through the method
69 * <code>toString</code>, defined by <code>Object</code> and
70 * inherited by all classes in Java. For additional information on
71 * string concatenation and conversion, see Gosling, Joy, and Steele,
72 * <i>The Java Language Specification</i>.
74 * <p> Unless otherwise noted, passing a <tt>null</tt> argument to a constructor
75 * or method in this class will cause a {@link NullPointerException} to be
78 * <p>A <code>String</code> represents a string in the UTF-16 format
79 * in which <em>supplementary characters</em> are represented by <em>surrogate
80 * pairs</em> (see the section <a href="Character.html#unicode">Unicode
81 * Character Representations</a> in the <code>Character</code> class for
83 * Index values refer to <code>char</code> code units, so a supplementary
84 * character uses two positions in a <code>String</code>.
85 * <p>The <code>String</code> class provides methods for dealing with
86 * Unicode code points (i.e., characters), in addition to those for
87 * dealing with Unicode code units (i.e., <code>char</code> values).
90 * @author Arthur van Hoff
91 * @author Martin Buchholz
93 * @see java.lang.Object#toString()
94 * @see java.lang.StringBuffer
95 * @see java.lang.StringBuilder
96 * @see java.nio.charset.Charset
101 resource="/org/apidesign/vm4brwsr/emul/java_lang_String.js",
102 processByteCode=false
104 public final class String
105 implements java.io.Serializable, Comparable<String>, CharSequence
107 /** The value is used for character storage. */
108 private final char value[];
110 /** The offset is the first index of the storage that is used. */
111 private final int offset;
113 /** The count is the number of characters in the String. */
114 private final int count;
116 /** Cache the hash code for the string */
117 private int hash; // Default to 0
119 /** use serialVersionUID from JDK 1.0.2 for interoperability */
120 private static final long serialVersionUID = -6849794470754667710L;
123 * Class String is special cased within the Serialization Stream Protocol.
125 * A String instance is written initially into an ObjectOutputStream in the
128 * <code>TC_STRING</code> (utf String)
130 * The String is written by method <code>DataOutput.writeUTF</code>.
131 * A new handle is generated to refer to all future references to the
132 * string instance within the stream.
134 // private static final ObjectStreamField[] serialPersistentFields =
135 // new ObjectStreamField[0];
138 * Initializes a newly created {@code String} object so that it represents
139 * an empty character sequence. Note that use of this constructor is
140 * unnecessary since Strings are immutable.
145 this.value = new char[0];
149 * Initializes a newly created {@code String} object so that it represents
150 * the same sequence of characters as the argument; in other words, the
151 * newly created string is a copy of the argument string. Unless an
152 * explicit copy of {@code original} is needed, use of this constructor is
153 * unnecessary since Strings are immutable.
158 public String(String original) {
159 int size = original.count;
160 char[] originalValue = original.value;
162 if (originalValue.length > size) {
163 // The array representing the String is bigger than the new
164 // String itself. Perhaps this constructor is being called
165 // in order to trim the baggage, so make a copy of the array.
166 int off = original.offset;
167 v = AbstractStringBuilder.copyOfRange(originalValue, off, off+size);
169 // The array representing the String is the same
170 // size as the String, so no point in making a copy.
179 * Allocates a new {@code String} so that it represents the sequence of
180 * characters currently contained in the character array argument. The
181 * contents of the character array are copied; subsequent modification of
182 * the character array does not affect the newly created string.
185 * The initial value of the string
187 public String(char value[]) {
188 int size = value.length;
191 this.value = AbstractStringBuilder.copyOf(value, size);
195 * Allocates a new {@code String} that contains characters from a subarray
196 * of the character array argument. The {@code offset} argument is the
197 * index of the first character of the subarray and the {@code count}
198 * argument specifies the length of the subarray. The contents of the
199 * subarray are copied; subsequent modification of the character array does
200 * not affect the newly created string.
203 * Array that is the source of characters
211 * @throws IndexOutOfBoundsException
212 * If the {@code offset} and {@code count} arguments index
213 * characters outside the bounds of the {@code value} array
215 public String(char value[], int offset, int count) {
217 throw new StringIndexOutOfBoundsException(offset);
220 throw new StringIndexOutOfBoundsException(count);
222 // Note: offset or count might be near -1>>>1.
223 if (offset > value.length - count) {
224 throw new StringIndexOutOfBoundsException(offset + count);
228 this.value = AbstractStringBuilder.copyOfRange(value, offset, offset+count);
232 * Allocates a new {@code String} that contains characters from a subarray
233 * of the <a href="Character.html#unicode">Unicode code point</a> array
234 * argument. The {@code offset} argument is the index of the first code
235 * point of the subarray and the {@code count} argument specifies the
236 * length of the subarray. The contents of the subarray are converted to
237 * {@code char}s; subsequent modification of the {@code int} array does not
238 * affect the newly created string.
241 * Array that is the source of Unicode code points
249 * @throws IllegalArgumentException
250 * If any invalid Unicode code point is found in {@code
253 * @throws IndexOutOfBoundsException
254 * If the {@code offset} and {@code count} arguments index
255 * characters outside the bounds of the {@code codePoints} array
259 public String(int[] codePoints, int offset, int count) {
261 throw new StringIndexOutOfBoundsException(offset);
264 throw new StringIndexOutOfBoundsException(count);
266 // Note: offset or count might be near -1>>>1.
267 if (offset > codePoints.length - count) {
268 throw new StringIndexOutOfBoundsException(offset + count);
271 final int end = offset + count;
273 // Pass 1: Compute precise size of char[]
275 for (int i = offset; i < end; i++) {
276 int c = codePoints[i];
277 if (Character.isBmpCodePoint(c))
279 else if (Character.isValidCodePoint(c))
281 else throw new IllegalArgumentException(Integer.toString(c));
284 // Pass 2: Allocate and fill in char[]
285 final char[] v = new char[n];
287 for (int i = offset, j = 0; i < end; i++, j++) {
288 int c = codePoints[i];
289 if (Character.isBmpCodePoint(c))
292 Character.toSurrogates(c, v, j++);
301 * Allocates a new {@code String} constructed from a subarray of an array
302 * of 8-bit integer values.
304 * <p> The {@code offset} argument is the index of the first byte of the
305 * subarray, and the {@code count} argument specifies the length of the
308 * <p> Each {@code byte} in the subarray is converted to a {@code char} as
309 * specified in the method above.
311 * @deprecated This method does not properly convert bytes into characters.
312 * As of JDK 1.1, the preferred way to do this is via the
313 * {@code String} constructors that take a {@link
314 * java.nio.charset.Charset}, charset name, or that use the platform's
318 * The bytes to be converted to characters
321 * The top 8 bits of each 16-bit Unicode code unit
328 * @throws IndexOutOfBoundsException
329 * If the {@code offset} or {@code count} argument is invalid
331 * @see #String(byte[], int)
332 * @see #String(byte[], int, int, java.lang.String)
333 * @see #String(byte[], int, int, java.nio.charset.Charset)
334 * @see #String(byte[], int, int)
335 * @see #String(byte[], java.lang.String)
336 * @see #String(byte[], java.nio.charset.Charset)
337 * @see #String(byte[])
340 public String(byte ascii[], int hibyte, int offset, int count) {
341 checkBounds(ascii, offset, count);
342 char value[] = new char[count];
345 for (int i = count ; i-- > 0 ;) {
346 value[i] = (char) (ascii[i + offset] & 0xff);
350 for (int i = count ; i-- > 0 ;) {
351 value[i] = (char) (hibyte | (ascii[i + offset] & 0xff));
360 * Allocates a new {@code String} containing characters constructed from
361 * an array of 8-bit integer values. Each character <i>c</i>in the
362 * resulting string is constructed from the corresponding component
363 * <i>b</i> in the byte array such that:
366 * <b><i>c</i></b> == (char)(((hibyte & 0xff) << 8)
367 * | (<b><i>b</i></b> & 0xff))
368 * </pre></blockquote>
370 * @deprecated This method does not properly convert bytes into
371 * characters. As of JDK 1.1, the preferred way to do this is via the
372 * {@code String} constructors that take a {@link
373 * java.nio.charset.Charset}, charset name, or that use the platform's
377 * The bytes to be converted to characters
380 * The top 8 bits of each 16-bit Unicode code unit
382 * @see #String(byte[], int, int, java.lang.String)
383 * @see #String(byte[], int, int, java.nio.charset.Charset)
384 * @see #String(byte[], int, int)
385 * @see #String(byte[], java.lang.String)
386 * @see #String(byte[], java.nio.charset.Charset)
387 * @see #String(byte[])
390 public String(byte ascii[], int hibyte) {
391 this(ascii, hibyte, 0, ascii.length);
394 /* Common private utility method used to bounds check the byte array
395 * and requested offset & length values used by the String(byte[],..)
398 private static void checkBounds(byte[] bytes, int offset, int length) {
400 throw new StringIndexOutOfBoundsException(length);
402 throw new StringIndexOutOfBoundsException(offset);
403 if (offset > bytes.length - length)
404 throw new StringIndexOutOfBoundsException(offset + length);
408 * Constructs a new {@code String} by decoding the specified subarray of
409 * bytes using the specified charset. The length of the new {@code String}
410 * is a function of the charset, and hence may not be equal to the length
413 * <p> The behavior of this constructor when the given bytes are not valid
414 * in the given charset is unspecified. The {@link
415 * java.nio.charset.CharsetDecoder} class should be used when more control
416 * over the decoding process is required.
419 * The bytes to be decoded into characters
422 * The index of the first byte to decode
425 * The number of bytes to decode
428 * The name of a supported {@linkplain java.nio.charset.Charset
431 * @throws UnsupportedEncodingException
432 * If the named charset is not supported
434 * @throws IndexOutOfBoundsException
435 * If the {@code offset} and {@code length} arguments index
436 * characters outside the bounds of the {@code bytes} array
440 // public String(byte bytes[], int offset, int length, String charsetName)
441 // throws UnsupportedEncodingException
443 // if (charsetName == null)
444 // throw new NullPointerException("charsetName");
445 // checkBounds(bytes, offset, length);
446 // char[] v = StringCoding.decode(charsetName, bytes, offset, length);
448 // this.count = v.length;
453 * Constructs a new {@code String} by decoding the specified subarray of
454 * bytes using the specified {@linkplain java.nio.charset.Charset charset}.
455 * The length of the new {@code String} is a function of the charset, and
456 * hence may not be equal to the length of the subarray.
458 * <p> This method always replaces malformed-input and unmappable-character
459 * sequences with this charset's default replacement string. The {@link
460 * java.nio.charset.CharsetDecoder} class should be used when more control
461 * over the decoding process is required.
464 * The bytes to be decoded into characters
467 * The index of the first byte to decode
470 * The number of bytes to decode
473 * The {@linkplain java.nio.charset.Charset charset} to be used to
474 * decode the {@code bytes}
476 * @throws IndexOutOfBoundsException
477 * If the {@code offset} and {@code length} arguments index
478 * characters outside the bounds of the {@code bytes} array
482 /* don't want dependnecy on Charset
483 public String(byte bytes[], int offset, int length, Charset charset) {
485 throw new NullPointerException("charset");
486 checkBounds(bytes, offset, length);
487 char[] v = StringCoding.decode(charset, bytes, offset, length);
489 this.count = v.length;
495 * Constructs a new {@code String} by decoding the specified array of bytes
496 * using the specified {@linkplain java.nio.charset.Charset charset}. The
497 * length of the new {@code String} is a function of the charset, and hence
498 * may not be equal to the length of the byte array.
500 * <p> The behavior of this constructor when the given bytes are not valid
501 * in the given charset is unspecified. The {@link
502 * java.nio.charset.CharsetDecoder} class should be used when more control
503 * over the decoding process is required.
506 * The bytes to be decoded into characters
509 * The name of a supported {@linkplain java.nio.charset.Charset
512 * @throws UnsupportedEncodingException
513 * If the named charset is not supported
517 // public String(byte bytes[], String charsetName)
518 // throws UnsupportedEncodingException
520 // this(bytes, 0, bytes.length, charsetName);
524 * Constructs a new {@code String} by decoding the specified array of
525 * bytes using the specified {@linkplain java.nio.charset.Charset charset}.
526 * The length of the new {@code String} is a function of the charset, and
527 * hence may not be equal to the length of the byte array.
529 * <p> This method always replaces malformed-input and unmappable-character
530 * sequences with this charset's default replacement string. The {@link
531 * java.nio.charset.CharsetDecoder} class should be used when more control
532 * over the decoding process is required.
535 * The bytes to be decoded into characters
538 * The {@linkplain java.nio.charset.Charset charset} to be used to
539 * decode the {@code bytes}
543 /* don't want dep on Charset
544 public String(byte bytes[], Charset charset) {
545 this(bytes, 0, bytes.length, charset);
550 * Constructs a new {@code String} by decoding the specified subarray of
551 * bytes using the platform's default charset. The length of the new
552 * {@code String} is a function of the charset, and hence may not be equal
553 * to the length of the subarray.
555 * <p> The behavior of this constructor when the given bytes are not valid
556 * in the default charset is unspecified. The {@link
557 * java.nio.charset.CharsetDecoder} class should be used when more control
558 * over the decoding process is required.
561 * The bytes to be decoded into characters
564 * The index of the first byte to decode
567 * The number of bytes to decode
569 * @throws IndexOutOfBoundsException
570 * If the {@code offset} and the {@code length} arguments index
571 * characters outside the bounds of the {@code bytes} array
575 public String(byte bytes[], int offset, int length) {
576 checkBounds(bytes, offset, length);
577 char[] v = new char[length];
578 for (int i = 0; i < length; i++) {
579 v[i] = (char)bytes[offset++];
582 this.count = v.length;
587 * Constructs a new {@code String} by decoding the specified array of bytes
588 * using the platform's default charset. The length of the new {@code
589 * String} is a function of the charset, and hence may not be equal to the
590 * length of the byte array.
592 * <p> The behavior of this constructor when the given bytes are not valid
593 * in the default charset is unspecified. The {@link
594 * java.nio.charset.CharsetDecoder} class should be used when more control
595 * over the decoding process is required.
598 * The bytes to be decoded into characters
602 public String(byte bytes[]) {
603 this(bytes, 0, bytes.length);
607 * Allocates a new string that contains the sequence of characters
608 * currently contained in the string buffer argument. The contents of the
609 * string buffer are copied; subsequent modification of the string buffer
610 * does not affect the newly created string.
613 * A {@code StringBuffer}
615 public String(StringBuffer buffer) {
616 String result = buffer.toString();
617 this.value = result.value;
618 this.count = result.count;
619 this.offset = result.offset;
623 * Allocates a new string that contains the sequence of characters
624 * currently contained in the string builder argument. The contents of the
625 * string builder are copied; subsequent modification of the string builder
626 * does not affect the newly created string.
628 * <p> This constructor is provided to ease migration to {@code
629 * StringBuilder}. Obtaining a string from a string builder via the {@code
630 * toString} method is likely to run faster and is generally preferred.
633 * A {@code StringBuilder}
637 public String(StringBuilder builder) {
638 String result = builder.toString();
639 this.value = result.value;
640 this.count = result.count;
641 this.offset = result.offset;
645 * Returns the length of this string.
646 * The length is equal to the number of <a href="Character.html#unicode">Unicode
647 * code units</a> in the string.
649 * @return the length of the sequence of characters represented by this
652 public int length() {
657 * Returns <tt>true</tt> if, and only if, {@link #length()} is <tt>0</tt>.
659 * @return <tt>true</tt> if {@link #length()} is <tt>0</tt>, otherwise
664 public boolean isEmpty() {
669 * Returns the <code>char</code> value at the
670 * specified index. An index ranges from <code>0</code> to
671 * <code>length() - 1</code>. The first <code>char</code> value of the sequence
672 * is at index <code>0</code>, the next at index <code>1</code>,
673 * and so on, as for array indexing.
675 * <p>If the <code>char</code> value specified by the index is a
676 * <a href="Character.html#unicode">surrogate</a>, the surrogate
679 * @param index the index of the <code>char</code> value.
680 * @return the <code>char</code> value at the specified index of this string.
681 * The first <code>char</code> value is at index <code>0</code>.
682 * @exception IndexOutOfBoundsException if the <code>index</code>
683 * argument is negative or not less than the length of this
686 public char charAt(int index) {
687 if ((index < 0) || (index >= count)) {
688 throw new StringIndexOutOfBoundsException(index);
690 return value[index + offset];
694 * Returns the character (Unicode code point) at the specified
695 * index. The index refers to <code>char</code> values
696 * (Unicode code units) and ranges from <code>0</code> to
697 * {@link #length()}<code> - 1</code>.
699 * <p> If the <code>char</code> value specified at the given index
700 * is in the high-surrogate range, the following index is less
701 * than the length of this <code>String</code>, and the
702 * <code>char</code> value at the following index is in the
703 * low-surrogate range, then the supplementary code point
704 * corresponding to this surrogate pair is returned. Otherwise,
705 * the <code>char</code> value at the given index is returned.
707 * @param index the index to the <code>char</code> values
708 * @return the code point value of the character at the
710 * @exception IndexOutOfBoundsException if the <code>index</code>
711 * argument is negative or not less than the length of this
715 public int codePointAt(int index) {
716 if ((index < 0) || (index >= count)) {
717 throw new StringIndexOutOfBoundsException(index);
719 return Character.codePointAtImpl(value, offset + index, offset + count);
723 * Returns the character (Unicode code point) before the specified
724 * index. The index refers to <code>char</code> values
725 * (Unicode code units) and ranges from <code>1</code> to {@link
726 * CharSequence#length() length}.
728 * <p> If the <code>char</code> value at <code>(index - 1)</code>
729 * is in the low-surrogate range, <code>(index - 2)</code> is not
730 * negative, and the <code>char</code> value at <code>(index -
731 * 2)</code> is in the high-surrogate range, then the
732 * supplementary code point value of the surrogate pair is
733 * returned. If the <code>char</code> value at <code>index -
734 * 1</code> is an unpaired low-surrogate or a high-surrogate, the
735 * surrogate value is returned.
737 * @param index the index following the code point that should be returned
738 * @return the Unicode code point value before the given index.
739 * @exception IndexOutOfBoundsException if the <code>index</code>
740 * argument is less than 1 or greater than the length
744 public int codePointBefore(int index) {
746 if ((i < 0) || (i >= count)) {
747 throw new StringIndexOutOfBoundsException(index);
749 return Character.codePointBeforeImpl(value, offset + index, offset);
753 * Returns the number of Unicode code points in the specified text
754 * range of this <code>String</code>. The text range begins at the
755 * specified <code>beginIndex</code> and extends to the
756 * <code>char</code> at index <code>endIndex - 1</code>. Thus the
757 * length (in <code>char</code>s) of the text range is
758 * <code>endIndex-beginIndex</code>. Unpaired surrogates within
759 * the text range count as one code point each.
761 * @param beginIndex the index to the first <code>char</code> of
763 * @param endIndex the index after the last <code>char</code> of
765 * @return the number of Unicode code points in the specified text
767 * @exception IndexOutOfBoundsException if the
768 * <code>beginIndex</code> is negative, or <code>endIndex</code>
769 * is larger than the length of this <code>String</code>, or
770 * <code>beginIndex</code> is larger than <code>endIndex</code>.
773 public int codePointCount(int beginIndex, int endIndex) {
774 if (beginIndex < 0 || endIndex > count || beginIndex > endIndex) {
775 throw new IndexOutOfBoundsException();
777 return Character.codePointCountImpl(value, offset+beginIndex, endIndex-beginIndex);
781 * Returns the index within this <code>String</code> that is
782 * offset from the given <code>index</code> by
783 * <code>codePointOffset</code> code points. Unpaired surrogates
784 * within the text range given by <code>index</code> and
785 * <code>codePointOffset</code> count as one code point each.
787 * @param index the index to be offset
788 * @param codePointOffset the offset in code points
789 * @return the index within this <code>String</code>
790 * @exception IndexOutOfBoundsException if <code>index</code>
791 * is negative or larger then the length of this
792 * <code>String</code>, or if <code>codePointOffset</code> is positive
793 * and the substring starting with <code>index</code> has fewer
794 * than <code>codePointOffset</code> code points,
795 * or if <code>codePointOffset</code> is negative and the substring
796 * before <code>index</code> has fewer than the absolute value
797 * of <code>codePointOffset</code> code points.
800 public int offsetByCodePoints(int index, int codePointOffset) {
801 if (index < 0 || index > count) {
802 throw new IndexOutOfBoundsException();
804 return Character.offsetByCodePointsImpl(value, offset, count,
805 offset+index, codePointOffset) - offset;
809 * Copy characters from this string into dst starting at dstBegin.
810 * This method doesn't perform any range checking.
812 void getChars(char dst[], int dstBegin) {
813 AbstractStringBuilder.arraycopy(value, offset, dst, dstBegin, count);
817 * Copies characters from this string into the destination character
820 * The first character to be copied is at index <code>srcBegin</code>;
821 * the last character to be copied is at index <code>srcEnd-1</code>
822 * (thus the total number of characters to be copied is
823 * <code>srcEnd-srcBegin</code>). The characters are copied into the
824 * subarray of <code>dst</code> starting at index <code>dstBegin</code>
825 * and ending at index:
826 * <p><blockquote><pre>
827 * dstbegin + (srcEnd-srcBegin) - 1
828 * </pre></blockquote>
830 * @param srcBegin index of the first character in the string
832 * @param srcEnd index after the last character in the string
834 * @param dst the destination array.
835 * @param dstBegin the start offset in the destination array.
836 * @exception IndexOutOfBoundsException If any of the following
838 * <ul><li><code>srcBegin</code> is negative.
839 * <li><code>srcBegin</code> is greater than <code>srcEnd</code>
840 * <li><code>srcEnd</code> is greater than the length of this
842 * <li><code>dstBegin</code> is negative
843 * <li><code>dstBegin+(srcEnd-srcBegin)</code> is larger than
844 * <code>dst.length</code></ul>
846 public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {
848 throw new StringIndexOutOfBoundsException(srcBegin);
850 if (srcEnd > count) {
851 throw new StringIndexOutOfBoundsException(srcEnd);
853 if (srcBegin > srcEnd) {
854 throw new StringIndexOutOfBoundsException(srcEnd - srcBegin);
856 AbstractStringBuilder.arraycopy(value, offset + srcBegin, dst, dstBegin,
861 * Copies characters from this string into the destination byte array. Each
862 * byte receives the 8 low-order bits of the corresponding character. The
863 * eight high-order bits of each character are not copied and do not
864 * participate in the transfer in any way.
866 * <p> The first character to be copied is at index {@code srcBegin}; the
867 * last character to be copied is at index {@code srcEnd-1}. The total
868 * number of characters to be copied is {@code srcEnd-srcBegin}. The
869 * characters, converted to bytes, are copied into the subarray of {@code
870 * dst} starting at index {@code dstBegin} and ending at index:
873 * dstbegin + (srcEnd-srcBegin) - 1
874 * </pre></blockquote>
876 * @deprecated This method does not properly convert characters into
877 * bytes. As of JDK 1.1, the preferred way to do this is via the
878 * {@link #getBytes()} method, which uses the platform's default charset.
881 * Index of the first character in the string to copy
884 * Index after the last character in the string to copy
887 * The destination array
890 * The start offset in the destination array
892 * @throws IndexOutOfBoundsException
893 * If any of the following is true:
895 * <li> {@code srcBegin} is negative
896 * <li> {@code srcBegin} is greater than {@code srcEnd}
897 * <li> {@code srcEnd} is greater than the length of this String
898 * <li> {@code dstBegin} is negative
899 * <li> {@code dstBegin+(srcEnd-srcBegin)} is larger than {@code
904 public void getBytes(int srcBegin, int srcEnd, byte dst[], int dstBegin) {
906 throw new StringIndexOutOfBoundsException(srcBegin);
908 if (srcEnd > count) {
909 throw new StringIndexOutOfBoundsException(srcEnd);
911 if (srcBegin > srcEnd) {
912 throw new StringIndexOutOfBoundsException(srcEnd - srcBegin);
915 int n = offset + srcEnd;
916 int i = offset + srcBegin;
917 char[] val = value; /* avoid getfield opcode */
920 dst[j++] = (byte)val[i++];
925 * Encodes this {@code String} into a sequence of bytes using the named
926 * charset, storing the result into a new byte array.
928 * <p> The behavior of this method when this string cannot be encoded in
929 * the given charset is unspecified. The {@link
930 * java.nio.charset.CharsetEncoder} class should be used when more control
931 * over the encoding process is required.
934 * The name of a supported {@linkplain java.nio.charset.Charset
937 * @return The resultant byte array
939 * @throws UnsupportedEncodingException
940 * If the named charset is not supported
944 // public byte[] getBytes(String charsetName)
945 // throws UnsupportedEncodingException
947 // if (charsetName == null) throw new NullPointerException();
948 // return StringCoding.encode(charsetName, value, offset, count);
952 * Encodes this {@code String} into a sequence of bytes using the given
953 * {@linkplain java.nio.charset.Charset charset}, storing the result into a
956 * <p> This method always replaces malformed-input and unmappable-character
957 * sequences with this charset's default replacement byte array. The
958 * {@link java.nio.charset.CharsetEncoder} class should be used when more
959 * control over the encoding process is required.
962 * The {@linkplain java.nio.charset.Charset} to be used to encode
965 * @return The resultant byte array
969 /* don't want dep on Charset
970 public byte[] getBytes(Charset charset) {
971 if (charset == null) throw new NullPointerException();
972 return StringCoding.encode(charset, value, offset, count);
977 * Encodes this {@code String} into a sequence of bytes using the
978 * platform's default charset, storing the result into a new byte array.
980 * <p> The behavior of this method when this string cannot be encoded in
981 * the default charset is unspecified. The {@link
982 * java.nio.charset.CharsetEncoder} class should be used when more control
983 * over the encoding process is required.
985 * @return The resultant byte array
989 public byte[] getBytes() {
990 byte[] arr = new byte[length()];
991 for (int i = 0; i < arr.length; i++) {
992 final char v = charAt(i);
999 * Compares this string to the specified object. The result is {@code
1000 * true} if and only if the argument is not {@code null} and is a {@code
1001 * String} object that represents the same sequence of characters as this
1005 * The object to compare this {@code String} against
1007 * @return {@code true} if the given object represents a {@code String}
1008 * equivalent to this string, {@code false} otherwise
1010 * @see #compareTo(String)
1011 * @see #equalsIgnoreCase(String)
1013 public boolean equals(Object anObject) {
1014 if (this == anObject) {
1017 if (anObject instanceof String) {
1018 String anotherString = (String)anObject;
1020 if (n == anotherString.count) {
1022 char v2[] = anotherString.value;
1024 int j = anotherString.offset;
1026 if (v1[i++] != v2[j++])
1036 * Compares this string to the specified {@code StringBuffer}. The result
1037 * is {@code true} if and only if this {@code String} represents the same
1038 * sequence of characters as the specified {@code StringBuffer}.
1041 * The {@code StringBuffer} to compare this {@code String} against
1043 * @return {@code true} if this {@code String} represents the same
1044 * sequence of characters as the specified {@code StringBuffer},
1045 * {@code false} otherwise
1049 public boolean contentEquals(StringBuffer sb) {
1051 return contentEquals((CharSequence)sb);
1056 * Compares this string to the specified {@code CharSequence}. The result
1057 * is {@code true} if and only if this {@code String} represents the same
1058 * sequence of char values as the specified sequence.
1061 * The sequence to compare this {@code String} against
1063 * @return {@code true} if this {@code String} represents the same
1064 * sequence of char values as the specified sequence, {@code
1069 public boolean contentEquals(CharSequence cs) {
1070 if (count != cs.length())
1072 // Argument is a StringBuffer, StringBuilder
1073 if (cs instanceof AbstractStringBuilder) {
1075 char v2[] = ((AbstractStringBuilder)cs).getValue();
1080 if (v1[i++] != v2[j++])
1085 // Argument is a String
1086 if (cs.equals(this))
1088 // Argument is a generic CharSequence
1094 if (v1[i++] != cs.charAt(j++))
1101 * Compares this {@code String} to another {@code String}, ignoring case
1102 * considerations. Two strings are considered equal ignoring case if they
1103 * are of the same length and corresponding characters in the two strings
1104 * are equal ignoring case.
1106 * <p> Two characters {@code c1} and {@code c2} are considered the same
1107 * ignoring case if at least one of the following is true:
1109 * <li> The two characters are the same (as compared by the
1110 * {@code ==} operator)
1111 * <li> Applying the method {@link
1112 * java.lang.Character#toUpperCase(char)} to each character
1113 * produces the same result
1114 * <li> Applying the method {@link
1115 * java.lang.Character#toLowerCase(char)} to each character
1116 * produces the same result
1119 * @param anotherString
1120 * The {@code String} to compare this {@code String} against
1122 * @return {@code true} if the argument is not {@code null} and it
1123 * represents an equivalent {@code String} ignoring case; {@code
1126 * @see #equals(Object)
1128 public boolean equalsIgnoreCase(String anotherString) {
1129 return (this == anotherString) ? true :
1130 (anotherString != null) && (anotherString.count == count) &&
1131 regionMatches(true, 0, anotherString, 0, count);
1135 * Compares two strings lexicographically.
1136 * The comparison is based on the Unicode value of each character in
1137 * the strings. The character sequence represented by this
1138 * <code>String</code> object is compared lexicographically to the
1139 * character sequence represented by the argument string. The result is
1140 * a negative integer if this <code>String</code> object
1141 * lexicographically precedes the argument string. The result is a
1142 * positive integer if this <code>String</code> object lexicographically
1143 * follows the argument string. The result is zero if the strings
1144 * are equal; <code>compareTo</code> returns <code>0</code> exactly when
1145 * the {@link #equals(Object)} method would return <code>true</code>.
1147 * This is the definition of lexicographic ordering. If two strings are
1148 * different, then either they have different characters at some index
1149 * that is a valid index for both strings, or their lengths are different,
1150 * or both. If they have different characters at one or more index
1151 * positions, let <i>k</i> be the smallest such index; then the string
1152 * whose character at position <i>k</i> has the smaller value, as
1153 * determined by using the < operator, lexicographically precedes the
1154 * other string. In this case, <code>compareTo</code> returns the
1155 * difference of the two character values at position <code>k</code> in
1156 * the two string -- that is, the value:
1158 * this.charAt(k)-anotherString.charAt(k)
1159 * </pre></blockquote>
1160 * If there is no index position at which they differ, then the shorter
1161 * string lexicographically precedes the longer string. In this case,
1162 * <code>compareTo</code> returns the difference of the lengths of the
1163 * strings -- that is, the value:
1165 * this.length()-anotherString.length()
1166 * </pre></blockquote>
1168 * @param anotherString the <code>String</code> to be compared.
1169 * @return the value <code>0</code> if the argument string is equal to
1170 * this string; a value less than <code>0</code> if this string
1171 * is lexicographically less than the string argument; and a
1172 * value greater than <code>0</code> if this string is
1173 * lexicographically greater than the string argument.
1175 public int compareTo(String anotherString) {
1177 int len2 = anotherString.count;
1178 int n = Math.min(len1, len2);
1180 char v2[] = anotherString.value;
1182 int j = anotherString.offset;
1208 * A Comparator that orders <code>String</code> objects as by
1209 * <code>compareToIgnoreCase</code>. This comparator is serializable.
1211 * Note that this Comparator does <em>not</em> take locale into account,
1212 * and will result in an unsatisfactory ordering for certain locales.
1213 * The java.text package provides <em>Collators</em> to allow
1214 * locale-sensitive ordering.
1216 * @see java.text.Collator#compare(String, String)
1219 public static final Comparator<String> CASE_INSENSITIVE_ORDER
1220 = new CaseInsensitiveComparator();
1221 private static class CaseInsensitiveComparator
1222 implements Comparator<String>, java.io.Serializable {
1223 // use serialVersionUID from JDK 1.2.2 for interoperability
1224 private static final long serialVersionUID = 8575799808933029326L;
1226 public int compare(String s1, String s2) {
1227 int n1 = s1.length();
1228 int n2 = s2.length();
1229 int min = Math.min(n1, n2);
1230 for (int i = 0; i < min; i++) {
1231 char c1 = s1.charAt(i);
1232 char c2 = s2.charAt(i);
1234 c1 = Character.toUpperCase(c1);
1235 c2 = Character.toUpperCase(c2);
1237 c1 = Character.toLowerCase(c1);
1238 c2 = Character.toLowerCase(c2);
1240 // No overflow because of numeric promotion
1251 * Compares two strings lexicographically, ignoring case
1252 * differences. This method returns an integer whose sign is that of
1253 * calling <code>compareTo</code> with normalized versions of the strings
1254 * where case differences have been eliminated by calling
1255 * <code>Character.toLowerCase(Character.toUpperCase(character))</code> on
1258 * Note that this method does <em>not</em> take locale into account,
1259 * and will result in an unsatisfactory ordering for certain locales.
1260 * The java.text package provides <em>collators</em> to allow
1261 * locale-sensitive ordering.
1263 * @param str the <code>String</code> to be compared.
1264 * @return a negative integer, zero, or a positive integer as the
1265 * specified String is greater than, equal to, or less
1266 * than this String, ignoring case considerations.
1267 * @see java.text.Collator#compare(String, String)
1270 public int compareToIgnoreCase(String str) {
1271 return CASE_INSENSITIVE_ORDER.compare(this, str);
1275 * Tests if two string regions are equal.
1277 * A substring of this <tt>String</tt> object is compared to a substring
1278 * of the argument other. The result is true if these substrings
1279 * represent identical character sequences. The substring of this
1280 * <tt>String</tt> object to be compared begins at index <tt>toffset</tt>
1281 * and has length <tt>len</tt>. The substring of other to be compared
1282 * begins at index <tt>ooffset</tt> and has length <tt>len</tt>. The
1283 * result is <tt>false</tt> if and only if at least one of the following
1285 * <ul><li><tt>toffset</tt> is negative.
1286 * <li><tt>ooffset</tt> is negative.
1287 * <li><tt>toffset+len</tt> is greater than the length of this
1288 * <tt>String</tt> object.
1289 * <li><tt>ooffset+len</tt> is greater than the length of the other
1291 * <li>There is some nonnegative integer <i>k</i> less than <tt>len</tt>
1293 * <tt>this.charAt(toffset+<i>k</i>) != other.charAt(ooffset+<i>k</i>)</tt>
1296 * @param toffset the starting offset of the subregion in this string.
1297 * @param other the string argument.
1298 * @param ooffset the starting offset of the subregion in the string
1300 * @param len the number of characters to compare.
1301 * @return <code>true</code> if the specified subregion of this string
1302 * exactly matches the specified subregion of the string argument;
1303 * <code>false</code> otherwise.
1305 public boolean regionMatches(int toffset, String other, int ooffset,
1308 int to = offset + toffset;
1309 char pa[] = other.value;
1310 int po = other.offset + ooffset;
1311 // Note: toffset, ooffset, or len might be near -1>>>1.
1312 if ((ooffset < 0) || (toffset < 0) || (toffset > (long)count - len)
1313 || (ooffset > (long)other.count - len)) {
1317 if (ta[to++] != pa[po++]) {
1325 * Tests if two string regions are equal.
1327 * A substring of this <tt>String</tt> object is compared to a substring
1328 * of the argument <tt>other</tt>. The result is <tt>true</tt> if these
1329 * substrings represent character sequences that are the same, ignoring
1330 * case if and only if <tt>ignoreCase</tt> is true. The substring of
1331 * this <tt>String</tt> object to be compared begins at index
1332 * <tt>toffset</tt> and has length <tt>len</tt>. The substring of
1333 * <tt>other</tt> to be compared begins at index <tt>ooffset</tt> and
1334 * has length <tt>len</tt>. The result is <tt>false</tt> if and only if
1335 * at least one of the following is true:
1336 * <ul><li><tt>toffset</tt> is negative.
1337 * <li><tt>ooffset</tt> is negative.
1338 * <li><tt>toffset+len</tt> is greater than the length of this
1339 * <tt>String</tt> object.
1340 * <li><tt>ooffset+len</tt> is greater than the length of the other
1342 * <li><tt>ignoreCase</tt> is <tt>false</tt> and there is some nonnegative
1343 * integer <i>k</i> less than <tt>len</tt> such that:
1345 * this.charAt(toffset+k) != other.charAt(ooffset+k)
1346 * </pre></blockquote>
1347 * <li><tt>ignoreCase</tt> is <tt>true</tt> and there is some nonnegative
1348 * integer <i>k</i> less than <tt>len</tt> such that:
1350 * Character.toLowerCase(this.charAt(toffset+k)) !=
1351 Character.toLowerCase(other.charAt(ooffset+k))
1352 * </pre></blockquote>
1355 * Character.toUpperCase(this.charAt(toffset+k)) !=
1356 * Character.toUpperCase(other.charAt(ooffset+k))
1357 * </pre></blockquote>
1360 * @param ignoreCase if <code>true</code>, ignore case when comparing
1362 * @param toffset the starting offset of the subregion in this
1364 * @param other the string argument.
1365 * @param ooffset the starting offset of the subregion in the string
1367 * @param len the number of characters to compare.
1368 * @return <code>true</code> if the specified subregion of this string
1369 * matches the specified subregion of the string argument;
1370 * <code>false</code> otherwise. Whether the matching is exact
1371 * or case insensitive depends on the <code>ignoreCase</code>
1374 public boolean regionMatches(boolean ignoreCase, int toffset,
1375 String other, int ooffset, int len) {
1377 int to = offset + toffset;
1378 char pa[] = other.value;
1379 int po = other.offset + ooffset;
1380 // Note: toffset, ooffset, or len might be near -1>>>1.
1381 if ((ooffset < 0) || (toffset < 0) || (toffset > (long)count - len) ||
1382 (ooffset > (long)other.count - len)) {
1392 // If characters don't match but case may be ignored,
1393 // try converting both characters to uppercase.
1394 // If the results match, then the comparison scan should
1396 char u1 = Character.toUpperCase(c1);
1397 char u2 = Character.toUpperCase(c2);
1401 // Unfortunately, conversion to uppercase does not work properly
1402 // for the Georgian alphabet, which has strange rules about case
1403 // conversion. So we need to make one last check before
1405 if (Character.toLowerCase(u1) == Character.toLowerCase(u2)) {
1415 * Tests if the substring of this string beginning at the
1416 * specified index starts with the specified prefix.
1418 * @param prefix the prefix.
1419 * @param toffset where to begin looking in this string.
1420 * @return <code>true</code> if the character sequence represented by the
1421 * argument is a prefix of the substring of this object starting
1422 * at index <code>toffset</code>; <code>false</code> otherwise.
1423 * The result is <code>false</code> if <code>toffset</code> is
1424 * negative or greater than the length of this
1425 * <code>String</code> object; otherwise the result is the same
1426 * as the result of the expression
1428 * this.substring(toffset).startsWith(prefix)
1431 public boolean startsWith(String prefix, int toffset) {
1433 int to = offset + toffset;
1434 char pa[] = prefix.value;
1435 int po = prefix.offset;
1436 int pc = prefix.count;
1437 // Note: toffset might be near -1>>>1.
1438 if ((toffset < 0) || (toffset > count - pc)) {
1442 if (ta[to++] != pa[po++]) {
1450 * Tests if this string starts with the specified prefix.
1452 * @param prefix the prefix.
1453 * @return <code>true</code> if the character sequence represented by the
1454 * argument is a prefix of the character sequence represented by
1455 * this string; <code>false</code> otherwise.
1456 * Note also that <code>true</code> will be returned if the
1457 * argument is an empty string or is equal to this
1458 * <code>String</code> object as determined by the
1459 * {@link #equals(Object)} method.
1462 public boolean startsWith(String prefix) {
1463 return startsWith(prefix, 0);
1467 * Tests if this string ends with the specified suffix.
1469 * @param suffix the suffix.
1470 * @return <code>true</code> if the character sequence represented by the
1471 * argument is a suffix of the character sequence represented by
1472 * this object; <code>false</code> otherwise. Note that the
1473 * result will be <code>true</code> if the argument is the
1474 * empty string or is equal to this <code>String</code> object
1475 * as determined by the {@link #equals(Object)} method.
1477 public boolean endsWith(String suffix) {
1478 return startsWith(suffix, count - suffix.count);
1482 * Returns a hash code for this string. The hash code for a
1483 * <code>String</code> object is computed as
1485 * s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
1486 * </pre></blockquote>
1487 * using <code>int</code> arithmetic, where <code>s[i]</code> is the
1488 * <i>i</i>th character of the string, <code>n</code> is the length of
1489 * the string, and <code>^</code> indicates exponentiation.
1490 * (The hash value of the empty string is zero.)
1492 * @return a hash code value for this object.
1494 public int hashCode() {
1496 if (h == 0 && count > 0) {
1501 for (int i = 0; i < len; i++) {
1502 h = 31*h + val[off++];
1510 * Returns the index within this string of the first occurrence of
1511 * the specified character. If a character with value
1512 * <code>ch</code> occurs in the character sequence represented by
1513 * this <code>String</code> object, then the index (in Unicode
1514 * code units) of the first such occurrence is returned. For
1515 * values of <code>ch</code> in the range from 0 to 0xFFFF
1516 * (inclusive), this is the smallest value <i>k</i> such that:
1518 * this.charAt(<i>k</i>) == ch
1519 * </pre></blockquote>
1520 * is true. For other values of <code>ch</code>, it is the
1521 * smallest value <i>k</i> such that:
1523 * this.codePointAt(<i>k</i>) == ch
1524 * </pre></blockquote>
1525 * is true. In either case, if no such character occurs in this
1526 * string, then <code>-1</code> is returned.
1528 * @param ch a character (Unicode code point).
1529 * @return the index of the first occurrence of the character in the
1530 * character sequence represented by this object, or
1531 * <code>-1</code> if the character does not occur.
1533 public int indexOf(int ch) {
1534 return indexOf(ch, 0);
1538 * Returns the index within this string of the first occurrence of the
1539 * specified character, starting the search at the specified index.
1541 * If a character with value <code>ch</code> occurs in the
1542 * character sequence represented by this <code>String</code>
1543 * object at an index no smaller than <code>fromIndex</code>, then
1544 * the index of the first such occurrence is returned. For values
1545 * of <code>ch</code> in the range from 0 to 0xFFFF (inclusive),
1546 * this is the smallest value <i>k</i> such that:
1548 * (this.charAt(<i>k</i>) == ch) && (<i>k</i> >= fromIndex)
1549 * </pre></blockquote>
1550 * is true. For other values of <code>ch</code>, it is the
1551 * smallest value <i>k</i> such that:
1553 * (this.codePointAt(<i>k</i>) == ch) && (<i>k</i> >= fromIndex)
1554 * </pre></blockquote>
1555 * is true. In either case, if no such character occurs in this
1556 * string at or after position <code>fromIndex</code>, then
1557 * <code>-1</code> is returned.
1560 * There is no restriction on the value of <code>fromIndex</code>. If it
1561 * is negative, it has the same effect as if it were zero: this entire
1562 * string may be searched. If it is greater than the length of this
1563 * string, it has the same effect as if it were equal to the length of
1564 * this string: <code>-1</code> is returned.
1566 * <p>All indices are specified in <code>char</code> values
1567 * (Unicode code units).
1569 * @param ch a character (Unicode code point).
1570 * @param fromIndex the index to start the search from.
1571 * @return the index of the first occurrence of the character in the
1572 * character sequence represented by this object that is greater
1573 * than or equal to <code>fromIndex</code>, or <code>-1</code>
1574 * if the character does not occur.
1576 public int indexOf(int ch, int fromIndex) {
1577 if (fromIndex < 0) {
1579 } else if (fromIndex >= count) {
1580 // Note: fromIndex might be near -1>>>1.
1584 if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) {
1585 // handle most cases here (ch is a BMP code point or a
1586 // negative value (invalid code point))
1587 final char[] value = this.value;
1588 final int offset = this.offset;
1589 final int max = offset + count;
1590 for (int i = offset + fromIndex; i < max ; i++) {
1591 if (value[i] == ch) {
1597 return indexOfSupplementary(ch, fromIndex);
1602 * Handles (rare) calls of indexOf with a supplementary character.
1604 private int indexOfSupplementary(int ch, int fromIndex) {
1605 if (Character.isValidCodePoint(ch)) {
1606 final char[] value = this.value;
1607 final int offset = this.offset;
1608 final char hi = Character.highSurrogate(ch);
1609 final char lo = Character.lowSurrogate(ch);
1610 final int max = offset + count - 1;
1611 for (int i = offset + fromIndex; i < max; i++) {
1612 if (value[i] == hi && value[i+1] == lo) {
1621 * Returns the index within this string of the last occurrence of
1622 * the specified character. For values of <code>ch</code> in the
1623 * range from 0 to 0xFFFF (inclusive), the index (in Unicode code
1624 * units) returned is the largest value <i>k</i> such that:
1626 * this.charAt(<i>k</i>) == ch
1627 * </pre></blockquote>
1628 * is true. For other values of <code>ch</code>, it is the
1629 * largest value <i>k</i> such that:
1631 * this.codePointAt(<i>k</i>) == ch
1632 * </pre></blockquote>
1633 * is true. In either case, if no such character occurs in this
1634 * string, then <code>-1</code> is returned. The
1635 * <code>String</code> is searched backwards starting at the last
1638 * @param ch a character (Unicode code point).
1639 * @return the index of the last occurrence of the character in the
1640 * character sequence represented by this object, or
1641 * <code>-1</code> if the character does not occur.
1643 public int lastIndexOf(int ch) {
1644 return lastIndexOf(ch, count - 1);
1648 * Returns the index within this string of the last occurrence of
1649 * the specified character, searching backward starting at the
1650 * specified index. For values of <code>ch</code> in the range
1651 * from 0 to 0xFFFF (inclusive), the index returned is the largest
1652 * value <i>k</i> such that:
1654 * (this.charAt(<i>k</i>) == ch) && (<i>k</i> <= fromIndex)
1655 * </pre></blockquote>
1656 * is true. For other values of <code>ch</code>, it is the
1657 * largest value <i>k</i> such that:
1659 * (this.codePointAt(<i>k</i>) == ch) && (<i>k</i> <= fromIndex)
1660 * </pre></blockquote>
1661 * is true. In either case, if no such character occurs in this
1662 * string at or before position <code>fromIndex</code>, then
1663 * <code>-1</code> is returned.
1665 * <p>All indices are specified in <code>char</code> values
1666 * (Unicode code units).
1668 * @param ch a character (Unicode code point).
1669 * @param fromIndex the index to start the search from. There is no
1670 * restriction on the value of <code>fromIndex</code>. If it is
1671 * greater than or equal to the length of this string, it has
1672 * the same effect as if it were equal to one less than the
1673 * length of this string: this entire string may be searched.
1674 * If it is negative, it has the same effect as if it were -1:
1676 * @return the index of the last occurrence of the character in the
1677 * character sequence represented by this object that is less
1678 * than or equal to <code>fromIndex</code>, or <code>-1</code>
1679 * if the character does not occur before that point.
1681 public int lastIndexOf(int ch, int fromIndex) {
1682 if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) {
1683 // handle most cases here (ch is a BMP code point or a
1684 // negative value (invalid code point))
1685 final char[] value = this.value;
1686 final int offset = this.offset;
1687 int i = offset + Math.min(fromIndex, count - 1);
1688 for (; i >= offset ; i--) {
1689 if (value[i] == ch) {
1695 return lastIndexOfSupplementary(ch, fromIndex);
1700 * Handles (rare) calls of lastIndexOf with a supplementary character.
1702 private int lastIndexOfSupplementary(int ch, int fromIndex) {
1703 if (Character.isValidCodePoint(ch)) {
1704 final char[] value = this.value;
1705 final int offset = this.offset;
1706 char hi = Character.highSurrogate(ch);
1707 char lo = Character.lowSurrogate(ch);
1708 int i = offset + Math.min(fromIndex, count - 2);
1709 for (; i >= offset; i--) {
1710 if (value[i] == hi && value[i+1] == lo) {
1719 * Returns the index within this string of the first occurrence of the
1720 * specified substring.
1722 * <p>The returned index is the smallest value <i>k</i> for which:
1724 * this.startsWith(str, <i>k</i>)
1725 * </pre></blockquote>
1726 * If no such value of <i>k</i> exists, then {@code -1} is returned.
1728 * @param str the substring to search for.
1729 * @return the index of the first occurrence of the specified substring,
1730 * or {@code -1} if there is no such occurrence.
1732 public int indexOf(String str) {
1733 return indexOf(str, 0);
1737 * Returns the index within this string of the first occurrence of the
1738 * specified substring, starting at the specified index.
1740 * <p>The returned index is the smallest value <i>k</i> for which:
1742 * <i>k</i> >= fromIndex && this.startsWith(str, <i>k</i>)
1743 * </pre></blockquote>
1744 * If no such value of <i>k</i> exists, then {@code -1} is returned.
1746 * @param str the substring to search for.
1747 * @param fromIndex the index from which to start the search.
1748 * @return the index of the first occurrence of the specified substring,
1749 * starting at the specified index,
1750 * or {@code -1} if there is no such occurrence.
1752 public int indexOf(String str, int fromIndex) {
1753 return indexOf(value, offset, count,
1754 str.value, str.offset, str.count, fromIndex);
1758 * Code shared by String and StringBuffer to do searches. The
1759 * source is the character array being searched, and the target
1760 * is the string being searched for.
1762 * @param source the characters being searched.
1763 * @param sourceOffset offset of the source string.
1764 * @param sourceCount count of the source string.
1765 * @param target the characters being searched for.
1766 * @param targetOffset offset of the target string.
1767 * @param targetCount count of the target string.
1768 * @param fromIndex the index to begin searching from.
1770 static int indexOf(char[] source, int sourceOffset, int sourceCount,
1771 char[] target, int targetOffset, int targetCount,
1773 if (fromIndex >= sourceCount) {
1774 return (targetCount == 0 ? sourceCount : -1);
1776 if (fromIndex < 0) {
1779 if (targetCount == 0) {
1783 char first = target[targetOffset];
1784 int max = sourceOffset + (sourceCount - targetCount);
1786 for (int i = sourceOffset + fromIndex; i <= max; i++) {
1787 /* Look for first character. */
1788 if (source[i] != first) {
1789 while (++i <= max && source[i] != first);
1792 /* Found first character, now look at the rest of v2 */
1795 int end = j + targetCount - 1;
1796 for (int k = targetOffset + 1; j < end && source[j] ==
1797 target[k]; j++, k++);
1800 /* Found whole string. */
1801 return i - sourceOffset;
1809 * Returns the index within this string of the last occurrence of the
1810 * specified substring. The last occurrence of the empty string ""
1811 * is considered to occur at the index value {@code this.length()}.
1813 * <p>The returned index is the largest value <i>k</i> for which:
1815 * this.startsWith(str, <i>k</i>)
1816 * </pre></blockquote>
1817 * If no such value of <i>k</i> exists, then {@code -1} is returned.
1819 * @param str the substring to search for.
1820 * @return the index of the last occurrence of the specified substring,
1821 * or {@code -1} if there is no such occurrence.
1823 public int lastIndexOf(String str) {
1824 return lastIndexOf(str, count);
1828 * Returns the index within this string of the last occurrence of the
1829 * specified substring, searching backward starting at the specified index.
1831 * <p>The returned index is the largest value <i>k</i> for which:
1833 * <i>k</i> <= fromIndex && this.startsWith(str, <i>k</i>)
1834 * </pre></blockquote>
1835 * If no such value of <i>k</i> exists, then {@code -1} is returned.
1837 * @param str the substring to search for.
1838 * @param fromIndex the index to start the search from.
1839 * @return the index of the last occurrence of the specified substring,
1840 * searching backward from the specified index,
1841 * or {@code -1} if there is no such occurrence.
1843 public int lastIndexOf(String str, int fromIndex) {
1844 return lastIndexOf(value, offset, count,
1845 str.value, str.offset, str.count, fromIndex);
1849 * Code shared by String and StringBuffer to do searches. The
1850 * source is the character array being searched, and the target
1851 * is the string being searched for.
1853 * @param source the characters being searched.
1854 * @param sourceOffset offset of the source string.
1855 * @param sourceCount count of the source string.
1856 * @param target the characters being searched for.
1857 * @param targetOffset offset of the target string.
1858 * @param targetCount count of the target string.
1859 * @param fromIndex the index to begin searching from.
1861 static int lastIndexOf(char[] source, int sourceOffset, int sourceCount,
1862 char[] target, int targetOffset, int targetCount,
1865 * Check arguments; return immediately where possible. For
1866 * consistency, don't check for null str.
1868 int rightIndex = sourceCount - targetCount;
1869 if (fromIndex < 0) {
1872 if (fromIndex > rightIndex) {
1873 fromIndex = rightIndex;
1875 /* Empty string always matches. */
1876 if (targetCount == 0) {
1880 int strLastIndex = targetOffset + targetCount - 1;
1881 char strLastChar = target[strLastIndex];
1882 int min = sourceOffset + targetCount - 1;
1883 int i = min + fromIndex;
1885 startSearchForLastChar:
1887 while (i >= min && source[i] != strLastChar) {
1894 int start = j - (targetCount - 1);
1895 int k = strLastIndex - 1;
1898 if (source[j--] != target[k--]) {
1900 continue startSearchForLastChar;
1903 return start - sourceOffset + 1;
1908 * Returns a new string that is a substring of this string. The
1909 * substring begins with the character at the specified index and
1910 * extends to the end of this string. <p>
1913 * "unhappy".substring(2) returns "happy"
1914 * "Harbison".substring(3) returns "bison"
1915 * "emptiness".substring(9) returns "" (an empty string)
1916 * </pre></blockquote>
1918 * @param beginIndex the beginning index, inclusive.
1919 * @return the specified substring.
1920 * @exception IndexOutOfBoundsException if
1921 * <code>beginIndex</code> is negative or larger than the
1922 * length of this <code>String</code> object.
1924 public String substring(int beginIndex) {
1925 return substring(beginIndex, count);
1929 * Returns a new string that is a substring of this string. The
1930 * substring begins at the specified <code>beginIndex</code> and
1931 * extends to the character at index <code>endIndex - 1</code>.
1932 * Thus the length of the substring is <code>endIndex-beginIndex</code>.
1936 * "hamburger".substring(4, 8) returns "urge"
1937 * "smiles".substring(1, 5) returns "mile"
1938 * </pre></blockquote>
1940 * @param beginIndex the beginning index, inclusive.
1941 * @param endIndex the ending index, exclusive.
1942 * @return the specified substring.
1943 * @exception IndexOutOfBoundsException if the
1944 * <code>beginIndex</code> is negative, or
1945 * <code>endIndex</code> is larger than the length of
1946 * this <code>String</code> object, or
1947 * <code>beginIndex</code> is larger than
1948 * <code>endIndex</code>.
1950 public String substring(int beginIndex, int endIndex) {
1951 if (beginIndex < 0) {
1952 throw new StringIndexOutOfBoundsException(beginIndex);
1954 if (endIndex > count) {
1955 throw new StringIndexOutOfBoundsException(endIndex);
1957 if (beginIndex > endIndex) {
1958 throw new StringIndexOutOfBoundsException(endIndex - beginIndex);
1960 return ((beginIndex == 0) && (endIndex == count)) ? this :
1961 new String(value, offset + beginIndex, endIndex - beginIndex);
1965 * Returns a new character sequence that is a subsequence of this sequence.
1967 * <p> An invocation of this method of the form
1970 * str.subSequence(begin, end)</pre></blockquote>
1972 * behaves in exactly the same way as the invocation
1975 * str.substring(begin, end)</pre></blockquote>
1977 * This method is defined so that the <tt>String</tt> class can implement
1978 * the {@link CharSequence} interface. </p>
1980 * @param beginIndex the begin index, inclusive.
1981 * @param endIndex the end index, exclusive.
1982 * @return the specified subsequence.
1984 * @throws IndexOutOfBoundsException
1985 * if <tt>beginIndex</tt> or <tt>endIndex</tt> are negative,
1986 * if <tt>endIndex</tt> is greater than <tt>length()</tt>,
1987 * or if <tt>beginIndex</tt> is greater than <tt>startIndex</tt>
1992 public CharSequence subSequence(int beginIndex, int endIndex) {
1993 return this.substring(beginIndex, endIndex);
1997 * Concatenates the specified string to the end of this string.
1999 * If the length of the argument string is <code>0</code>, then this
2000 * <code>String</code> object is returned. Otherwise, a new
2001 * <code>String</code> object is created, representing a character
2002 * sequence that is the concatenation of the character sequence
2003 * represented by this <code>String</code> object and the character
2004 * sequence represented by the argument string.<p>
2007 * "cares".concat("s") returns "caress"
2008 * "to".concat("get").concat("her") returns "together"
2009 * </pre></blockquote>
2011 * @param str the <code>String</code> that is concatenated to the end
2012 * of this <code>String</code>.
2013 * @return a string that represents the concatenation of this object's
2014 * characters followed by the string argument's characters.
2016 public String concat(String str) {
2017 int otherLen = str.length();
2018 if (otherLen == 0) {
2021 char buf[] = new char[count + otherLen];
2022 getChars(0, count, buf, 0);
2023 str.getChars(0, otherLen, buf, count);
2024 return new String(buf, 0, count + otherLen);
2028 * Returns a new string resulting from replacing all occurrences of
2029 * <code>oldChar</code> in this string with <code>newChar</code>.
2031 * If the character <code>oldChar</code> does not occur in the
2032 * character sequence represented by this <code>String</code> object,
2033 * then a reference to this <code>String</code> object is returned.
2034 * Otherwise, a new <code>String</code> object is created that
2035 * represents a character sequence identical to the character sequence
2036 * represented by this <code>String</code> object, except that every
2037 * occurrence of <code>oldChar</code> is replaced by an occurrence
2038 * of <code>newChar</code>.
2042 * "mesquite in your cellar".replace('e', 'o')
2043 * returns "mosquito in your collar"
2044 * "the war of baronets".replace('r', 'y')
2045 * returns "the way of bayonets"
2046 * "sparring with a purple porpoise".replace('p', 't')
2047 * returns "starring with a turtle tortoise"
2048 * "JonL".replace('q', 'x') returns "JonL" (no change)
2049 * </pre></blockquote>
2051 * @param oldChar the old character.
2052 * @param newChar the new character.
2053 * @return a string derived from this string by replacing every
2054 * occurrence of <code>oldChar</code> with <code>newChar</code>.
2056 public String replace(char oldChar, char newChar) {
2057 if (oldChar != newChar) {
2060 char[] val = value; /* avoid getfield opcode */
2061 int off = offset; /* avoid getfield opcode */
2064 if (val[off + i] == oldChar) {
2069 char buf[] = new char[len];
2070 for (int j = 0 ; j < i ; j++) {
2071 buf[j] = val[off+j];
2074 char c = val[off + i];
2075 buf[i] = (c == oldChar) ? newChar : c;
2078 return new String(buf, 0, len);
2085 * Tells whether or not this string matches the given <a
2086 * href="../util/regex/Pattern.html#sum">regular expression</a>.
2088 * <p> An invocation of this method of the form
2089 * <i>str</i><tt>.matches(</tt><i>regex</i><tt>)</tt> yields exactly the
2090 * same result as the expression
2092 * <blockquote><tt> {@link java.util.regex.Pattern}.{@link
2093 * java.util.regex.Pattern#matches(String,CharSequence)
2094 * matches}(</tt><i>regex</i><tt>,</tt> <i>str</i><tt>)</tt></blockquote>
2097 * the regular expression to which this string is to be matched
2099 * @return <tt>true</tt> if, and only if, this string matches the
2100 * given regular expression
2102 * @throws PatternSyntaxException
2103 * if the regular expression's syntax is invalid
2105 * @see java.util.regex.Pattern
2110 public boolean matches(String regex) {
2111 throw new UnsupportedOperationException();
2115 * Returns true if and only if this string contains the specified
2116 * sequence of char values.
2118 * @param s the sequence to search for
2119 * @return true if this string contains <code>s</code>, false otherwise
2120 * @throws NullPointerException if <code>s</code> is <code>null</code>
2123 public boolean contains(CharSequence s) {
2124 return indexOf(s.toString()) > -1;
2128 * Replaces the first substring of this string that matches the given <a
2129 * href="../util/regex/Pattern.html#sum">regular expression</a> with the
2130 * given replacement.
2132 * <p> An invocation of this method of the form
2133 * <i>str</i><tt>.replaceFirst(</tt><i>regex</i><tt>,</tt> <i>repl</i><tt>)</tt>
2134 * yields exactly the same result as the expression
2137 * {@link java.util.regex.Pattern}.{@link java.util.regex.Pattern#compile
2138 * compile}(</tt><i>regex</i><tt>).{@link
2139 * java.util.regex.Pattern#matcher(java.lang.CharSequence)
2140 * matcher}(</tt><i>str</i><tt>).{@link java.util.regex.Matcher#replaceFirst
2141 * replaceFirst}(</tt><i>repl</i><tt>)</tt></blockquote>
2144 * Note that backslashes (<tt>\</tt>) and dollar signs (<tt>$</tt>) in the
2145 * replacement string may cause the results to be different than if it were
2146 * being treated as a literal replacement string; see
2147 * {@link java.util.regex.Matcher#replaceFirst}.
2148 * Use {@link java.util.regex.Matcher#quoteReplacement} to suppress the special
2149 * meaning of these characters, if desired.
2152 * the regular expression to which this string is to be matched
2153 * @param replacement
2154 * the string to be substituted for the first match
2156 * @return The resulting <tt>String</tt>
2158 * @throws PatternSyntaxException
2159 * if the regular expression's syntax is invalid
2161 * @see java.util.regex.Pattern
2166 public String replaceFirst(String regex, String replacement) {
2167 throw new UnsupportedOperationException();
2171 * Replaces each substring of this string that matches the given <a
2172 * href="../util/regex/Pattern.html#sum">regular expression</a> with the
2173 * given replacement.
2175 * <p> An invocation of this method of the form
2176 * <i>str</i><tt>.replaceAll(</tt><i>regex</i><tt>,</tt> <i>repl</i><tt>)</tt>
2177 * yields exactly the same result as the expression
2180 * {@link java.util.regex.Pattern}.{@link java.util.regex.Pattern#compile
2181 * compile}(</tt><i>regex</i><tt>).{@link
2182 * java.util.regex.Pattern#matcher(java.lang.CharSequence)
2183 * matcher}(</tt><i>str</i><tt>).{@link java.util.regex.Matcher#replaceAll
2184 * replaceAll}(</tt><i>repl</i><tt>)</tt></blockquote>
2187 * Note that backslashes (<tt>\</tt>) and dollar signs (<tt>$</tt>) in the
2188 * replacement string may cause the results to be different than if it were
2189 * being treated as a literal replacement string; see
2190 * {@link java.util.regex.Matcher#replaceAll Matcher.replaceAll}.
2191 * Use {@link java.util.regex.Matcher#quoteReplacement} to suppress the special
2192 * meaning of these characters, if desired.
2195 * the regular expression to which this string is to be matched
2196 * @param replacement
2197 * the string to be substituted for each match
2199 * @return The resulting <tt>String</tt>
2201 * @throws PatternSyntaxException
2202 * if the regular expression's syntax is invalid
2204 * @see java.util.regex.Pattern
2209 public String replaceAll(String regex, String replacement) {
2210 throw new UnsupportedOperationException();
2214 * Replaces each substring of this string that matches the literal target
2215 * sequence with the specified literal replacement sequence. The
2216 * replacement proceeds from the beginning of the string to the end, for
2217 * example, replacing "aa" with "b" in the string "aaa" will result in
2218 * "ba" rather than "ab".
2220 * @param target The sequence of char values to be replaced
2221 * @param replacement The replacement sequence of char values
2222 * @return The resulting string
2223 * @throws NullPointerException if <code>target</code> or
2224 * <code>replacement</code> is <code>null</code>.
2227 public String replace(CharSequence target, CharSequence replacement) {
2228 throw new UnsupportedOperationException("This one should be supported, but without dep on rest of regexp");
2232 * Splits this string around matches of the given
2233 * <a href="../util/regex/Pattern.html#sum">regular expression</a>.
2235 * <p> The array returned by this method contains each substring of this
2236 * string that is terminated by another substring that matches the given
2237 * expression or is terminated by the end of the string. The substrings in
2238 * the array are in the order in which they occur in this string. If the
2239 * expression does not match any part of the input then the resulting array
2240 * has just one element, namely this string.
2242 * <p> The <tt>limit</tt> parameter controls the number of times the
2243 * pattern is applied and therefore affects the length of the resulting
2244 * array. If the limit <i>n</i> is greater than zero then the pattern
2245 * will be applied at most <i>n</i> - 1 times, the array's
2246 * length will be no greater than <i>n</i>, and the array's last entry
2247 * will contain all input beyond the last matched delimiter. If <i>n</i>
2248 * is non-positive then the pattern will be applied as many times as
2249 * possible and the array can have any length. If <i>n</i> is zero then
2250 * the pattern will be applied as many times as possible, the array can
2251 * have any length, and trailing empty strings will be discarded.
2253 * <p> The string <tt>"boo:and:foo"</tt>, for example, yields the
2254 * following results with these parameters:
2256 * <blockquote><table cellpadding=1 cellspacing=0 summary="Split example showing regex, limit, and result">
2262 * <tr><td align=center>:</td>
2263 * <td align=center>2</td>
2264 * <td><tt>{ "boo", "and:foo" }</tt></td></tr>
2265 * <tr><td align=center>:</td>
2266 * <td align=center>5</td>
2267 * <td><tt>{ "boo", "and", "foo" }</tt></td></tr>
2268 * <tr><td align=center>:</td>
2269 * <td align=center>-2</td>
2270 * <td><tt>{ "boo", "and", "foo" }</tt></td></tr>
2271 * <tr><td align=center>o</td>
2272 * <td align=center>5</td>
2273 * <td><tt>{ "b", "", ":and:f", "", "" }</tt></td></tr>
2274 * <tr><td align=center>o</td>
2275 * <td align=center>-2</td>
2276 * <td><tt>{ "b", "", ":and:f", "", "" }</tt></td></tr>
2277 * <tr><td align=center>o</td>
2278 * <td align=center>0</td>
2279 * <td><tt>{ "b", "", ":and:f" }</tt></td></tr>
2280 * </table></blockquote>
2282 * <p> An invocation of this method of the form
2283 * <i>str.</i><tt>split(</tt><i>regex</i><tt>,</tt> <i>n</i><tt>)</tt>
2284 * yields the same result as the expression
2287 * {@link java.util.regex.Pattern}.{@link java.util.regex.Pattern#compile
2288 * compile}<tt>(</tt><i>regex</i><tt>)</tt>.{@link
2289 * java.util.regex.Pattern#split(java.lang.CharSequence,int)
2290 * split}<tt>(</tt><i>str</i><tt>,</tt> <i>n</i><tt>)</tt>
2295 * the delimiting regular expression
2298 * the result threshold, as described above
2300 * @return the array of strings computed by splitting this string
2301 * around matches of the given regular expression
2303 * @throws PatternSyntaxException
2304 * if the regular expression's syntax is invalid
2306 * @see java.util.regex.Pattern
2311 public String[] split(String regex, int limit) {
2312 throw new UnsupportedOperationException("Needs regexp");
2316 * Splits this string around matches of the given <a
2317 * href="../util/regex/Pattern.html#sum">regular expression</a>.
2319 * <p> This method works as if by invoking the two-argument {@link
2320 * #split(String, int) split} method with the given expression and a limit
2321 * argument of zero. Trailing empty strings are therefore not included in
2322 * the resulting array.
2324 * <p> The string <tt>"boo:and:foo"</tt>, for example, yields the following
2325 * results with these expressions:
2327 * <blockquote><table cellpadding=1 cellspacing=0 summary="Split examples showing regex and result">
2332 * <tr><td align=center>:</td>
2333 * <td><tt>{ "boo", "and", "foo" }</tt></td></tr>
2334 * <tr><td align=center>o</td>
2335 * <td><tt>{ "b", "", ":and:f" }</tt></td></tr>
2336 * </table></blockquote>
2340 * the delimiting regular expression
2342 * @return the array of strings computed by splitting this string
2343 * around matches of the given regular expression
2345 * @throws PatternSyntaxException
2346 * if the regular expression's syntax is invalid
2348 * @see java.util.regex.Pattern
2353 public String[] split(String regex) {
2354 return split(regex, 0);
2358 * Converts all of the characters in this <code>String</code> to lower
2359 * case using the rules of the given <code>Locale</code>. Case mapping is based
2360 * on the Unicode Standard version specified by the {@link java.lang.Character Character}
2361 * class. Since case mappings are not always 1:1 char mappings, the resulting
2362 * <code>String</code> may be a different length than the original <code>String</code>.
2364 * Examples of lowercase mappings are in the following table:
2365 * <table border="1" summary="Lowercase mapping examples showing language code of locale, upper case, lower case, and description">
2367 * <th>Language Code of Locale</th>
2368 * <th>Upper Case</th>
2369 * <th>Lower Case</th>
2370 * <th>Description</th>
2373 * <td>tr (Turkish)</td>
2374 * <td>\u0130</td>
2375 * <td>\u0069</td>
2376 * <td>capital letter I with dot above -> small letter i</td>
2379 * <td>tr (Turkish)</td>
2380 * <td>\u0049</td>
2381 * <td>\u0131</td>
2382 * <td>capital letter I -> small letter dotless i </td>
2386 * <td>French Fries</td>
2387 * <td>french fries</td>
2388 * <td>lowercased all chars in String</td>
2392 * <td><img src="doc-files/capiota.gif" alt="capiota"><img src="doc-files/capchi.gif" alt="capchi">
2393 * <img src="doc-files/captheta.gif" alt="captheta"><img src="doc-files/capupsil.gif" alt="capupsil">
2394 * <img src="doc-files/capsigma.gif" alt="capsigma"></td>
2395 * <td><img src="doc-files/iota.gif" alt="iota"><img src="doc-files/chi.gif" alt="chi">
2396 * <img src="doc-files/theta.gif" alt="theta"><img src="doc-files/upsilon.gif" alt="upsilon">
2397 * <img src="doc-files/sigma1.gif" alt="sigma"></td>
2398 * <td>lowercased all chars in String</td>
2402 * @param locale use the case transformation rules for this locale
2403 * @return the <code>String</code>, converted to lowercase.
2404 * @see java.lang.String#toLowerCase()
2405 * @see java.lang.String#toUpperCase()
2406 * @see java.lang.String#toUpperCase(Locale)
2409 // public String toLowerCase(Locale locale) {
2410 // if (locale == null) {
2411 // throw new NullPointerException();
2416 // /* Now check if there are any characters that need to be changed. */
2418 // for (firstUpper = 0 ; firstUpper < count; ) {
2419 // char c = value[offset+firstUpper];
2420 // if ((c >= Character.MIN_HIGH_SURROGATE) &&
2421 // (c <= Character.MAX_HIGH_SURROGATE)) {
2422 // int supplChar = codePointAt(firstUpper);
2423 // if (supplChar != Character.toLowerCase(supplChar)) {
2426 // firstUpper += Character.charCount(supplChar);
2428 // if (c != Character.toLowerCase(c)) {
2437 // char[] result = new char[count];
2438 // int resultOffset = 0; /* result may grow, so i+resultOffset
2439 // * is the write location in result */
2441 // /* Just copy the first few lowerCase characters. */
2442 // arraycopy(value, offset, result, 0, firstUpper);
2444 // String lang = locale.getLanguage();
2445 // boolean localeDependent =
2446 // (lang == "tr" || lang == "az" || lang == "lt");
2447 // char[] lowerCharArray;
2451 // for (int i = firstUpper; i < count; i += srcCount) {
2452 // srcChar = (int)value[offset+i];
2453 // if ((char)srcChar >= Character.MIN_HIGH_SURROGATE &&
2454 // (char)srcChar <= Character.MAX_HIGH_SURROGATE) {
2455 // srcChar = codePointAt(i);
2456 // srcCount = Character.charCount(srcChar);
2460 // if (localeDependent || srcChar == '\u03A3') { // GREEK CAPITAL LETTER SIGMA
2461 // lowerChar = ConditionalSpecialCasing.toLowerCaseEx(this, i, locale);
2462 // } else if (srcChar == '\u0130') { // LATIN CAPITAL LETTER I DOT
2463 // lowerChar = Character.ERROR;
2465 // lowerChar = Character.toLowerCase(srcChar);
2467 // if ((lowerChar == Character.ERROR) ||
2468 // (lowerChar >= Character.MIN_SUPPLEMENTARY_CODE_POINT)) {
2469 // if (lowerChar == Character.ERROR) {
2470 // if (!localeDependent && srcChar == '\u0130') {
2472 // ConditionalSpecialCasing.toLowerCaseCharArray(this, i, Locale.ENGLISH);
2475 // ConditionalSpecialCasing.toLowerCaseCharArray(this, i, locale);
2477 // } else if (srcCount == 2) {
2478 // resultOffset += Character.toChars(lowerChar, result, i + resultOffset) - srcCount;
2481 // lowerCharArray = Character.toChars(lowerChar);
2484 // /* Grow result if needed */
2485 // int mapLen = lowerCharArray.length;
2486 // if (mapLen > srcCount) {
2487 // char[] result2 = new char[result.length + mapLen - srcCount];
2488 // arraycopy(result, 0, result2, 0,
2489 // i + resultOffset);
2490 // result = result2;
2492 // for (int x=0; x<mapLen; ++x) {
2493 // result[i+resultOffset+x] = lowerCharArray[x];
2495 // resultOffset += (mapLen - srcCount);
2497 // result[i+resultOffset] = (char)lowerChar;
2500 // return new String(0, count+resultOffset, result);
2504 * Converts all of the characters in this <code>String</code> to lower
2505 * case using the rules of the default locale. This is equivalent to calling
2506 * <code>toLowerCase(Locale.getDefault())</code>.
2508 * <b>Note:</b> This method is locale sensitive, and may produce unexpected
2509 * results if used for strings that are intended to be interpreted locale
2511 * Examples are programming language identifiers, protocol keys, and HTML
2513 * For instance, <code>"TITLE".toLowerCase()</code> in a Turkish locale
2514 * returns <code>"t\u005Cu0131tle"</code>, where '\u005Cu0131' is the
2515 * LATIN SMALL LETTER DOTLESS I character.
2516 * To obtain correct results for locale insensitive strings, use
2517 * <code>toLowerCase(Locale.ENGLISH)</code>.
2519 * @return the <code>String</code>, converted to lowercase.
2520 * @see java.lang.String#toLowerCase(Locale)
2522 public String toLowerCase() {
2523 throw new UnsupportedOperationException("Should be supported but without connection to locale");
2527 * Converts all of the characters in this <code>String</code> to upper
2528 * case using the rules of the given <code>Locale</code>. Case mapping is based
2529 * on the Unicode Standard version specified by the {@link java.lang.Character Character}
2530 * class. Since case mappings are not always 1:1 char mappings, the resulting
2531 * <code>String</code> may be a different length than the original <code>String</code>.
2533 * Examples of locale-sensitive and 1:M case mappings are in the following table.
2535 * <table border="1" summary="Examples of locale-sensitive and 1:M case mappings. Shows Language code of locale, lower case, upper case, and description.">
2537 * <th>Language Code of Locale</th>
2538 * <th>Lower Case</th>
2539 * <th>Upper Case</th>
2540 * <th>Description</th>
2543 * <td>tr (Turkish)</td>
2544 * <td>\u0069</td>
2545 * <td>\u0130</td>
2546 * <td>small letter i -> capital letter I with dot above</td>
2549 * <td>tr (Turkish)</td>
2550 * <td>\u0131</td>
2551 * <td>\u0049</td>
2552 * <td>small letter dotless i -> capital letter I</td>
2556 * <td>\u00df</td>
2557 * <td>\u0053 \u0053</td>
2558 * <td>small letter sharp s -> two letters: SS</td>
2562 * <td>Fahrvergnügen</td>
2563 * <td>FAHRVERGNÜGEN</td>
2567 * @param locale use the case transformation rules for this locale
2568 * @return the <code>String</code>, converted to uppercase.
2569 * @see java.lang.String#toUpperCase()
2570 * @see java.lang.String#toLowerCase()
2571 * @see java.lang.String#toLowerCase(Locale)
2574 /* not for javascript
2575 public String toUpperCase(Locale locale) {
2576 if (locale == null) {
2577 throw new NullPointerException();
2582 // Now check if there are any characters that need to be changed.
2584 for (firstLower = 0 ; firstLower < count; ) {
2585 int c = (int)value[offset+firstLower];
2587 if ((c >= Character.MIN_HIGH_SURROGATE) &&
2588 (c <= Character.MAX_HIGH_SURROGATE)) {
2589 c = codePointAt(firstLower);
2590 srcCount = Character.charCount(c);
2594 int upperCaseChar = Character.toUpperCaseEx(c);
2595 if ((upperCaseChar == Character.ERROR) ||
2596 (c != upperCaseChar)) {
2599 firstLower += srcCount;
2604 char[] result = new char[count]; /* may grow *
2605 int resultOffset = 0; /* result may grow, so i+resultOffset
2606 * is the write location in result *
2608 /* Just copy the first few upperCase characters. *
2609 arraycopy(value, offset, result, 0, firstLower);
2611 String lang = locale.getLanguage();
2612 boolean localeDependent =
2613 (lang == "tr" || lang == "az" || lang == "lt");
2614 char[] upperCharArray;
2618 for (int i = firstLower; i < count; i += srcCount) {
2619 srcChar = (int)value[offset+i];
2620 if ((char)srcChar >= Character.MIN_HIGH_SURROGATE &&
2621 (char)srcChar <= Character.MAX_HIGH_SURROGATE) {
2622 srcChar = codePointAt(i);
2623 srcCount = Character.charCount(srcChar);
2627 if (localeDependent) {
2628 upperChar = ConditionalSpecialCasing.toUpperCaseEx(this, i, locale);
2630 upperChar = Character.toUpperCaseEx(srcChar);
2632 if ((upperChar == Character.ERROR) ||
2633 (upperChar >= Character.MIN_SUPPLEMENTARY_CODE_POINT)) {
2634 if (upperChar == Character.ERROR) {
2635 if (localeDependent) {
2637 ConditionalSpecialCasing.toUpperCaseCharArray(this, i, locale);
2639 upperCharArray = Character.toUpperCaseCharArray(srcChar);
2641 } else if (srcCount == 2) {
2642 resultOffset += Character.toChars(upperChar, result, i + resultOffset) - srcCount;
2645 upperCharArray = Character.toChars(upperChar);
2648 /* Grow result if needed *
2649 int mapLen = upperCharArray.length;
2650 if (mapLen > srcCount) {
2651 char[] result2 = new char[result.length + mapLen - srcCount];
2652 arraycopy(result, 0, result2, 0,
2656 for (int x=0; x<mapLen; ++x) {
2657 result[i+resultOffset+x] = upperCharArray[x];
2659 resultOffset += (mapLen - srcCount);
2661 result[i+resultOffset] = (char)upperChar;
2664 return new String(0, count+resultOffset, result);
2669 * Converts all of the characters in this <code>String</code> to upper
2670 * case using the rules of the default locale. This method is equivalent to
2671 * <code>toUpperCase(Locale.getDefault())</code>.
2673 * <b>Note:</b> This method is locale sensitive, and may produce unexpected
2674 * results if used for strings that are intended to be interpreted locale
2676 * Examples are programming language identifiers, protocol keys, and HTML
2678 * For instance, <code>"title".toUpperCase()</code> in a Turkish locale
2679 * returns <code>"T\u005Cu0130TLE"</code>, where '\u005Cu0130' is the
2680 * LATIN CAPITAL LETTER I WITH DOT ABOVE character.
2681 * To obtain correct results for locale insensitive strings, use
2682 * <code>toUpperCase(Locale.ENGLISH)</code>.
2684 * @return the <code>String</code>, converted to uppercase.
2685 * @see java.lang.String#toUpperCase(Locale)
2687 public String toUpperCase() {
2688 throw new UnsupportedOperationException();
2692 * Returns a copy of the string, with leading and trailing whitespace
2695 * If this <code>String</code> object represents an empty character
2696 * sequence, or the first and last characters of character sequence
2697 * represented by this <code>String</code> object both have codes
2698 * greater than <code>'\u0020'</code> (the space character), then a
2699 * reference to this <code>String</code> object is returned.
2701 * Otherwise, if there is no character with a code greater than
2702 * <code>'\u0020'</code> in the string, then a new
2703 * <code>String</code> object representing an empty string is created
2706 * Otherwise, let <i>k</i> be the index of the first character in the
2707 * string whose code is greater than <code>'\u0020'</code>, and let
2708 * <i>m</i> be the index of the last character in the string whose code
2709 * is greater than <code>'\u0020'</code>. A new <code>String</code>
2710 * object is created, representing the substring of this string that
2711 * begins with the character at index <i>k</i> and ends with the
2712 * character at index <i>m</i>-that is, the result of
2713 * <code>this.substring(<i>k</i>, <i>m</i>+1)</code>.
2715 * This method may be used to trim whitespace (as defined above) from
2716 * the beginning and end of a string.
2718 * @return A copy of this string with leading and trailing white
2719 * space removed, or this string if it has no leading or
2720 * trailing white space.
2722 public String trim() {
2725 int off = offset; /* avoid getfield opcode */
2726 char[] val = value; /* avoid getfield opcode */
2728 while ((st < len) && (val[off + st] <= ' ')) {
2731 while ((st < len) && (val[off + len - 1] <= ' ')) {
2734 return ((st > 0) || (len < count)) ? substring(st, len) : this;
2738 * This object (which is already a string!) is itself returned.
2740 * @return the string itself.
2742 public String toString() {
2747 * Converts this string to a new character array.
2749 * @return a newly allocated character array whose length is the length
2750 * of this string and whose contents are initialized to contain
2751 * the character sequence represented by this string.
2753 public char[] toCharArray() {
2754 char result[] = new char[count];
2755 getChars(0, count, result, 0);
2760 * Returns a formatted string using the specified format string and
2763 * <p> The locale always used is the one returned by {@link
2764 * java.util.Locale#getDefault() Locale.getDefault()}.
2767 * A <a href="../util/Formatter.html#syntax">format string</a>
2770 * Arguments referenced by the format specifiers in the format
2771 * string. If there are more arguments than format specifiers, the
2772 * extra arguments are ignored. The number of arguments is
2773 * variable and may be zero. The maximum number of arguments is
2774 * limited by the maximum dimension of a Java array as defined by
2775 * <cite>The Java™ Virtual Machine Specification</cite>.
2776 * The behaviour on a
2777 * <tt>null</tt> argument depends on the <a
2778 * href="../util/Formatter.html#syntax">conversion</a>.
2780 * @throws IllegalFormatException
2781 * If a format string contains an illegal syntax, a format
2782 * specifier that is incompatible with the given arguments,
2783 * insufficient arguments given the format string, or other
2784 * illegal conditions. For specification of all possible
2785 * formatting errors, see the <a
2786 * href="../util/Formatter.html#detail">Details</a> section of the
2787 * formatter class specification.
2789 * @throws NullPointerException
2790 * If the <tt>format</tt> is <tt>null</tt>
2792 * @return A formatted string
2794 * @see java.util.Formatter
2797 public static String format(String format, Object ... args) {
2798 throw new UnsupportedOperationException();
2802 * Returns a formatted string using the specified locale, format string,
2806 * The {@linkplain java.util.Locale locale} to apply during
2807 * formatting. If <tt>l</tt> is <tt>null</tt> then no localization
2811 * A <a href="../util/Formatter.html#syntax">format string</a>
2814 * Arguments referenced by the format specifiers in the format
2815 * string. If there are more arguments than format specifiers, the
2816 * extra arguments are ignored. The number of arguments is
2817 * variable and may be zero. The maximum number of arguments is
2818 * limited by the maximum dimension of a Java array as defined by
2819 * <cite>The Java™ Virtual Machine Specification</cite>.
2820 * The behaviour on a
2821 * <tt>null</tt> argument depends on the <a
2822 * href="../util/Formatter.html#syntax">conversion</a>.
2824 * @throws IllegalFormatException
2825 * If a format string contains an illegal syntax, a format
2826 * specifier that is incompatible with the given arguments,
2827 * insufficient arguments given the format string, or other
2828 * illegal conditions. For specification of all possible
2829 * formatting errors, see the <a
2830 * href="../util/Formatter.html#detail">Details</a> section of the
2831 * formatter class specification
2833 * @throws NullPointerException
2834 * If the <tt>format</tt> is <tt>null</tt>
2836 * @return A formatted string
2838 * @see java.util.Formatter
2841 // public static String format(Locale l, String format, Object ... args) {
2842 // return new Formatter(l).format(format, args).toString();
2846 * Returns the string representation of the <code>Object</code> argument.
2848 * @param obj an <code>Object</code>.
2849 * @return if the argument is <code>null</code>, then a string equal to
2850 * <code>"null"</code>; otherwise, the value of
2851 * <code>obj.toString()</code> is returned.
2852 * @see java.lang.Object#toString()
2854 public static String valueOf(Object obj) {
2855 return (obj == null) ? "null" : obj.toString();
2859 * Returns the string representation of the <code>char</code> array
2860 * argument. The contents of the character array are copied; subsequent
2861 * modification of the character array does not affect the newly
2864 * @param data a <code>char</code> array.
2865 * @return a newly allocated string representing the same sequence of
2866 * characters contained in the character array argument.
2868 public static String valueOf(char data[]) {
2869 return new String(data);
2873 * Returns the string representation of a specific subarray of the
2874 * <code>char</code> array argument.
2876 * The <code>offset</code> argument is the index of the first
2877 * character of the subarray. The <code>count</code> argument
2878 * specifies the length of the subarray. The contents of the subarray
2879 * are copied; subsequent modification of the character array does not
2880 * affect the newly created string.
2882 * @param data the character array.
2883 * @param offset the initial offset into the value of the
2884 * <code>String</code>.
2885 * @param count the length of the value of the <code>String</code>.
2886 * @return a string representing the sequence of characters contained
2887 * in the subarray of the character array argument.
2888 * @exception IndexOutOfBoundsException if <code>offset</code> is
2889 * negative, or <code>count</code> is negative, or
2890 * <code>offset+count</code> is larger than
2891 * <code>data.length</code>.
2893 public static String valueOf(char data[], int offset, int count) {
2894 return new String(data, offset, count);
2898 * Returns a String that represents the character sequence in the
2901 * @param data the character array.
2902 * @param offset initial offset of the subarray.
2903 * @param count length of the subarray.
2904 * @return a <code>String</code> that contains the characters of the
2905 * specified subarray of the character array.
2907 public static String copyValueOf(char data[], int offset, int count) {
2908 // All public String constructors now copy the data.
2909 return new String(data, offset, count);
2913 * Returns a String that represents the character sequence in the
2916 * @param data the character array.
2917 * @return a <code>String</code> that contains the characters of the
2920 public static String copyValueOf(char data[]) {
2921 return copyValueOf(data, 0, data.length);
2925 * Returns the string representation of the <code>boolean</code> argument.
2927 * @param b a <code>boolean</code>.
2928 * @return if the argument is <code>true</code>, a string equal to
2929 * <code>"true"</code> is returned; otherwise, a string equal to
2930 * <code>"false"</code> is returned.
2932 public static String valueOf(boolean b) {
2933 return b ? "true" : "false";
2937 * Returns the string representation of the <code>char</code>
2940 * @param c a <code>char</code>.
2941 * @return a string of length <code>1</code> containing
2942 * as its single character the argument <code>c</code>.
2944 public static String valueOf(char c) {
2946 return new String(data, 0, 1);
2950 * Returns the string representation of the <code>int</code> argument.
2952 * The representation is exactly the one returned by the
2953 * <code>Integer.toString</code> method of one argument.
2955 * @param i an <code>int</code>.
2956 * @return a string representation of the <code>int</code> argument.
2957 * @see java.lang.Integer#toString(int, int)
2959 public static String valueOf(int i) {
2960 return Integer.toString(i);
2964 * Returns the string representation of the <code>long</code> argument.
2966 * The representation is exactly the one returned by the
2967 * <code>Long.toString</code> method of one argument.
2969 * @param l a <code>long</code>.
2970 * @return a string representation of the <code>long</code> argument.
2971 * @see java.lang.Long#toString(long)
2973 public static String valueOf(long l) {
2974 return Long.toString(l);
2978 * Returns the string representation of the <code>float</code> argument.
2980 * The representation is exactly the one returned by the
2981 * <code>Float.toString</code> method of one argument.
2983 * @param f a <code>float</code>.
2984 * @return a string representation of the <code>float</code> argument.
2985 * @see java.lang.Float#toString(float)
2987 public static String valueOf(float f) {
2988 return Float.toString(f);
2992 * Returns the string representation of the <code>double</code> argument.
2994 * The representation is exactly the one returned by the
2995 * <code>Double.toString</code> method of one argument.
2997 * @param d a <code>double</code>.
2998 * @return a string representation of the <code>double</code> argument.
2999 * @see java.lang.Double#toString(double)
3001 public static String valueOf(double d) {
3002 return Double.toString(d);
3006 * Returns a canonical representation for the string object.
3008 * A pool of strings, initially empty, is maintained privately by the
3009 * class <code>String</code>.
3011 * When the intern method is invoked, if the pool already contains a
3012 * string equal to this <code>String</code> object as determined by
3013 * the {@link #equals(Object)} method, then the string from the pool is
3014 * returned. Otherwise, this <code>String</code> object is added to the
3015 * pool and a reference to this <code>String</code> object is returned.
3017 * It follows that for any two strings <code>s</code> and <code>t</code>,
3018 * <code>s.intern() == t.intern()</code> is <code>true</code>
3019 * if and only if <code>s.equals(t)</code> is <code>true</code>.
3021 * All literal strings and string-valued constant expressions are
3022 * interned. String literals are defined in section 3.10.5 of the
3023 * <cite>The Java™ Language Specification</cite>.
3025 * @return a string that has the same contents as this string, but is
3026 * guaranteed to be from a pool of unique strings.
3028 public native String intern();