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 java.util.Comparator;
29 import org.apidesign.bck2brwsr.core.ExtraJavaScript;
30 import org.apidesign.bck2brwsr.core.JavaScriptBody;
31 import org.apidesign.bck2brwsr.core.JavaScriptOnly;
32 import org.apidesign.bck2brwsr.core.JavaScriptPrototype;
33 import org.apidesign.bck2brwsr.emul.lang.System;
36 * The <code>String</code> class represents character strings. All
37 * string literals in Java programs, such as <code>"abc"</code>, are
38 * implemented as instances of this class.
40 * Strings are constant; their values cannot be changed after they
41 * are created. String buffers support mutable strings.
42 * Because String objects are immutable they can be shared. For example:
43 * <p><blockquote><pre>
45 * </pre></blockquote><p>
47 * <p><blockquote><pre>
48 * char data[] = {'a', 'b', 'c'};
49 * String str = new String(data);
50 * </pre></blockquote><p>
51 * Here are some more examples of how strings can be used:
52 * <p><blockquote><pre>
53 * System.out.println("abc");
55 * System.out.println("abc" + cde);
56 * String c = "abc".substring(2,3);
57 * String d = cde.substring(1, 2);
60 * The class <code>String</code> includes methods for examining
61 * individual characters of the sequence, for comparing strings, for
62 * searching strings, for extracting substrings, and for creating a
63 * copy of a string with all characters translated to uppercase or to
64 * lowercase. Case mapping is based on the Unicode Standard version
65 * specified by the {@link java.lang.Character Character} class.
67 * The Java language provides special support for the string
68 * concatenation operator ( + ), and for conversion of
69 * other objects to strings. String concatenation is implemented
70 * through the <code>StringBuilder</code>(or <code>StringBuffer</code>)
71 * class and its <code>append</code> method.
72 * String conversions are implemented through the method
73 * <code>toString</code>, defined by <code>Object</code> and
74 * inherited by all classes in Java. For additional information on
75 * string concatenation and conversion, see Gosling, Joy, and Steele,
76 * <i>The Java Language Specification</i>.
78 * <p> Unless otherwise noted, passing a <tt>null</tt> argument to a constructor
79 * or method in this class will cause a {@link NullPointerException} to be
82 * <p>A <code>String</code> represents a string in the UTF-16 format
83 * in which <em>supplementary characters</em> are represented by <em>surrogate
84 * pairs</em> (see the section <a href="Character.html#unicode">Unicode
85 * Character Representations</a> in the <code>Character</code> class for
87 * Index values refer to <code>char</code> code units, so a supplementary
88 * character uses two positions in a <code>String</code>.
89 * <p>The <code>String</code> class provides methods for dealing with
90 * Unicode code points (i.e., characters), in addition to those for
91 * dealing with Unicode code units (i.e., <code>char</code> values).
94 * @author Arthur van Hoff
95 * @author Martin Buchholz
97 * @see java.lang.Object#toString()
98 * @see java.lang.StringBuffer
99 * @see java.lang.StringBuilder
100 * @see java.nio.charset.Charset
105 resource="/org/apidesign/vm4brwsr/emul/lang/java_lang_String.js",
108 @JavaScriptPrototype(container = "String.prototype", prototype = "new String")
109 public final class String
110 implements java.io.Serializable, Comparable<String>, CharSequence
112 /** real string to delegate to */
115 /** use serialVersionUID from JDK 1.0.2 for interoperability */
116 private static final long serialVersionUID = -6849794470754667710L;
118 @JavaScriptOnly(name="toString", value="function() { return this.fld_r; }")
119 private static void jsToString() {
122 @JavaScriptOnly(name="valueOf", value="function() { return this.toString().valueOf(); }")
123 private static void jsValudOf() {
127 * Class String is special cased within the Serialization Stream Protocol.
129 * A String instance is written initially into an ObjectOutputStream in the
132 * <code>TC_STRING</code> (utf String)
134 * The String is written by method <code>DataOutput.writeUTF</code>.
135 * A new handle is generated to refer to all future references to the
136 * string instance within the stream.
138 // private static final ObjectStreamField[] serialPersistentFields =
139 // new ObjectStreamField[0];
142 * Initializes a newly created {@code String} object so that it represents
143 * an empty character sequence. Note that use of this constructor is
144 * unnecessary since Strings are immutable.
151 * Initializes a newly created {@code String} object so that it represents
152 * the same sequence of characters as the argument; in other words, the
153 * newly created string is a copy of the argument string. Unless an
154 * explicit copy of {@code original} is needed, use of this constructor is
155 * unnecessary since Strings are immutable.
160 public String(String original) {
161 this.r = original.toString();
165 * Allocates a new {@code String} so that it represents the sequence of
166 * characters currently contained in the character array argument. The
167 * contents of the character array are copied; subsequent modification of
168 * the character array does not affect the newly created string.
171 * The initial value of the string
173 @JavaScriptBody(args = { "charArr" }, body=
174 "for (var i = 0; i < charArr.length; i++) {\n"
175 + " if (typeof charArr[i] === 'number') charArr[i] = String.fromCharCode(charArr[i]);\n"
177 + "this.fld_r = charArr.join('');\n"
179 public String(char value[]) {
183 * Allocates a new {@code String} that contains characters from a subarray
184 * of the character array argument. The {@code offset} argument is the
185 * index of the first character of the subarray and the {@code count}
186 * argument specifies the length of the subarray. The contents of the
187 * subarray are copied; subsequent modification of the character array does
188 * not affect the newly created string.
191 * Array that is the source of characters
199 * @throws IndexOutOfBoundsException
200 * If the {@code offset} and {@code count} arguments index
201 * characters outside the bounds of the {@code value} array
203 @JavaScriptBody(args = { "charArr", "off", "cnt" }, body =
204 "var up = off + cnt;\n" +
205 "for (var i = off; i < up; i++) {\n" +
206 " if (typeof charArr[i] === 'number') charArr[i] = String.fromCharCode(charArr[i]);\n" +
208 "this.fld_r = charArr.slice(off, up).join(\"\");\n"
210 public String(char value[], int offset, int count) {
214 * Allocates a new {@code String} that contains characters from a subarray
215 * of the <a href="Character.html#unicode">Unicode code point</a> array
216 * argument. The {@code offset} argument is the index of the first code
217 * point of the subarray and the {@code count} argument specifies the
218 * length of the subarray. The contents of the subarray are converted to
219 * {@code char}s; subsequent modification of the {@code int} array does not
220 * affect the newly created string.
223 * Array that is the source of Unicode code points
231 * @throws IllegalArgumentException
232 * If any invalid Unicode code point is found in {@code
235 * @throws IndexOutOfBoundsException
236 * If the {@code offset} and {@code count} arguments index
237 * characters outside the bounds of the {@code codePoints} array
241 public String(int[] codePoints, int offset, int count) {
243 throw new StringIndexOutOfBoundsException(offset);
246 throw new StringIndexOutOfBoundsException(count);
248 // Note: offset or count might be near -1>>>1.
249 if (offset > codePoints.length - count) {
250 throw new StringIndexOutOfBoundsException(offset + count);
253 final int end = offset + count;
255 // Pass 1: Compute precise size of char[]
257 for (int i = offset; i < end; i++) {
258 int c = codePoints[i];
259 if (Character.isBmpCodePoint(c))
261 else if (Character.isValidCodePoint(c))
263 else throw new IllegalArgumentException(Integer.toString(c));
266 // Pass 2: Allocate and fill in char[]
267 final char[] v = new char[n];
269 for (int i = offset, j = 0; i < end; i++, j++) {
270 int c = codePoints[i];
271 if (Character.isBmpCodePoint(c))
274 Character.toSurrogates(c, v, j++);
277 this.r = new String(v, 0, n);
281 * Allocates a new {@code String} constructed from a subarray of an array
282 * of 8-bit integer values.
284 * <p> The {@code offset} argument is the index of the first byte of the
285 * subarray, and the {@code count} argument specifies the length of the
288 * <p> Each {@code byte} in the subarray is converted to a {@code char} as
289 * specified in the method above.
291 * @deprecated This method does not properly convert bytes into characters.
292 * As of JDK 1.1, the preferred way to do this is via the
293 * {@code String} constructors that take a {@link
294 * java.nio.charset.Charset}, charset name, or that use the platform's
298 * The bytes to be converted to characters
301 * The top 8 bits of each 16-bit Unicode code unit
308 * @throws IndexOutOfBoundsException
309 * If the {@code offset} or {@code count} argument is invalid
311 * @see #String(byte[], int)
312 * @see #String(byte[], int, int, java.lang.String)
313 * @see #String(byte[], int, int, java.nio.charset.Charset)
314 * @see #String(byte[], int, int)
315 * @see #String(byte[], java.lang.String)
316 * @see #String(byte[], java.nio.charset.Charset)
317 * @see #String(byte[])
320 public String(byte ascii[], int hibyte, int offset, int count) {
321 checkBounds(ascii, offset, count);
322 char value[] = new char[count];
325 for (int i = count ; i-- > 0 ;) {
326 value[i] = (char) (ascii[i + offset] & 0xff);
330 for (int i = count ; i-- > 0 ;) {
331 value[i] = (char) (hibyte | (ascii[i + offset] & 0xff));
334 this.r = new String(value, 0, count);
338 * Allocates a new {@code String} containing characters constructed from
339 * an array of 8-bit integer values. Each character <i>c</i>in the
340 * resulting string is constructed from the corresponding component
341 * <i>b</i> in the byte array such that:
344 * <b><i>c</i></b> == (char)(((hibyte & 0xff) << 8)
345 * | (<b><i>b</i></b> & 0xff))
346 * </pre></blockquote>
348 * @deprecated This method does not properly convert bytes into
349 * characters. As of JDK 1.1, the preferred way to do this is via the
350 * {@code String} constructors that take a {@link
351 * java.nio.charset.Charset}, charset name, or that use the platform's
355 * The bytes to be converted to characters
358 * The top 8 bits of each 16-bit Unicode code unit
360 * @see #String(byte[], int, int, java.lang.String)
361 * @see #String(byte[], int, int, java.nio.charset.Charset)
362 * @see #String(byte[], int, int)
363 * @see #String(byte[], java.lang.String)
364 * @see #String(byte[], java.nio.charset.Charset)
365 * @see #String(byte[])
368 public String(byte ascii[], int hibyte) {
369 this(ascii, hibyte, 0, ascii.length);
372 /* Common private utility method used to bounds check the byte array
373 * and requested offset & length values used by the String(byte[],..)
376 private static void checkBounds(byte[] bytes, int offset, int length) {
378 throw new StringIndexOutOfBoundsException(length);
380 throw new StringIndexOutOfBoundsException(offset);
381 if (offset > bytes.length - length)
382 throw new StringIndexOutOfBoundsException(offset + length);
386 * Constructs a new {@code String} by decoding the specified subarray of
387 * bytes using the specified charset. The length of the new {@code String}
388 * is a function of the charset, and hence may not be equal to the length
391 * <p> The behavior of this constructor when the given bytes are not valid
392 * in the given charset is unspecified. The {@link
393 * java.nio.charset.CharsetDecoder} class should be used when more control
394 * over the decoding process is required.
397 * The bytes to be decoded into characters
400 * The index of the first byte to decode
403 * The number of bytes to decode
406 * The name of a supported {@linkplain java.nio.charset.Charset
409 * @throws UnsupportedEncodingException
410 * If the named charset is not supported
412 * @throws IndexOutOfBoundsException
413 * If the {@code offset} and {@code length} arguments index
414 * characters outside the bounds of the {@code bytes} array
418 // public String(byte bytes[], int offset, int length, String charsetName)
419 // throws UnsupportedEncodingException
421 // if (charsetName == null)
422 // throw new NullPointerException("charsetName");
423 // checkBounds(bytes, offset, length);
424 // char[] v = StringCoding.decode(charsetName, bytes, offset, length);
426 // this.count = v.length;
431 * Constructs a new {@code String} by decoding the specified subarray of
432 * bytes using the specified {@linkplain java.nio.charset.Charset charset}.
433 * The length of the new {@code String} is a function of the charset, and
434 * hence may not be equal to the length of the subarray.
436 * <p> This method always replaces malformed-input and unmappable-character
437 * sequences with this charset's default replacement string. The {@link
438 * java.nio.charset.CharsetDecoder} class should be used when more control
439 * over the decoding process is required.
442 * The bytes to be decoded into characters
445 * The index of the first byte to decode
448 * The number of bytes to decode
451 * The {@linkplain java.nio.charset.Charset charset} to be used to
452 * decode the {@code bytes}
454 * @throws IndexOutOfBoundsException
455 * If the {@code offset} and {@code length} arguments index
456 * characters outside the bounds of the {@code bytes} array
460 /* don't want dependnecy on Charset
461 public String(byte bytes[], int offset, int length, Charset charset) {
463 throw new NullPointerException("charset");
464 checkBounds(bytes, offset, length);
465 char[] v = StringCoding.decode(charset, bytes, offset, length);
467 this.count = v.length;
473 * Constructs a new {@code String} by decoding the specified array of bytes
474 * using the specified {@linkplain java.nio.charset.Charset charset}. The
475 * length of the new {@code String} is a function of the charset, and hence
476 * may not be equal to the length of the byte array.
478 * <p> The behavior of this constructor when the given bytes are not valid
479 * in the given charset is unspecified. The {@link
480 * java.nio.charset.CharsetDecoder} class should be used when more control
481 * over the decoding process is required.
484 * The bytes to be decoded into characters
487 * The name of a supported {@linkplain java.nio.charset.Charset
490 * @throws UnsupportedEncodingException
491 * If the named charset is not supported
495 // public String(byte bytes[], String charsetName)
496 // throws UnsupportedEncodingException
498 // this(bytes, 0, bytes.length, charsetName);
502 * Constructs a new {@code String} by decoding the specified array of
503 * bytes using the specified {@linkplain java.nio.charset.Charset charset}.
504 * The length of the new {@code String} is a function of the charset, and
505 * hence may not be equal to the length of the byte array.
507 * <p> This method always replaces malformed-input and unmappable-character
508 * sequences with this charset's default replacement string. The {@link
509 * java.nio.charset.CharsetDecoder} class should be used when more control
510 * over the decoding process is required.
513 * The bytes to be decoded into characters
516 * The {@linkplain java.nio.charset.Charset charset} to be used to
517 * decode the {@code bytes}
521 /* don't want dep on Charset
522 public String(byte bytes[], Charset charset) {
523 this(bytes, 0, bytes.length, charset);
528 * Constructs a new {@code String} by decoding the specified subarray of
529 * bytes using the platform's default charset. The length of the new
530 * {@code String} is a function of the charset, and hence may not be equal
531 * to the length of the subarray.
533 * <p> The behavior of this constructor when the given bytes are not valid
534 * in the default charset is unspecified. The {@link
535 * java.nio.charset.CharsetDecoder} class should be used when more control
536 * over the decoding process is required.
539 * The bytes to be decoded into characters
542 * The index of the first byte to decode
545 * The number of bytes to decode
547 * @throws IndexOutOfBoundsException
548 * If the {@code offset} and the {@code length} arguments index
549 * characters outside the bounds of the {@code bytes} array
553 public String(byte bytes[], int offset, int length) {
554 checkBounds(bytes, offset, length);
555 char[] v = new char[length];
556 for (int i = 0; i < length; i++) {
557 v[i] = (char)bytes[offset++];
559 this.r = new String(v, 0, v.length);
563 * Constructs a new {@code String} by decoding the specified array of bytes
564 * using the platform's default charset. The length of the new {@code
565 * String} is a function of the charset, and hence may not be equal to the
566 * length of the byte array.
568 * <p> The behavior of this constructor when the given bytes are not valid
569 * in the default charset is unspecified. The {@link
570 * java.nio.charset.CharsetDecoder} class should be used when more control
571 * over the decoding process is required.
574 * The bytes to be decoded into characters
578 public String(byte bytes[]) {
579 this(bytes, 0, bytes.length);
583 * Allocates a new string that contains the sequence of characters
584 * currently contained in the string buffer argument. The contents of the
585 * string buffer are copied; subsequent modification of the string buffer
586 * does not affect the newly created string.
589 * A {@code StringBuffer}
591 public String(StringBuffer buffer) {
592 this.r = buffer.toString();
596 * Allocates a new string that contains the sequence of characters
597 * currently contained in the string builder argument. The contents of the
598 * string builder are copied; subsequent modification of the string builder
599 * does not affect the newly created string.
601 * <p> This constructor is provided to ease migration to {@code
602 * StringBuilder}. Obtaining a string from a string builder via the {@code
603 * toString} method is likely to run faster and is generally preferred.
606 * A {@code StringBuilder}
610 public String(StringBuilder builder) {
611 this.r = builder.toString();
615 * Returns the length of this string.
616 * The length is equal to the number of <a href="Character.html#unicode">Unicode
617 * code units</a> in the string.
619 * @return the length of the sequence of characters represented by this
622 @JavaScriptBody(args = {}, body = "return this.toString().length;")
623 public int length() {
624 throw new UnsupportedOperationException();
628 * Returns <tt>true</tt> if, and only if, {@link #length()} is <tt>0</tt>.
630 * @return <tt>true</tt> if {@link #length()} is <tt>0</tt>, otherwise
635 @JavaScriptBody(args = {}, body="return this.toString().length === 0;")
636 public boolean isEmpty() {
637 return length() == 0;
641 * Returns the <code>char</code> value at the
642 * specified index. An index ranges from <code>0</code> to
643 * <code>length() - 1</code>. The first <code>char</code> value of the sequence
644 * is at index <code>0</code>, the next at index <code>1</code>,
645 * and so on, as for array indexing.
647 * <p>If the <code>char</code> value specified by the index is a
648 * <a href="Character.html#unicode">surrogate</a>, the surrogate
651 * @param index the index of the <code>char</code> value.
652 * @return the <code>char</code> value at the specified index of this string.
653 * The first <code>char</code> value is at index <code>0</code>.
654 * @exception IndexOutOfBoundsException if the <code>index</code>
655 * argument is negative or not less than the length of this
658 @JavaScriptBody(args = { "index" },
659 body = "return this.toString().charCodeAt(index);"
661 public char charAt(int index) {
662 throw new UnsupportedOperationException();
666 * Returns the character (Unicode code point) at the specified
667 * index. The index refers to <code>char</code> values
668 * (Unicode code units) and ranges from <code>0</code> to
669 * {@link #length()}<code> - 1</code>.
671 * <p> If the <code>char</code> value specified at the given index
672 * is in the high-surrogate range, the following index is less
673 * than the length of this <code>String</code>, and the
674 * <code>char</code> value at the following index is in the
675 * low-surrogate range, then the supplementary code point
676 * corresponding to this surrogate pair is returned. Otherwise,
677 * the <code>char</code> value at the given index is returned.
679 * @param index the index to the <code>char</code> values
680 * @return the code point value of the character at the
682 * @exception IndexOutOfBoundsException if the <code>index</code>
683 * argument is negative or not less than the length of this
687 public int codePointAt(int index) {
688 if ((index < 0) || (index >= length())) {
689 throw new StringIndexOutOfBoundsException(index);
691 return Character.codePointAtImpl(toCharArray(), offset() + index, offset() + length());
695 * Returns the character (Unicode code point) before the specified
696 * index. The index refers to <code>char</code> values
697 * (Unicode code units) and ranges from <code>1</code> to {@link
698 * CharSequence#length() length}.
700 * <p> If the <code>char</code> value at <code>(index - 1)</code>
701 * is in the low-surrogate range, <code>(index - 2)</code> is not
702 * negative, and the <code>char</code> value at <code>(index -
703 * 2)</code> is in the high-surrogate range, then the
704 * supplementary code point value of the surrogate pair is
705 * returned. If the <code>char</code> value at <code>index -
706 * 1</code> is an unpaired low-surrogate or a high-surrogate, the
707 * surrogate value is returned.
709 * @param index the index following the code point that should be returned
710 * @return the Unicode code point value before the given index.
711 * @exception IndexOutOfBoundsException if the <code>index</code>
712 * argument is less than 1 or greater than the length
716 public int codePointBefore(int index) {
718 if ((i < 0) || (i >= length())) {
719 throw new StringIndexOutOfBoundsException(index);
721 return Character.codePointBeforeImpl(toCharArray(), offset() + index, offset());
725 * Returns the number of Unicode code points in the specified text
726 * range of this <code>String</code>. The text range begins at the
727 * specified <code>beginIndex</code> and extends to the
728 * <code>char</code> at index <code>endIndex - 1</code>. Thus the
729 * length (in <code>char</code>s) of the text range is
730 * <code>endIndex-beginIndex</code>. Unpaired surrogates within
731 * the text range count as one code point each.
733 * @param beginIndex the index to the first <code>char</code> of
735 * @param endIndex the index after the last <code>char</code> of
737 * @return the number of Unicode code points in the specified text
739 * @exception IndexOutOfBoundsException if the
740 * <code>beginIndex</code> is negative, or <code>endIndex</code>
741 * is larger than the length of this <code>String</code>, or
742 * <code>beginIndex</code> is larger than <code>endIndex</code>.
745 public int codePointCount(int beginIndex, int endIndex) {
746 if (beginIndex < 0 || endIndex > length() || beginIndex > endIndex) {
747 throw new IndexOutOfBoundsException();
749 return Character.codePointCountImpl(toCharArray(), offset()+beginIndex, endIndex-beginIndex);
753 * Returns the index within this <code>String</code> that is
754 * offset from the given <code>index</code> by
755 * <code>codePointOffset</code> code points. Unpaired surrogates
756 * within the text range given by <code>index</code> and
757 * <code>codePointOffset</code> count as one code point each.
759 * @param index the index to be offset
760 * @param codePointOffset the offset in code points
761 * @return the index within this <code>String</code>
762 * @exception IndexOutOfBoundsException if <code>index</code>
763 * is negative or larger then the length of this
764 * <code>String</code>, or if <code>codePointOffset</code> is positive
765 * and the substring starting with <code>index</code> has fewer
766 * than <code>codePointOffset</code> code points,
767 * or if <code>codePointOffset</code> is negative and the substring
768 * before <code>index</code> has fewer than the absolute value
769 * of <code>codePointOffset</code> code points.
772 public int offsetByCodePoints(int index, int codePointOffset) {
773 if (index < 0 || index > length()) {
774 throw new IndexOutOfBoundsException();
776 return Character.offsetByCodePointsImpl(toCharArray(), offset(), length(),
777 offset()+index, codePointOffset) - offset();
781 * Copy characters from this string into dst starting at dstBegin.
782 * This method doesn't perform any range checking.
784 @JavaScriptBody(args = { "arr", "to" }, body =
785 "var s = this.toString();\n" +
786 "for (var i = 0; i < s.length; i++) {\n" +
787 " arr[to++] = s[i];\n" +
790 void getChars(char dst[], int dstBegin) {
791 System.arraycopy(toCharArray(), offset(), dst, dstBegin, length());
795 * Copies characters from this string into the destination character
798 * The first character to be copied is at index <code>srcBegin</code>;
799 * the last character to be copied is at index <code>srcEnd-1</code>
800 * (thus the total number of characters to be copied is
801 * <code>srcEnd-srcBegin</code>). The characters are copied into the
802 * subarray of <code>dst</code> starting at index <code>dstBegin</code>
803 * and ending at index:
804 * <p><blockquote><pre>
805 * dstbegin + (srcEnd-srcBegin) - 1
806 * </pre></blockquote>
808 * @param srcBegin index of the first character in the string
810 * @param srcEnd index after the last character in the string
812 * @param dst the destination array.
813 * @param dstBegin the start offset in the destination array.
814 * @exception IndexOutOfBoundsException If any of the following
816 * <ul><li><code>srcBegin</code> is negative.
817 * <li><code>srcBegin</code> is greater than <code>srcEnd</code>
818 * <li><code>srcEnd</code> is greater than the length of this
820 * <li><code>dstBegin</code> is negative
821 * <li><code>dstBegin+(srcEnd-srcBegin)</code> is larger than
822 * <code>dst.length</code></ul>
824 @JavaScriptBody(args = { "beg", "end", "arr", "dst" }, body=
825 "var s = this.toString();\n" +
826 "while (beg < end) {\n" +
827 " arr[dst++] = s.charCodeAt(beg++);\n" +
830 public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {
832 throw new StringIndexOutOfBoundsException(srcBegin);
834 if (srcEnd > length()) {
835 throw new StringIndexOutOfBoundsException(srcEnd);
837 if (srcBegin > srcEnd) {
838 throw new StringIndexOutOfBoundsException(srcEnd - srcBegin);
840 System.arraycopy(toCharArray(), offset() + srcBegin, dst, dstBegin,
845 * Copies characters from this string into the destination byte array. Each
846 * byte receives the 8 low-order bits of the corresponding character. The
847 * eight high-order bits of each character are not copied and do not
848 * participate in the transfer in any way.
850 * <p> The first character to be copied is at index {@code srcBegin}; the
851 * last character to be copied is at index {@code srcEnd-1}. The total
852 * number of characters to be copied is {@code srcEnd-srcBegin}. The
853 * characters, converted to bytes, are copied into the subarray of {@code
854 * dst} starting at index {@code dstBegin} and ending at index:
857 * dstbegin + (srcEnd-srcBegin) - 1
858 * </pre></blockquote>
860 * @deprecated This method does not properly convert characters into
861 * bytes. As of JDK 1.1, the preferred way to do this is via the
862 * {@link #getBytes()} method, which uses the platform's default charset.
865 * Index of the first character in the string to copy
868 * Index after the last character in the string to copy
871 * The destination array
874 * The start offset in the destination array
876 * @throws IndexOutOfBoundsException
877 * If any of the following is true:
879 * <li> {@code srcBegin} is negative
880 * <li> {@code srcBegin} is greater than {@code srcEnd}
881 * <li> {@code srcEnd} is greater than the length of this String
882 * <li> {@code dstBegin} is negative
883 * <li> {@code dstBegin+(srcEnd-srcBegin)} is larger than {@code
888 public void getBytes(int srcBegin, int srcEnd, byte dst[], int dstBegin) {
890 throw new StringIndexOutOfBoundsException(srcBegin);
892 if (srcEnd > length()) {
893 throw new StringIndexOutOfBoundsException(srcEnd);
895 if (srcBegin > srcEnd) {
896 throw new StringIndexOutOfBoundsException(srcEnd - srcBegin);
899 int n = offset() + srcEnd;
900 int i = offset() + srcBegin;
901 char[] val = toCharArray(); /* avoid getfield opcode */
904 dst[j++] = (byte)val[i++];
909 * Encodes this {@code String} into a sequence of bytes using the named
910 * charset, storing the result into a new byte array.
912 * <p> The behavior of this method when this string cannot be encoded in
913 * the given charset is unspecified. The {@link
914 * java.nio.charset.CharsetEncoder} class should be used when more control
915 * over the encoding process is required.
918 * The name of a supported {@linkplain java.nio.charset.Charset
921 * @return The resultant byte array
923 * @throws UnsupportedEncodingException
924 * If the named charset is not supported
928 // public byte[] getBytes(String charsetName)
929 // throws UnsupportedEncodingException
931 // if (charsetName == null) throw new NullPointerException();
932 // return StringCoding.encode(charsetName, value, offset, count);
936 * Encodes this {@code String} into a sequence of bytes using the given
937 * {@linkplain java.nio.charset.Charset charset}, storing the result into a
940 * <p> This method always replaces malformed-input and unmappable-character
941 * sequences with this charset's default replacement byte array. The
942 * {@link java.nio.charset.CharsetEncoder} class should be used when more
943 * control over the encoding process is required.
946 * The {@linkplain java.nio.charset.Charset} to be used to encode
949 * @return The resultant byte array
953 /* don't want dep on Charset
954 public byte[] getBytes(Charset charset) {
955 if (charset == null) throw new NullPointerException();
956 return StringCoding.encode(charset, value, offset, count);
961 * Encodes this {@code String} into a sequence of bytes using the
962 * platform's default charset, storing the result into a new byte array.
964 * <p> The behavior of this method when this string cannot be encoded in
965 * the default charset is unspecified. The {@link
966 * java.nio.charset.CharsetEncoder} class should be used when more control
967 * over the encoding process is required.
969 * @return The resultant byte array
973 public byte[] getBytes() {
974 byte[] arr = new byte[length()];
975 for (int i = 0; i < arr.length; i++) {
976 final char v = charAt(i);
983 * Compares this string to the specified object. The result is {@code
984 * true} if and only if the argument is not {@code null} and is a {@code
985 * String} object that represents the same sequence of characters as this
989 * The object to compare this {@code String} against
991 * @return {@code true} if the given object represents a {@code String}
992 * equivalent to this string, {@code false} otherwise
994 * @see #compareTo(String)
995 * @see #equalsIgnoreCase(String)
997 @JavaScriptBody(args = { "obj" }, body =
998 "return obj != null && obj.$instOf_java_lang_String && "
999 + "this.toString() === obj.toString();"
1001 public boolean equals(Object anObject) {
1002 if (this == anObject) {
1005 if (anObject instanceof String) {
1006 String anotherString = (String)anObject;
1008 if (n == anotherString.length()) {
1009 char v1[] = toCharArray();
1010 char v2[] = anotherString.toCharArray();
1012 int j = anotherString.offset();
1014 if (v1[i++] != v2[j++])
1024 * Compares this string to the specified {@code StringBuffer}. The result
1025 * is {@code true} if and only if this {@code String} represents the same
1026 * sequence of characters as the specified {@code StringBuffer}.
1029 * The {@code StringBuffer} to compare this {@code String} against
1031 * @return {@code true} if this {@code String} represents the same
1032 * sequence of characters as the specified {@code StringBuffer},
1033 * {@code false} otherwise
1037 public boolean contentEquals(StringBuffer sb) {
1039 return contentEquals((CharSequence)sb);
1044 * Compares this string to the specified {@code CharSequence}. The result
1045 * is {@code true} if and only if this {@code String} represents the same
1046 * sequence of char values as the specified sequence.
1049 * The sequence to compare this {@code String} against
1051 * @return {@code true} if this {@code String} represents the same
1052 * sequence of char values as the specified sequence, {@code
1057 public boolean contentEquals(CharSequence cs) {
1058 if (length() != cs.length())
1060 // Argument is a StringBuffer, StringBuilder
1061 if (cs instanceof AbstractStringBuilder) {
1062 char v1[] = toCharArray();
1063 char v2[] = ((AbstractStringBuilder)cs).getValue();
1068 if (v1[i++] != v2[j++])
1073 // Argument is a String
1074 if (cs.equals(this))
1076 // Argument is a generic CharSequence
1077 char v1[] = toCharArray();
1082 if (v1[i++] != cs.charAt(j++))
1089 * Compares this {@code String} to another {@code String}, ignoring case
1090 * considerations. Two strings are considered equal ignoring case if they
1091 * are of the same length and corresponding characters in the two strings
1092 * are equal ignoring case.
1094 * <p> Two characters {@code c1} and {@code c2} are considered the same
1095 * ignoring case if at least one of the following is true:
1097 * <li> The two characters are the same (as compared by the
1098 * {@code ==} operator)
1099 * <li> Applying the method {@link
1100 * java.lang.Character#toUpperCase(char)} to each character
1101 * produces the same result
1102 * <li> Applying the method {@link
1103 * java.lang.Character#toLowerCase(char)} to each character
1104 * produces the same result
1107 * @param anotherString
1108 * The {@code String} to compare this {@code String} against
1110 * @return {@code true} if the argument is not {@code null} and it
1111 * represents an equivalent {@code String} ignoring case; {@code
1114 * @see #equals(Object)
1116 public boolean equalsIgnoreCase(String anotherString) {
1117 return (this == anotherString) ? true :
1118 (anotherString != null) && (anotherString.length() == length()) &&
1119 regionMatches(true, 0, anotherString, 0, length());
1123 * Compares two strings lexicographically.
1124 * The comparison is based on the Unicode value of each character in
1125 * the strings. The character sequence represented by this
1126 * <code>String</code> object is compared lexicographically to the
1127 * character sequence represented by the argument string. The result is
1128 * a negative integer if this <code>String</code> object
1129 * lexicographically precedes the argument string. The result is a
1130 * positive integer if this <code>String</code> object lexicographically
1131 * follows the argument string. The result is zero if the strings
1132 * are equal; <code>compareTo</code> returns <code>0</code> exactly when
1133 * the {@link #equals(Object)} method would return <code>true</code>.
1135 * This is the definition of lexicographic ordering. If two strings are
1136 * different, then either they have different characters at some index
1137 * that is a valid index for both strings, or their lengths are different,
1138 * or both. If they have different characters at one or more index
1139 * positions, let <i>k</i> be the smallest such index; then the string
1140 * whose character at position <i>k</i> has the smaller value, as
1141 * determined by using the < operator, lexicographically precedes the
1142 * other string. In this case, <code>compareTo</code> returns the
1143 * difference of the two character values at position <code>k</code> in
1144 * the two string -- that is, the value:
1146 * this.charAt(k)-anotherString.charAt(k)
1147 * </pre></blockquote>
1148 * If there is no index position at which they differ, then the shorter
1149 * string lexicographically precedes the longer string. In this case,
1150 * <code>compareTo</code> returns the difference of the lengths of the
1151 * strings -- that is, the value:
1153 * this.length()-anotherString.length()
1154 * </pre></blockquote>
1156 * @param anotherString the <code>String</code> to be compared.
1157 * @return the value <code>0</code> if the argument string is equal to
1158 * this string; a value less than <code>0</code> if this string
1159 * is lexicographically less than the string argument; and a
1160 * value greater than <code>0</code> if this string is
1161 * lexicographically greater than the string argument.
1163 public int compareTo(String anotherString) {
1164 int len1 = length();
1165 int len2 = anotherString.length();
1166 int n = Math.min(len1, len2);
1167 char v1[] = toCharArray();
1168 char v2[] = anotherString.toCharArray();
1170 int j = anotherString.offset();
1196 * A Comparator that orders <code>String</code> objects as by
1197 * <code>compareToIgnoreCase</code>. This comparator is serializable.
1199 * Note that this Comparator does <em>not</em> take locale into account,
1200 * and will result in an unsatisfactory ordering for certain locales.
1201 * The java.text package provides <em>Collators</em> to allow
1202 * locale-sensitive ordering.
1204 * @see java.text.Collator#compare(String, String)
1207 public static final Comparator<String> CASE_INSENSITIVE_ORDER
1208 = new CaseInsensitiveComparator();
1210 private static int offset() {
1214 private static class CaseInsensitiveComparator
1215 implements Comparator<String>, java.io.Serializable {
1216 // use serialVersionUID from JDK 1.2.2 for interoperability
1217 private static final long serialVersionUID = 8575799808933029326L;
1219 public int compare(String s1, String s2) {
1220 int n1 = s1.length();
1221 int n2 = s2.length();
1222 int min = Math.min(n1, n2);
1223 for (int i = 0; i < min; i++) {
1224 char c1 = s1.charAt(i);
1225 char c2 = s2.charAt(i);
1227 c1 = Character.toUpperCase(c1);
1228 c2 = Character.toUpperCase(c2);
1230 c1 = Character.toLowerCase(c1);
1231 c2 = Character.toLowerCase(c2);
1233 // No overflow because of numeric promotion
1244 * Compares two strings lexicographically, ignoring case
1245 * differences. This method returns an integer whose sign is that of
1246 * calling <code>compareTo</code> with normalized versions of the strings
1247 * where case differences have been eliminated by calling
1248 * <code>Character.toLowerCase(Character.toUpperCase(character))</code> on
1251 * Note that this method does <em>not</em> take locale into account,
1252 * and will result in an unsatisfactory ordering for certain locales.
1253 * The java.text package provides <em>collators</em> to allow
1254 * locale-sensitive ordering.
1256 * @param str the <code>String</code> to be compared.
1257 * @return a negative integer, zero, or a positive integer as the
1258 * specified String is greater than, equal to, or less
1259 * than this String, ignoring case considerations.
1260 * @see java.text.Collator#compare(String, String)
1263 public int compareToIgnoreCase(String str) {
1264 return CASE_INSENSITIVE_ORDER.compare(this, str);
1268 * Tests if two string regions are equal.
1270 * A substring of this <tt>String</tt> object is compared to a substring
1271 * of the argument other. The result is true if these substrings
1272 * represent identical character sequences. The substring of this
1273 * <tt>String</tt> object to be compared begins at index <tt>toffset</tt>
1274 * and has length <tt>len</tt>. The substring of other to be compared
1275 * begins at index <tt>ooffset</tt> and has length <tt>len</tt>. The
1276 * result is <tt>false</tt> if and only if at least one of the following
1278 * <ul><li><tt>toffset</tt> is negative.
1279 * <li><tt>ooffset</tt> is negative.
1280 * <li><tt>toffset+len</tt> is greater than the length of this
1281 * <tt>String</tt> object.
1282 * <li><tt>ooffset+len</tt> is greater than the length of the other
1284 * <li>There is some nonnegative integer <i>k</i> less than <tt>len</tt>
1286 * <tt>this.charAt(toffset+<i>k</i>) != other.charAt(ooffset+<i>k</i>)</tt>
1289 * @param toffset the starting offset of the subregion in this string.
1290 * @param other the string argument.
1291 * @param ooffset the starting offset of the subregion in the string
1293 * @param len the number of characters to compare.
1294 * @return <code>true</code> if the specified subregion of this string
1295 * exactly matches the specified subregion of the string argument;
1296 * <code>false</code> otherwise.
1298 public boolean regionMatches(int toffset, String other, int ooffset,
1300 char ta[] = toCharArray();
1301 int to = offset() + toffset;
1302 char pa[] = other.toCharArray();
1303 int po = other.offset() + ooffset;
1304 // Note: toffset, ooffset, or len might be near -1>>>1.
1305 if ((ooffset < 0) || (toffset < 0) || (toffset > (long)length() - len)
1306 || (ooffset > (long)other.length() - len)) {
1310 if (ta[to++] != pa[po++]) {
1318 * Tests if two string regions are equal.
1320 * A substring of this <tt>String</tt> object is compared to a substring
1321 * of the argument <tt>other</tt>. The result is <tt>true</tt> if these
1322 * substrings represent character sequences that are the same, ignoring
1323 * case if and only if <tt>ignoreCase</tt> is true. The substring of
1324 * this <tt>String</tt> object to be compared begins at index
1325 * <tt>toffset</tt> and has length <tt>len</tt>. The substring of
1326 * <tt>other</tt> to be compared begins at index <tt>ooffset</tt> and
1327 * has length <tt>len</tt>. The result is <tt>false</tt> if and only if
1328 * at least one of the following is true:
1329 * <ul><li><tt>toffset</tt> is negative.
1330 * <li><tt>ooffset</tt> is negative.
1331 * <li><tt>toffset+len</tt> is greater than the length of this
1332 * <tt>String</tt> object.
1333 * <li><tt>ooffset+len</tt> is greater than the length of the other
1335 * <li><tt>ignoreCase</tt> is <tt>false</tt> and there is some nonnegative
1336 * integer <i>k</i> less than <tt>len</tt> such that:
1338 * this.charAt(toffset+k) != other.charAt(ooffset+k)
1339 * </pre></blockquote>
1340 * <li><tt>ignoreCase</tt> is <tt>true</tt> and there is some nonnegative
1341 * integer <i>k</i> less than <tt>len</tt> such that:
1343 * Character.toLowerCase(this.charAt(toffset+k)) !=
1344 Character.toLowerCase(other.charAt(ooffset+k))
1345 * </pre></blockquote>
1348 * Character.toUpperCase(this.charAt(toffset+k)) !=
1349 * Character.toUpperCase(other.charAt(ooffset+k))
1350 * </pre></blockquote>
1353 * @param ignoreCase if <code>true</code>, ignore case when comparing
1355 * @param toffset the starting offset of the subregion in this
1357 * @param other the string argument.
1358 * @param ooffset the starting offset of the subregion in the string
1360 * @param len the number of characters to compare.
1361 * @return <code>true</code> if the specified subregion of this string
1362 * matches the specified subregion of the string argument;
1363 * <code>false</code> otherwise. Whether the matching is exact
1364 * or case insensitive depends on the <code>ignoreCase</code>
1367 public boolean regionMatches(boolean ignoreCase, int toffset,
1368 String other, int ooffset, int len) {
1369 char ta[] = toCharArray();
1370 int to = offset() + toffset;
1371 char pa[] = other.toCharArray();
1372 int po = other.offset() + ooffset;
1373 // Note: toffset, ooffset, or len might be near -1>>>1.
1374 if ((ooffset < 0) || (toffset < 0) || (toffset > (long)length() - len) ||
1375 (ooffset > (long)other.length() - len)) {
1385 // If characters don't match but case may be ignored,
1386 // try converting both characters to uppercase.
1387 // If the results match, then the comparison scan should
1389 char u1 = Character.toUpperCase(c1);
1390 char u2 = Character.toUpperCase(c2);
1394 // Unfortunately, conversion to uppercase does not work properly
1395 // for the Georgian alphabet, which has strange rules about case
1396 // conversion. So we need to make one last check before
1398 if (Character.toLowerCase(u1) == Character.toLowerCase(u2)) {
1408 * Tests if the substring of this string beginning at the
1409 * specified index starts with the specified prefix.
1411 * @param prefix the prefix.
1412 * @param toffset where to begin looking in this string.
1413 * @return <code>true</code> if the character sequence represented by the
1414 * argument is a prefix of the substring of this object starting
1415 * at index <code>toffset</code>; <code>false</code> otherwise.
1416 * The result is <code>false</code> if <code>toffset</code> is
1417 * negative or greater than the length of this
1418 * <code>String</code> object; otherwise the result is the same
1419 * as the result of the expression
1421 * this.substring(toffset).startsWith(prefix)
1424 @JavaScriptBody(args = { "find", "from" }, body=
1425 "find = find.toString();\n" +
1426 "return this.toString().substring(from, from + find.length) === find;\n"
1428 public boolean startsWith(String prefix, int toffset) {
1429 char ta[] = toCharArray();
1430 int to = offset() + toffset;
1431 char pa[] = prefix.toCharArray();
1432 int po = prefix.offset();
1433 int pc = prefix.length();
1434 // Note: toffset might be near -1>>>1.
1435 if ((toffset < 0) || (toffset > length() - pc)) {
1439 if (ta[to++] != pa[po++]) {
1447 * Tests if this string starts with the specified prefix.
1449 * @param prefix the prefix.
1450 * @return <code>true</code> if the character sequence represented by the
1451 * argument is a prefix of the character sequence represented by
1452 * this string; <code>false</code> otherwise.
1453 * Note also that <code>true</code> will be returned if the
1454 * argument is an empty string or is equal to this
1455 * <code>String</code> object as determined by the
1456 * {@link #equals(Object)} method.
1459 public boolean startsWith(String prefix) {
1460 return startsWith(prefix, 0);
1464 * Tests if this string ends with the specified suffix.
1466 * @param suffix the suffix.
1467 * @return <code>true</code> if the character sequence represented by the
1468 * argument is a suffix of the character sequence represented by
1469 * this object; <code>false</code> otherwise. Note that the
1470 * result will be <code>true</code> if the argument is the
1471 * empty string or is equal to this <code>String</code> object
1472 * as determined by the {@link #equals(Object)} method.
1474 public boolean endsWith(String suffix) {
1475 return startsWith(suffix, length() - suffix.length());
1479 * Returns a hash code for this string. The hash code for a
1480 * <code>String</code> object is computed as
1482 * s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
1483 * </pre></blockquote>
1484 * using <code>int</code> arithmetic, where <code>s[i]</code> is the
1485 * <i>i</i>th character of the string, <code>n</code> is the length of
1486 * the string, and <code>^</code> indicates exponentiation.
1487 * (The hash value of the empty string is zero.)
1489 * @return a hash code value for this object.
1491 public int hashCode() {
1492 return super.hashCode();
1494 int computeHashCode() {
1496 if (h == 0 && length() > 0) {
1500 for (int i = 0; i < len; i++) {
1501 h = 31*h + charAt(off++);
1508 * Returns the index within this string of the first occurrence of
1509 * the specified character. If a character with value
1510 * <code>ch</code> occurs in the character sequence represented by
1511 * this <code>String</code> object, then the index (in Unicode
1512 * code units) of the first such occurrence is returned. For
1513 * values of <code>ch</code> in the range from 0 to 0xFFFF
1514 * (inclusive), this is the smallest value <i>k</i> such that:
1516 * this.charAt(<i>k</i>) == ch
1517 * </pre></blockquote>
1518 * is true. For other values of <code>ch</code>, it is the
1519 * smallest value <i>k</i> such that:
1521 * this.codePointAt(<i>k</i>) == ch
1522 * </pre></blockquote>
1523 * is true. In either case, if no such character occurs in this
1524 * string, then <code>-1</code> is returned.
1526 * @param ch a character (Unicode code point).
1527 * @return the index of the first occurrence of the character in the
1528 * character sequence represented by this object, or
1529 * <code>-1</code> if the character does not occur.
1531 public int indexOf(int ch) {
1532 return indexOf(ch, 0);
1536 * Returns the index within this string of the first occurrence of the
1537 * specified character, starting the search at the specified index.
1539 * If a character with value <code>ch</code> occurs in the
1540 * character sequence represented by this <code>String</code>
1541 * object at an index no smaller than <code>fromIndex</code>, then
1542 * the index of the first such occurrence is returned. For values
1543 * of <code>ch</code> in the range from 0 to 0xFFFF (inclusive),
1544 * this is the smallest value <i>k</i> such that:
1546 * (this.charAt(<i>k</i>) == ch) && (<i>k</i> >= fromIndex)
1547 * </pre></blockquote>
1548 * is true. For other values of <code>ch</code>, it is the
1549 * smallest value <i>k</i> such that:
1551 * (this.codePointAt(<i>k</i>) == ch) && (<i>k</i> >= fromIndex)
1552 * </pre></blockquote>
1553 * is true. In either case, if no such character occurs in this
1554 * string at or after position <code>fromIndex</code>, then
1555 * <code>-1</code> is returned.
1558 * There is no restriction on the value of <code>fromIndex</code>. If it
1559 * is negative, it has the same effect as if it were zero: this entire
1560 * string may be searched. If it is greater than the length of this
1561 * string, it has the same effect as if it were equal to the length of
1562 * this string: <code>-1</code> is returned.
1564 * <p>All indices are specified in <code>char</code> values
1565 * (Unicode code units).
1567 * @param ch a character (Unicode code point).
1568 * @param fromIndex the index to start the search from.
1569 * @return the index of the first occurrence of the character in the
1570 * character sequence represented by this object that is greater
1571 * than or equal to <code>fromIndex</code>, or <code>-1</code>
1572 * if the character does not occur.
1574 @JavaScriptBody(args = { "ch", "from" }, body =
1575 "if (typeof ch === 'number') ch = String.fromCharCode(ch);\n" +
1576 "return this.toString().indexOf(ch, from);\n"
1578 public int indexOf(int ch, int fromIndex) {
1579 if (fromIndex < 0) {
1581 } else if (fromIndex >= length()) {
1582 // Note: fromIndex might be near -1>>>1.
1586 if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) {
1587 // handle most cases here (ch is a BMP code point or a
1588 // negative value (invalid code point))
1589 final char[] value = this.toCharArray();
1590 final int offset = this.offset();
1591 final int max = offset + length();
1592 for (int i = offset + fromIndex; i < max ; i++) {
1593 if (value[i] == ch) {
1599 return indexOfSupplementary(ch, fromIndex);
1604 * Handles (rare) calls of indexOf with a supplementary character.
1606 private int indexOfSupplementary(int ch, int fromIndex) {
1607 if (Character.isValidCodePoint(ch)) {
1608 final char[] value = this.toCharArray();
1609 final int offset = this.offset();
1610 final char hi = Character.highSurrogate(ch);
1611 final char lo = Character.lowSurrogate(ch);
1612 final int max = offset + length() - 1;
1613 for (int i = offset + fromIndex; i < max; i++) {
1614 if (value[i] == hi && value[i+1] == lo) {
1623 * Returns the index within this string of the last occurrence of
1624 * the specified character. For values of <code>ch</code> in the
1625 * range from 0 to 0xFFFF (inclusive), the index (in Unicode code
1626 * units) returned is the largest value <i>k</i> such that:
1628 * this.charAt(<i>k</i>) == ch
1629 * </pre></blockquote>
1630 * is true. For other values of <code>ch</code>, it is the
1631 * largest value <i>k</i> such that:
1633 * this.codePointAt(<i>k</i>) == ch
1634 * </pre></blockquote>
1635 * is true. In either case, if no such character occurs in this
1636 * string, then <code>-1</code> is returned. The
1637 * <code>String</code> is searched backwards starting at the last
1640 * @param ch a character (Unicode code point).
1641 * @return the index of the last occurrence of the character in the
1642 * character sequence represented by this object, or
1643 * <code>-1</code> if the character does not occur.
1645 public int lastIndexOf(int ch) {
1646 return lastIndexOf(ch, length() - 1);
1650 * Returns the index within this string of the last occurrence of
1651 * the specified character, searching backward starting at the
1652 * specified index. For values of <code>ch</code> in the range
1653 * from 0 to 0xFFFF (inclusive), the index returned is the largest
1654 * value <i>k</i> such that:
1656 * (this.charAt(<i>k</i>) == ch) && (<i>k</i> <= fromIndex)
1657 * </pre></blockquote>
1658 * is true. For other values of <code>ch</code>, it is the
1659 * largest value <i>k</i> such that:
1661 * (this.codePointAt(<i>k</i>) == ch) && (<i>k</i> <= fromIndex)
1662 * </pre></blockquote>
1663 * is true. In either case, if no such character occurs in this
1664 * string at or before position <code>fromIndex</code>, then
1665 * <code>-1</code> is returned.
1667 * <p>All indices are specified in <code>char</code> values
1668 * (Unicode code units).
1670 * @param ch a character (Unicode code point).
1671 * @param fromIndex the index to start the search from. There is no
1672 * restriction on the value of <code>fromIndex</code>. If it is
1673 * greater than or equal to the length of this string, it has
1674 * the same effect as if it were equal to one less than the
1675 * length of this string: this entire string may be searched.
1676 * If it is negative, it has the same effect as if it were -1:
1678 * @return the index of the last occurrence of the character in the
1679 * character sequence represented by this object that is less
1680 * than or equal to <code>fromIndex</code>, or <code>-1</code>
1681 * if the character does not occur before that point.
1683 @JavaScriptBody(args = { "ch", "from" }, body =
1684 "if (typeof ch === 'number') ch = String.fromCharCode(ch);\n" +
1685 "return this.toString().lastIndexOf(ch, from);"
1687 public int lastIndexOf(int ch, int fromIndex) {
1688 if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) {
1689 // handle most cases here (ch is a BMP code point or a
1690 // negative value (invalid code point))
1691 final char[] value = this.toCharArray();
1692 final int offset = this.offset();
1693 int i = offset + Math.min(fromIndex, length() - 1);
1694 for (; i >= offset ; i--) {
1695 if (value[i] == ch) {
1701 return lastIndexOfSupplementary(ch, fromIndex);
1706 * Handles (rare) calls of lastIndexOf with a supplementary character.
1708 private int lastIndexOfSupplementary(int ch, int fromIndex) {
1709 if (Character.isValidCodePoint(ch)) {
1710 final char[] value = this.toCharArray();
1711 final int offset = this.offset();
1712 char hi = Character.highSurrogate(ch);
1713 char lo = Character.lowSurrogate(ch);
1714 int i = offset + Math.min(fromIndex, length() - 2);
1715 for (; i >= offset; i--) {
1716 if (value[i] == hi && value[i+1] == lo) {
1725 * Returns the index within this string of the first occurrence of the
1726 * specified substring.
1728 * <p>The returned index is the smallest value <i>k</i> for which:
1730 * this.startsWith(str, <i>k</i>)
1731 * </pre></blockquote>
1732 * If no such value of <i>k</i> exists, then {@code -1} is returned.
1734 * @param str the substring to search for.
1735 * @return the index of the first occurrence of the specified substring,
1736 * or {@code -1} if there is no such occurrence.
1738 public int indexOf(String str) {
1739 return indexOf(str, 0);
1743 * Returns the index within this string of the first occurrence of the
1744 * specified substring, starting at the specified index.
1746 * <p>The returned index is the smallest value <i>k</i> for which:
1748 * <i>k</i> >= fromIndex && this.startsWith(str, <i>k</i>)
1749 * </pre></blockquote>
1750 * If no such value of <i>k</i> exists, then {@code -1} is returned.
1752 * @param str the substring to search for.
1753 * @param fromIndex the index from which to start the search.
1754 * @return the index of the first occurrence of the specified substring,
1755 * starting at the specified index,
1756 * or {@code -1} if there is no such occurrence.
1758 @JavaScriptBody(args = { "str", "fromIndex" }, body =
1759 "return this.toString().indexOf(str.toString(), fromIndex);"
1761 public native int indexOf(String str, int fromIndex);
1764 * Returns the index within this string of the last occurrence of the
1765 * specified substring. The last occurrence of the empty string ""
1766 * is considered to occur at the index value {@code this.length()}.
1768 * <p>The returned index is the largest value <i>k</i> for which:
1770 * this.startsWith(str, <i>k</i>)
1771 * </pre></blockquote>
1772 * If no such value of <i>k</i> exists, then {@code -1} is returned.
1774 * @param str the substring to search for.
1775 * @return the index of the last occurrence of the specified substring,
1776 * or {@code -1} if there is no such occurrence.
1778 public int lastIndexOf(String str) {
1779 return lastIndexOf(str, length());
1783 * Returns the index within this string of the last occurrence of the
1784 * specified substring, searching backward starting at the specified index.
1786 * <p>The returned index is the largest value <i>k</i> for which:
1788 * <i>k</i> <= fromIndex && this.startsWith(str, <i>k</i>)
1789 * </pre></blockquote>
1790 * If no such value of <i>k</i> exists, then {@code -1} is returned.
1792 * @param str the substring to search for.
1793 * @param fromIndex the index to start the search from.
1794 * @return the index of the last occurrence of the specified substring,
1795 * searching backward from the specified index,
1796 * or {@code -1} if there is no such occurrence.
1798 @JavaScriptBody(args = { "s", "from" }, body =
1799 "return this.toString().lastIndexOf(s.toString(), from);"
1801 public int lastIndexOf(String str, int fromIndex) {
1802 return lastIndexOf(toCharArray(), offset(), length(), str.toCharArray(), str.offset(), str.length(), fromIndex);
1806 * Code shared by String and StringBuffer to do searches. The
1807 * source is the character array being searched, and the target
1808 * is the string being searched for.
1810 * @param source the characters being searched.
1811 * @param sourceOffset offset of the source string.
1812 * @param sourceCount count of the source string.
1813 * @param target the characters being searched for.
1814 * @param targetOffset offset of the target string.
1815 * @param targetCount count of the target string.
1816 * @param fromIndex the index to begin searching from.
1818 static int lastIndexOf(char[] source, int sourceOffset, int sourceCount,
1819 char[] target, int targetOffset, int targetCount,
1822 * Check arguments; return immediately where possible. For
1823 * consistency, don't check for null str.
1825 int rightIndex = sourceCount - targetCount;
1826 if (fromIndex < 0) {
1829 if (fromIndex > rightIndex) {
1830 fromIndex = rightIndex;
1832 /* Empty string always matches. */
1833 if (targetCount == 0) {
1837 int strLastIndex = targetOffset + targetCount - 1;
1838 char strLastChar = target[strLastIndex];
1839 int min = sourceOffset + targetCount - 1;
1840 int i = min + fromIndex;
1842 startSearchForLastChar:
1844 while (i >= min && source[i] != strLastChar) {
1851 int start = j - (targetCount - 1);
1852 int k = strLastIndex - 1;
1855 if (source[j--] != target[k--]) {
1857 continue startSearchForLastChar;
1860 return start - sourceOffset + 1;
1865 * Returns a new string that is a substring of this string. The
1866 * substring begins with the character at the specified index and
1867 * extends to the end of this string. <p>
1870 * "unhappy".substring(2) returns "happy"
1871 * "Harbison".substring(3) returns "bison"
1872 * "emptiness".substring(9) returns "" (an empty string)
1873 * </pre></blockquote>
1875 * @param beginIndex the beginning index, inclusive.
1876 * @return the specified substring.
1877 * @exception IndexOutOfBoundsException if
1878 * <code>beginIndex</code> is negative or larger than the
1879 * length of this <code>String</code> object.
1881 public String substring(int beginIndex) {
1882 return substring(beginIndex, length());
1886 * Returns a new string that is a substring of this string. The
1887 * substring begins at the specified <code>beginIndex</code> and
1888 * extends to the character at index <code>endIndex - 1</code>.
1889 * Thus the length of the substring is <code>endIndex-beginIndex</code>.
1893 * "hamburger".substring(4, 8) returns "urge"
1894 * "smiles".substring(1, 5) returns "mile"
1895 * </pre></blockquote>
1897 * @param beginIndex the beginning index, inclusive.
1898 * @param endIndex the ending index, exclusive.
1899 * @return the specified substring.
1900 * @exception IndexOutOfBoundsException if the
1901 * <code>beginIndex</code> is negative, or
1902 * <code>endIndex</code> is larger than the length of
1903 * this <code>String</code> object, or
1904 * <code>beginIndex</code> is larger than
1905 * <code>endIndex</code>.
1907 @JavaScriptBody(args = { "beginIndex", "endIndex" }, body =
1908 "return this.toString().substring(beginIndex, endIndex);"
1910 public String substring(int beginIndex, int endIndex) {
1911 if (beginIndex < 0) {
1912 throw new StringIndexOutOfBoundsException(beginIndex);
1914 if (endIndex > length()) {
1915 throw new StringIndexOutOfBoundsException(endIndex);
1917 if (beginIndex > endIndex) {
1918 throw new StringIndexOutOfBoundsException(endIndex - beginIndex);
1920 return ((beginIndex == 0) && (endIndex == length())) ? this :
1921 new String(toCharArray(), offset() + beginIndex, endIndex - beginIndex);
1925 * Returns a new character sequence that is a subsequence of this sequence.
1927 * <p> An invocation of this method of the form
1930 * str.subSequence(begin, end)</pre></blockquote>
1932 * behaves in exactly the same way as the invocation
1935 * str.substring(begin, end)</pre></blockquote>
1937 * This method is defined so that the <tt>String</tt> class can implement
1938 * the {@link CharSequence} interface. </p>
1940 * @param beginIndex the begin index, inclusive.
1941 * @param endIndex the end index, exclusive.
1942 * @return the specified subsequence.
1944 * @throws IndexOutOfBoundsException
1945 * if <tt>beginIndex</tt> or <tt>endIndex</tt> are negative,
1946 * if <tt>endIndex</tt> is greater than <tt>length()</tt>,
1947 * or if <tt>beginIndex</tt> is greater than <tt>startIndex</tt>
1952 public CharSequence subSequence(int beginIndex, int endIndex) {
1953 return this.substring(beginIndex, endIndex);
1957 * Concatenates the specified string to the end of this string.
1959 * If the length of the argument string is <code>0</code>, then this
1960 * <code>String</code> object is returned. Otherwise, a new
1961 * <code>String</code> object is created, representing a character
1962 * sequence that is the concatenation of the character sequence
1963 * represented by this <code>String</code> object and the character
1964 * sequence represented by the argument string.<p>
1967 * "cares".concat("s") returns "caress"
1968 * "to".concat("get").concat("her") returns "together"
1969 * </pre></blockquote>
1971 * @param str the <code>String</code> that is concatenated to the end
1972 * of this <code>String</code>.
1973 * @return a string that represents the concatenation of this object's
1974 * characters followed by the string argument's characters.
1976 public String concat(String str) {
1977 int otherLen = str.length();
1978 if (otherLen == 0) {
1981 char buf[] = new char[length() + otherLen];
1982 getChars(0, length(), buf, 0);
1983 str.getChars(0, otherLen, buf, length());
1984 return new String(buf, 0, length() + otherLen);
1988 * Returns a new string resulting from replacing all occurrences of
1989 * <code>oldChar</code> in this string with <code>newChar</code>.
1991 * If the character <code>oldChar</code> does not occur in the
1992 * character sequence represented by this <code>String</code> object,
1993 * then a reference to this <code>String</code> object is returned.
1994 * Otherwise, a new <code>String</code> object is created that
1995 * represents a character sequence identical to the character sequence
1996 * represented by this <code>String</code> object, except that every
1997 * occurrence of <code>oldChar</code> is replaced by an occurrence
1998 * of <code>newChar</code>.
2002 * "mesquite in your cellar".replace('e', 'o')
2003 * returns "mosquito in your collar"
2004 * "the war of baronets".replace('r', 'y')
2005 * returns "the way of bayonets"
2006 * "sparring with a purple porpoise".replace('p', 't')
2007 * returns "starring with a turtle tortoise"
2008 * "JonL".replace('q', 'x') returns "JonL" (no change)
2009 * </pre></blockquote>
2011 * @param oldChar the old character.
2012 * @param newChar the new character.
2013 * @return a string derived from this string by replacing every
2014 * occurrence of <code>oldChar</code> with <code>newChar</code>.
2016 @JavaScriptBody(args = { "arg1", "arg2" }, body =
2017 "if (typeof arg1 === 'number') arg1 = String.fromCharCode(arg1);\n" +
2018 "if (typeof arg2 === 'number') arg2 = String.fromCharCode(arg2);\n" +
2019 "var s = this.toString();\n" +
2021 " var ret = s.replace(arg1, arg2);\n" +
2022 " if (ret === s) {\n" +
2028 public String replace(char oldChar, char newChar) {
2029 if (oldChar != newChar) {
2032 char[] val = toCharArray(); /* avoid getfield opcode */
2033 int off = offset(); /* avoid getfield opcode */
2036 if (val[off + i] == oldChar) {
2041 char buf[] = new char[len];
2042 for (int j = 0 ; j < i ; j++) {
2043 buf[j] = val[off+j];
2046 char c = val[off + i];
2047 buf[i] = (c == oldChar) ? newChar : c;
2050 return new String(buf, 0, len);
2057 * Tells whether or not this string matches the given <a
2058 * href="../util/regex/Pattern.html#sum">regular expression</a>.
2060 * <p> An invocation of this method of the form
2061 * <i>str</i><tt>.matches(</tt><i>regex</i><tt>)</tt> yields exactly the
2062 * same result as the expression
2064 * <blockquote><tt> {@link java.util.regex.Pattern}.{@link
2065 * java.util.regex.Pattern#matches(String,CharSequence)
2066 * matches}(</tt><i>regex</i><tt>,</tt> <i>str</i><tt>)</tt></blockquote>
2069 * the regular expression to which this string is to be matched
2071 * @return <tt>true</tt> if, and only if, this string matches the
2072 * given regular expression
2074 * @throws PatternSyntaxException
2075 * if the regular expression's syntax is invalid
2077 * @see java.util.regex.Pattern
2082 @JavaScriptBody(args = { "regex" }, body =
2083 "var self = this.toString();\n"
2084 + "var re = new RegExp(regex.toString());\n"
2085 + "var r = re.exec(self);\n"
2086 + "return r != null && r.length > 0 && self.length == r[0].length;"
2088 public boolean matches(String regex) {
2089 throw new UnsupportedOperationException();
2093 * Returns true if and only if this string contains the specified
2094 * sequence of char values.
2096 * @param s the sequence to search for
2097 * @return true if this string contains <code>s</code>, false otherwise
2098 * @throws NullPointerException if <code>s</code> is <code>null</code>
2101 public boolean contains(CharSequence s) {
2102 return indexOf(s.toString()) > -1;
2106 * Replaces the first substring of this string that matches the given <a
2107 * href="../util/regex/Pattern.html#sum">regular expression</a> with the
2108 * given replacement.
2110 * <p> An invocation of this method of the form
2111 * <i>str</i><tt>.replaceFirst(</tt><i>regex</i><tt>,</tt> <i>repl</i><tt>)</tt>
2112 * yields exactly the same result as the expression
2115 * {@link java.util.regex.Pattern}.{@link java.util.regex.Pattern#compile
2116 * compile}(</tt><i>regex</i><tt>).{@link
2117 * java.util.regex.Pattern#matcher(java.lang.CharSequence)
2118 * matcher}(</tt><i>str</i><tt>).{@link java.util.regex.Matcher#replaceFirst
2119 * replaceFirst}(</tt><i>repl</i><tt>)</tt></blockquote>
2122 * Note that backslashes (<tt>\</tt>) and dollar signs (<tt>$</tt>) in the
2123 * replacement string may cause the results to be different than if it were
2124 * being treated as a literal replacement string; see
2125 * {@link java.util.regex.Matcher#replaceFirst}.
2126 * Use {@link java.util.regex.Matcher#quoteReplacement} to suppress the special
2127 * meaning of these characters, if desired.
2130 * the regular expression to which this string is to be matched
2131 * @param replacement
2132 * the string to be substituted for the first match
2134 * @return The resulting <tt>String</tt>
2136 * @throws PatternSyntaxException
2137 * if the regular expression's syntax is invalid
2139 * @see java.util.regex.Pattern
2144 public String replaceFirst(String regex, String replacement) {
2145 throw new UnsupportedOperationException();
2149 * Replaces each substring of this string that matches the given <a
2150 * href="../util/regex/Pattern.html#sum">regular expression</a> with the
2151 * given replacement.
2153 * <p> An invocation of this method of the form
2154 * <i>str</i><tt>.replaceAll(</tt><i>regex</i><tt>,</tt> <i>repl</i><tt>)</tt>
2155 * yields exactly the same result as the expression
2158 * {@link java.util.regex.Pattern}.{@link java.util.regex.Pattern#compile
2159 * compile}(</tt><i>regex</i><tt>).{@link
2160 * java.util.regex.Pattern#matcher(java.lang.CharSequence)
2161 * matcher}(</tt><i>str</i><tt>).{@link java.util.regex.Matcher#replaceAll
2162 * replaceAll}(</tt><i>repl</i><tt>)</tt></blockquote>
2165 * Note that backslashes (<tt>\</tt>) and dollar signs (<tt>$</tt>) in the
2166 * replacement string may cause the results to be different than if it were
2167 * being treated as a literal replacement string; see
2168 * {@link java.util.regex.Matcher#replaceAll Matcher.replaceAll}.
2169 * Use {@link java.util.regex.Matcher#quoteReplacement} to suppress the special
2170 * meaning of these characters, if desired.
2173 * the regular expression to which this string is to be matched
2174 * @param replacement
2175 * the string to be substituted for each match
2177 * @return The resulting <tt>String</tt>
2179 * @throws PatternSyntaxException
2180 * if the regular expression's syntax is invalid
2182 * @see java.util.regex.Pattern
2187 public String replaceAll(String regex, String replacement) {
2188 throw new UnsupportedOperationException();
2192 * Replaces each substring of this string that matches the literal target
2193 * sequence with the specified literal replacement sequence. The
2194 * replacement proceeds from the beginning of the string to the end, for
2195 * example, replacing "aa" with "b" in the string "aaa" will result in
2196 * "ba" rather than "ab".
2198 * @param target The sequence of char values to be replaced
2199 * @param replacement The replacement sequence of char values
2200 * @return The resulting string
2201 * @throws NullPointerException if <code>target</code> or
2202 * <code>replacement</code> is <code>null</code>.
2205 public String replace(CharSequence target, CharSequence replacement) {
2206 throw new UnsupportedOperationException("This one should be supported, but without dep on rest of regexp");
2210 * Splits this string around matches of the given
2211 * <a href="../util/regex/Pattern.html#sum">regular expression</a>.
2213 * <p> The array returned by this method contains each substring of this
2214 * string that is terminated by another substring that matches the given
2215 * expression or is terminated by the end of the string. The substrings in
2216 * the array are in the order in which they occur in this string. If the
2217 * expression does not match any part of the input then the resulting array
2218 * has just one element, namely this string.
2220 * <p> The <tt>limit</tt> parameter controls the number of times the
2221 * pattern is applied and therefore affects the length of the resulting
2222 * array. If the limit <i>n</i> is greater than zero then the pattern
2223 * will be applied at most <i>n</i> - 1 times, the array's
2224 * length will be no greater than <i>n</i>, and the array's last entry
2225 * will contain all input beyond the last matched delimiter. If <i>n</i>
2226 * is non-positive then the pattern will be applied as many times as
2227 * possible and the array can have any length. If <i>n</i> is zero then
2228 * the pattern will be applied as many times as possible, the array can
2229 * have any length, and trailing empty strings will be discarded.
2231 * <p> The string <tt>"boo:and:foo"</tt>, for example, yields the
2232 * following results with these parameters:
2234 * <blockquote><table cellpadding=1 cellspacing=0 summary="Split example showing regex, limit, and result">
2240 * <tr><td align=center>:</td>
2241 * <td align=center>2</td>
2242 * <td><tt>{ "boo", "and:foo" }</tt></td></tr>
2243 * <tr><td align=center>:</td>
2244 * <td align=center>5</td>
2245 * <td><tt>{ "boo", "and", "foo" }</tt></td></tr>
2246 * <tr><td align=center>:</td>
2247 * <td align=center>-2</td>
2248 * <td><tt>{ "boo", "and", "foo" }</tt></td></tr>
2249 * <tr><td align=center>o</td>
2250 * <td align=center>5</td>
2251 * <td><tt>{ "b", "", ":and:f", "", "" }</tt></td></tr>
2252 * <tr><td align=center>o</td>
2253 * <td align=center>-2</td>
2254 * <td><tt>{ "b", "", ":and:f", "", "" }</tt></td></tr>
2255 * <tr><td align=center>o</td>
2256 * <td align=center>0</td>
2257 * <td><tt>{ "b", "", ":and:f" }</tt></td></tr>
2258 * </table></blockquote>
2260 * <p> An invocation of this method of the form
2261 * <i>str.</i><tt>split(</tt><i>regex</i><tt>,</tt> <i>n</i><tt>)</tt>
2262 * yields the same result as the expression
2265 * {@link java.util.regex.Pattern}.{@link java.util.regex.Pattern#compile
2266 * compile}<tt>(</tt><i>regex</i><tt>)</tt>.{@link
2267 * java.util.regex.Pattern#split(java.lang.CharSequence,int)
2268 * split}<tt>(</tt><i>str</i><tt>,</tt> <i>n</i><tt>)</tt>
2273 * the delimiting regular expression
2276 * the result threshold, as described above
2278 * @return the array of strings computed by splitting this string
2279 * around matches of the given regular expression
2281 * @throws PatternSyntaxException
2282 * if the regular expression's syntax is invalid
2284 * @see java.util.regex.Pattern
2289 public String[] split(String regex, int limit) {
2290 throw new UnsupportedOperationException("Needs regexp");
2294 * Splits this string around matches of the given <a
2295 * href="../util/regex/Pattern.html#sum">regular expression</a>.
2297 * <p> This method works as if by invoking the two-argument {@link
2298 * #split(String, int) split} method with the given expression and a limit
2299 * argument of zero. Trailing empty strings are therefore not included in
2300 * the resulting array.
2302 * <p> The string <tt>"boo:and:foo"</tt>, for example, yields the following
2303 * results with these expressions:
2305 * <blockquote><table cellpadding=1 cellspacing=0 summary="Split examples showing regex and result">
2310 * <tr><td align=center>:</td>
2311 * <td><tt>{ "boo", "and", "foo" }</tt></td></tr>
2312 * <tr><td align=center>o</td>
2313 * <td><tt>{ "b", "", ":and:f" }</tt></td></tr>
2314 * </table></blockquote>
2318 * the delimiting regular expression
2320 * @return the array of strings computed by splitting this string
2321 * around matches of the given regular expression
2323 * @throws PatternSyntaxException
2324 * if the regular expression's syntax is invalid
2326 * @see java.util.regex.Pattern
2331 public String[] split(String regex) {
2332 return split(regex, 0);
2336 * Converts all of the characters in this <code>String</code> to lower
2337 * case using the rules of the given <code>Locale</code>. Case mapping is based
2338 * on the Unicode Standard version specified by the {@link java.lang.Character Character}
2339 * class. Since case mappings are not always 1:1 char mappings, the resulting
2340 * <code>String</code> may be a different length than the original <code>String</code>.
2342 * Examples of lowercase mappings are in the following table:
2343 * <table border="1" summary="Lowercase mapping examples showing language code of locale, upper case, lower case, and description">
2345 * <th>Language Code of Locale</th>
2346 * <th>Upper Case</th>
2347 * <th>Lower Case</th>
2348 * <th>Description</th>
2351 * <td>tr (Turkish)</td>
2352 * <td>\u0130</td>
2353 * <td>\u0069</td>
2354 * <td>capital letter I with dot above -> small letter i</td>
2357 * <td>tr (Turkish)</td>
2358 * <td>\u0049</td>
2359 * <td>\u0131</td>
2360 * <td>capital letter I -> small letter dotless i </td>
2364 * <td>French Fries</td>
2365 * <td>french fries</td>
2366 * <td>lowercased all chars in String</td>
2370 * <td><img src="doc-files/capiota.gif" alt="capiota"><img src="doc-files/capchi.gif" alt="capchi">
2371 * <img src="doc-files/captheta.gif" alt="captheta"><img src="doc-files/capupsil.gif" alt="capupsil">
2372 * <img src="doc-files/capsigma.gif" alt="capsigma"></td>
2373 * <td><img src="doc-files/iota.gif" alt="iota"><img src="doc-files/chi.gif" alt="chi">
2374 * <img src="doc-files/theta.gif" alt="theta"><img src="doc-files/upsilon.gif" alt="upsilon">
2375 * <img src="doc-files/sigma1.gif" alt="sigma"></td>
2376 * <td>lowercased all chars in String</td>
2380 * @param locale use the case transformation rules for this locale
2381 * @return the <code>String</code>, converted to lowercase.
2382 * @see java.lang.String#toLowerCase()
2383 * @see java.lang.String#toUpperCase()
2384 * @see java.lang.String#toUpperCase(Locale)
2387 // public String toLowerCase(Locale locale) {
2388 // if (locale == null) {
2389 // throw new NullPointerException();
2394 // /* Now check if there are any characters that need to be changed. */
2396 // for (firstUpper = 0 ; firstUpper < count; ) {
2397 // char c = value[offset+firstUpper];
2398 // if ((c >= Character.MIN_HIGH_SURROGATE) &&
2399 // (c <= Character.MAX_HIGH_SURROGATE)) {
2400 // int supplChar = codePointAt(firstUpper);
2401 // if (supplChar != Character.toLowerCase(supplChar)) {
2404 // firstUpper += Character.charCount(supplChar);
2406 // if (c != Character.toLowerCase(c)) {
2415 // char[] result = new char[count];
2416 // int resultOffset = 0; /* result may grow, so i+resultOffset
2417 // * is the write location in result */
2419 // /* Just copy the first few lowerCase characters. */
2420 // System.arraycopy(value, offset, result, 0, firstUpper);
2422 // String lang = locale.getLanguage();
2423 // boolean localeDependent =
2424 // (lang == "tr" || lang == "az" || lang == "lt");
2425 // char[] lowerCharArray;
2429 // for (int i = firstUpper; i < count; i += srcCount) {
2430 // srcChar = (int)value[offset+i];
2431 // if ((char)srcChar >= Character.MIN_HIGH_SURROGATE &&
2432 // (char)srcChar <= Character.MAX_HIGH_SURROGATE) {
2433 // srcChar = codePointAt(i);
2434 // srcCount = Character.charCount(srcChar);
2438 // if (localeDependent || srcChar == '\u03A3') { // GREEK CAPITAL LETTER SIGMA
2439 // lowerChar = ConditionalSpecialCasing.toLowerCaseEx(this, i, locale);
2440 // } else if (srcChar == '\u0130') { // LATIN CAPITAL LETTER I DOT
2441 // lowerChar = Character.ERROR;
2443 // lowerChar = Character.toLowerCase(srcChar);
2445 // if ((lowerChar == Character.ERROR) ||
2446 // (lowerChar >= Character.MIN_SUPPLEMENTARY_CODE_POINT)) {
2447 // if (lowerChar == Character.ERROR) {
2448 // if (!localeDependent && srcChar == '\u0130') {
2450 // ConditionalSpecialCasing.toLowerCaseCharArray(this, i, Locale.ENGLISH);
2453 // ConditionalSpecialCasing.toLowerCaseCharArray(this, i, locale);
2455 // } else if (srcCount == 2) {
2456 // resultOffset += Character.toChars(lowerChar, result, i + resultOffset) - srcCount;
2459 // lowerCharArray = Character.toChars(lowerChar);
2462 // /* Grow result if needed */
2463 // int mapLen = lowerCharArray.length;
2464 // if (mapLen > srcCount) {
2465 // char[] result2 = new char[result.length + mapLen - srcCount];
2466 // System.arraycopy(result, 0, result2, 0,
2467 // i + resultOffset);
2468 // result = result2;
2470 // for (int x=0; x<mapLen; ++x) {
2471 // result[i+resultOffset+x] = lowerCharArray[x];
2473 // resultOffset += (mapLen - srcCount);
2475 // result[i+resultOffset] = (char)lowerChar;
2478 // return new String(0, count+resultOffset, result);
2482 * Converts all of the characters in this <code>String</code> to lower
2483 * case using the rules of the default locale. This is equivalent to calling
2484 * <code>toLowerCase(Locale.getDefault())</code>.
2486 * <b>Note:</b> This method is locale sensitive, and may produce unexpected
2487 * results if used for strings that are intended to be interpreted locale
2489 * Examples are programming language identifiers, protocol keys, and HTML
2491 * For instance, <code>"TITLE".toLowerCase()</code> in a Turkish locale
2492 * returns <code>"t\u005Cu0131tle"</code>, where '\u005Cu0131' is the
2493 * LATIN SMALL LETTER DOTLESS I character.
2494 * To obtain correct results for locale insensitive strings, use
2495 * <code>toLowerCase(Locale.ENGLISH)</code>.
2497 * @return the <code>String</code>, converted to lowercase.
2498 * @see java.lang.String#toLowerCase(Locale)
2500 @JavaScriptBody(args = {}, body = "return this.toLowerCase();")
2501 public String toLowerCase() {
2502 throw new UnsupportedOperationException("Should be supported but without connection to locale");
2506 * Converts all of the characters in this <code>String</code> to upper
2507 * case using the rules of the given <code>Locale</code>. Case mapping is based
2508 * on the Unicode Standard version specified by the {@link java.lang.Character Character}
2509 * class. Since case mappings are not always 1:1 char mappings, the resulting
2510 * <code>String</code> may be a different length than the original <code>String</code>.
2512 * Examples of locale-sensitive and 1:M case mappings are in the following table.
2514 * <table border="1" summary="Examples of locale-sensitive and 1:M case mappings. Shows Language code of locale, lower case, upper case, and description.">
2516 * <th>Language Code of Locale</th>
2517 * <th>Lower Case</th>
2518 * <th>Upper Case</th>
2519 * <th>Description</th>
2522 * <td>tr (Turkish)</td>
2523 * <td>\u0069</td>
2524 * <td>\u0130</td>
2525 * <td>small letter i -> capital letter I with dot above</td>
2528 * <td>tr (Turkish)</td>
2529 * <td>\u0131</td>
2530 * <td>\u0049</td>
2531 * <td>small letter dotless i -> capital letter I</td>
2535 * <td>\u00df</td>
2536 * <td>\u0053 \u0053</td>
2537 * <td>small letter sharp s -> two letters: SS</td>
2541 * <td>Fahrvergnügen</td>
2542 * <td>FAHRVERGNÜGEN</td>
2546 * @param locale use the case transformation rules for this locale
2547 * @return the <code>String</code>, converted to uppercase.
2548 * @see java.lang.String#toUpperCase()
2549 * @see java.lang.String#toLowerCase()
2550 * @see java.lang.String#toLowerCase(Locale)
2553 /* not for javascript
2554 public String toUpperCase(Locale locale) {
2555 if (locale == null) {
2556 throw new NullPointerException();
2561 // Now check if there are any characters that need to be changed.
2563 for (firstLower = 0 ; firstLower < count; ) {
2564 int c = (int)value[offset+firstLower];
2566 if ((c >= Character.MIN_HIGH_SURROGATE) &&
2567 (c <= Character.MAX_HIGH_SURROGATE)) {
2568 c = codePointAt(firstLower);
2569 srcCount = Character.charCount(c);
2573 int upperCaseChar = Character.toUpperCaseEx(c);
2574 if ((upperCaseChar == Character.ERROR) ||
2575 (c != upperCaseChar)) {
2578 firstLower += srcCount;
2583 char[] result = new char[count]; /* may grow *
2584 int resultOffset = 0; /* result may grow, so i+resultOffset
2585 * is the write location in result *
2587 /* Just copy the first few upperCase characters. *
2588 System.arraycopy(value, offset, result, 0, firstLower);
2590 String lang = locale.getLanguage();
2591 boolean localeDependent =
2592 (lang == "tr" || lang == "az" || lang == "lt");
2593 char[] upperCharArray;
2597 for (int i = firstLower; i < count; i += srcCount) {
2598 srcChar = (int)value[offset+i];
2599 if ((char)srcChar >= Character.MIN_HIGH_SURROGATE &&
2600 (char)srcChar <= Character.MAX_HIGH_SURROGATE) {
2601 srcChar = codePointAt(i);
2602 srcCount = Character.charCount(srcChar);
2606 if (localeDependent) {
2607 upperChar = ConditionalSpecialCasing.toUpperCaseEx(this, i, locale);
2609 upperChar = Character.toUpperCaseEx(srcChar);
2611 if ((upperChar == Character.ERROR) ||
2612 (upperChar >= Character.MIN_SUPPLEMENTARY_CODE_POINT)) {
2613 if (upperChar == Character.ERROR) {
2614 if (localeDependent) {
2616 ConditionalSpecialCasing.toUpperCaseCharArray(this, i, locale);
2618 upperCharArray = Character.toUpperCaseCharArray(srcChar);
2620 } else if (srcCount == 2) {
2621 resultOffset += Character.toChars(upperChar, result, i + resultOffset) - srcCount;
2624 upperCharArray = Character.toChars(upperChar);
2627 /* Grow result if needed *
2628 int mapLen = upperCharArray.length;
2629 if (mapLen > srcCount) {
2630 char[] result2 = new char[result.length + mapLen - srcCount];
2631 System.arraycopy(result, 0, result2, 0,
2635 for (int x=0; x<mapLen; ++x) {
2636 result[i+resultOffset+x] = upperCharArray[x];
2638 resultOffset += (mapLen - srcCount);
2640 result[i+resultOffset] = (char)upperChar;
2643 return new String(0, count+resultOffset, result);
2648 * Converts all of the characters in this <code>String</code> to upper
2649 * case using the rules of the default locale. This method is equivalent to
2650 * <code>toUpperCase(Locale.getDefault())</code>.
2652 * <b>Note:</b> This method is locale sensitive, and may produce unexpected
2653 * results if used for strings that are intended to be interpreted locale
2655 * Examples are programming language identifiers, protocol keys, and HTML
2657 * For instance, <code>"title".toUpperCase()</code> in a Turkish locale
2658 * returns <code>"T\u005Cu0130TLE"</code>, where '\u005Cu0130' is the
2659 * LATIN CAPITAL LETTER I WITH DOT ABOVE character.
2660 * To obtain correct results for locale insensitive strings, use
2661 * <code>toUpperCase(Locale.ENGLISH)</code>.
2663 * @return the <code>String</code>, converted to uppercase.
2664 * @see java.lang.String#toUpperCase(Locale)
2666 @JavaScriptBody(args = {}, body = "return this.toUpperCase();")
2667 public String toUpperCase() {
2668 throw new UnsupportedOperationException();
2672 * Returns a copy of the string, with leading and trailing whitespace
2675 * If this <code>String</code> object represents an empty character
2676 * sequence, or the first and last characters of character sequence
2677 * represented by this <code>String</code> object both have codes
2678 * greater than <code>'\u0020'</code> (the space character), then a
2679 * reference to this <code>String</code> object is returned.
2681 * Otherwise, if there is no character with a code greater than
2682 * <code>'\u0020'</code> in the string, then a new
2683 * <code>String</code> object representing an empty string is created
2686 * Otherwise, let <i>k</i> be the index of the first character in the
2687 * string whose code is greater than <code>'\u0020'</code>, and let
2688 * <i>m</i> be the index of the last character in the string whose code
2689 * is greater than <code>'\u0020'</code>. A new <code>String</code>
2690 * object is created, representing the substring of this string that
2691 * begins with the character at index <i>k</i> and ends with the
2692 * character at index <i>m</i>-that is, the result of
2693 * <code>this.substring(<i>k</i>, <i>m</i>+1)</code>.
2695 * This method may be used to trim whitespace (as defined above) from
2696 * the beginning and end of a string.
2698 * @return A copy of this string with leading and trailing white
2699 * space removed, or this string if it has no leading or
2700 * trailing white space.
2702 public String trim() {
2705 int off = offset(); /* avoid getfield opcode */
2706 char[] val = toCharArray(); /* avoid getfield opcode */
2708 while ((st < len) && (val[off + st] <= ' ')) {
2711 while ((st < len) && (val[off + len - 1] <= ' ')) {
2714 return ((st > 0) || (len < length())) ? substring(st, len) : this;
2718 * This object (which is already a string!) is itself returned.
2720 * @return the string itself.
2722 @JavaScriptBody(args = {}, body = "return this.toString();")
2723 public String toString() {
2728 * Converts this string to a new character array.
2730 * @return a newly allocated character array whose length is the length
2731 * of this string and whose contents are initialized to contain
2732 * the character sequence represented by this string.
2734 public char[] toCharArray() {
2735 char result[] = new char[length()];
2736 getChars(0, length(), result, 0);
2741 * Returns a formatted string using the specified format string and
2744 * <p> The locale always used is the one returned by {@link
2745 * java.util.Locale#getDefault() Locale.getDefault()}.
2748 * A <a href="../util/Formatter.html#syntax">format string</a>
2751 * Arguments referenced by the format specifiers in the format
2752 * string. If there are more arguments than format specifiers, the
2753 * extra arguments are ignored. The number of arguments is
2754 * variable and may be zero. The maximum number of arguments is
2755 * limited by the maximum dimension of a Java array as defined by
2756 * <cite>The Java™ Virtual Machine Specification</cite>.
2757 * The behaviour on a
2758 * <tt>null</tt> argument depends on the <a
2759 * href="../util/Formatter.html#syntax">conversion</a>.
2761 * @throws IllegalFormatException
2762 * If a format string contains an illegal syntax, a format
2763 * specifier that is incompatible with the given arguments,
2764 * insufficient arguments given the format string, or other
2765 * illegal conditions. For specification of all possible
2766 * formatting errors, see the <a
2767 * href="../util/Formatter.html#detail">Details</a> section of the
2768 * formatter class specification.
2770 * @throws NullPointerException
2771 * If the <tt>format</tt> is <tt>null</tt>
2773 * @return A formatted string
2775 * @see java.util.Formatter
2778 public static String format(String format, Object ... args) {
2779 throw new UnsupportedOperationException();
2783 * Returns a formatted string using the specified locale, format string,
2787 * The {@linkplain java.util.Locale locale} to apply during
2788 * formatting. If <tt>l</tt> is <tt>null</tt> then no localization
2792 * A <a href="../util/Formatter.html#syntax">format string</a>
2795 * Arguments referenced by the format specifiers in the format
2796 * string. If there are more arguments than format specifiers, the
2797 * extra arguments are ignored. The number of arguments is
2798 * variable and may be zero. The maximum number of arguments is
2799 * limited by the maximum dimension of a Java array as defined by
2800 * <cite>The Java™ Virtual Machine Specification</cite>.
2801 * The behaviour on a
2802 * <tt>null</tt> argument depends on the <a
2803 * href="../util/Formatter.html#syntax">conversion</a>.
2805 * @throws IllegalFormatException
2806 * If a format string contains an illegal syntax, a format
2807 * specifier that is incompatible with the given arguments,
2808 * insufficient arguments given the format string, or other
2809 * illegal conditions. For specification of all possible
2810 * formatting errors, see the <a
2811 * href="../util/Formatter.html#detail">Details</a> section of the
2812 * formatter class specification
2814 * @throws NullPointerException
2815 * If the <tt>format</tt> is <tt>null</tt>
2817 * @return A formatted string
2819 * @see java.util.Formatter
2822 // public static String format(Locale l, String format, Object ... args) {
2823 // return new Formatter(l).format(format, args).toString();
2827 * Returns the string representation of the <code>Object</code> argument.
2829 * @param obj an <code>Object</code>.
2830 * @return if the argument is <code>null</code>, then a string equal to
2831 * <code>"null"</code>; otherwise, the value of
2832 * <code>obj.toString()</code> is returned.
2833 * @see java.lang.Object#toString()
2835 public static String valueOf(Object obj) {
2836 return (obj == null) ? "null" : obj.toString();
2840 * Returns the string representation of the <code>char</code> array
2841 * argument. The contents of the character array are copied; subsequent
2842 * modification of the character array does not affect the newly
2845 * @param data a <code>char</code> array.
2846 * @return a newly allocated string representing the same sequence of
2847 * characters contained in the character array argument.
2849 public static String valueOf(char data[]) {
2850 return new String(data);
2854 * Returns the string representation of a specific subarray of the
2855 * <code>char</code> array argument.
2857 * The <code>offset</code> argument is the index of the first
2858 * character of the subarray. The <code>count</code> argument
2859 * specifies the length of the subarray. The contents of the subarray
2860 * are copied; subsequent modification of the character array does not
2861 * affect the newly created string.
2863 * @param data the character array.
2864 * @param offset the initial offset into the value of the
2865 * <code>String</code>.
2866 * @param count the length of the value of the <code>String</code>.
2867 * @return a string representing the sequence of characters contained
2868 * in the subarray of the character array argument.
2869 * @exception IndexOutOfBoundsException if <code>offset</code> is
2870 * negative, or <code>count</code> is negative, or
2871 * <code>offset+count</code> is larger than
2872 * <code>data.length</code>.
2874 public static String valueOf(char data[], int offset, int count) {
2875 return new String(data, offset, count);
2879 * Returns a String that represents the character sequence in the
2882 * @param data the character array.
2883 * @param offset initial offset of the subarray.
2884 * @param count length of the subarray.
2885 * @return a <code>String</code> that contains the characters of the
2886 * specified subarray of the character array.
2888 public static String copyValueOf(char data[], int offset, int count) {
2889 // All public String constructors now copy the data.
2890 return new String(data, offset, count);
2894 * Returns a String that represents the character sequence in the
2897 * @param data the character array.
2898 * @return a <code>String</code> that contains the characters of the
2901 public static String copyValueOf(char data[]) {
2902 return copyValueOf(data, 0, data.length);
2906 * Returns the string representation of the <code>boolean</code> argument.
2908 * @param b a <code>boolean</code>.
2909 * @return if the argument is <code>true</code>, a string equal to
2910 * <code>"true"</code> is returned; otherwise, a string equal to
2911 * <code>"false"</code> is returned.
2913 public static String valueOf(boolean b) {
2914 return b ? "true" : "false";
2918 * Returns the string representation of the <code>char</code>
2921 * @param c a <code>char</code>.
2922 * @return a string of length <code>1</code> containing
2923 * as its single character the argument <code>c</code>.
2925 public static String valueOf(char c) {
2927 return new String(data, 0, 1);
2931 * Returns the string representation of the <code>int</code> argument.
2933 * The representation is exactly the one returned by the
2934 * <code>Integer.toString</code> method of one argument.
2936 * @param i an <code>int</code>.
2937 * @return a string representation of the <code>int</code> argument.
2938 * @see java.lang.Integer#toString(int, int)
2940 public static String valueOf(int i) {
2941 return Integer.toString(i);
2945 * Returns the string representation of the <code>long</code> argument.
2947 * The representation is exactly the one returned by the
2948 * <code>Long.toString</code> method of one argument.
2950 * @param l a <code>long</code>.
2951 * @return a string representation of the <code>long</code> argument.
2952 * @see java.lang.Long#toString(long)
2954 public static String valueOf(long l) {
2955 return Long.toString(l);
2959 * Returns the string representation of the <code>float</code> argument.
2961 * The representation is exactly the one returned by the
2962 * <code>Float.toString</code> method of one argument.
2964 * @param f a <code>float</code>.
2965 * @return a string representation of the <code>float</code> argument.
2966 * @see java.lang.Float#toString(float)
2968 public static String valueOf(float f) {
2969 return Float.toString(f);
2973 * Returns the string representation of the <code>double</code> argument.
2975 * The representation is exactly the one returned by the
2976 * <code>Double.toString</code> method of one argument.
2978 * @param d a <code>double</code>.
2979 * @return a string representation of the <code>double</code> argument.
2980 * @see java.lang.Double#toString(double)
2982 public static String valueOf(double d) {
2983 return Double.toString(d);
2987 * Returns a canonical representation for the string object.
2989 * A pool of strings, initially empty, is maintained privately by the
2990 * class <code>String</code>.
2992 * When the intern method is invoked, if the pool already contains a
2993 * string equal to this <code>String</code> object as determined by
2994 * the {@link #equals(Object)} method, then the string from the pool is
2995 * returned. Otherwise, this <code>String</code> object is added to the
2996 * pool and a reference to this <code>String</code> object is returned.
2998 * It follows that for any two strings <code>s</code> and <code>t</code>,
2999 * <code>s.intern() == t.intern()</code> is <code>true</code>
3000 * if and only if <code>s.equals(t)</code> is <code>true</code>.
3002 * All literal strings and string-valued constant expressions are
3003 * interned. String literals are defined in section 3.10.5 of the
3004 * <cite>The Java™ Language Specification</cite>.
3006 * @return a string that has the same contents as this string, but is
3007 * guaranteed to be from a pool of unique strings.
3009 public native String intern();