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