jaroslav@49: /*
jaroslav@49: * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
jaroslav@49: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@49: *
jaroslav@49: * This code is free software; you can redistribute it and/or modify it
jaroslav@49: * under the terms of the GNU General Public License version 2 only, as
jaroslav@49: * published by the Free Software Foundation. Oracle designates this
jaroslav@49: * particular file as subject to the "Classpath" exception as provided
jaroslav@49: * by Oracle in the LICENSE file that accompanied this code.
jaroslav@49: *
jaroslav@49: * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@49: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@49: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jaroslav@49: * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@49: * accompanied this code).
jaroslav@49: *
jaroslav@49: * You should have received a copy of the GNU General Public License version
jaroslav@49: * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@49: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@49: *
jaroslav@49: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@49: * or visit www.oracle.com if you need additional information or have any
jaroslav@49: * questions.
jaroslav@49: */
jaroslav@49:
jaroslav@49: package java.lang;
jaroslav@49:
jaroslav@49: import java.io.UnsupportedEncodingException;
jaroslav@49: import java.util.Comparator;
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * The String
class represents character strings. All
jaroslav@49: * string literals in Java programs, such as "abc"
, are
jaroslav@49: * implemented as instances of this class.
jaroslav@49: *
jaroslav@49: * Strings are constant; their values cannot be changed after they jaroslav@49: * are created. String buffers support mutable strings. jaroslav@49: * Because String objects are immutable they can be shared. For example: jaroslav@49: *
jaroslav@49: * String str = "abc"; jaroslav@49: *
jaroslav@49: * is equivalent to: jaroslav@49: *
jaroslav@49: * char data[] = {'a', 'b', 'c'}; jaroslav@49: * String str = new String(data); jaroslav@49: *
jaroslav@49: * Here are some more examples of how strings can be used: jaroslav@49: *
jaroslav@49: *jaroslav@49: * System.out.println("abc"); jaroslav@49: * String cde = "cde"; jaroslav@49: * System.out.println("abc" + cde); jaroslav@49: * String c = "abc".substring(2,3); jaroslav@49: * String d = cde.substring(1, 2); jaroslav@49: *
jaroslav@49: * The class String
includes methods for examining
jaroslav@49: * individual characters of the sequence, for comparing strings, for
jaroslav@49: * searching strings, for extracting substrings, and for creating a
jaroslav@49: * copy of a string with all characters translated to uppercase or to
jaroslav@49: * lowercase. Case mapping is based on the Unicode Standard version
jaroslav@49: * specified by the {@link java.lang.Character Character} class.
jaroslav@49: *
jaroslav@49: * The Java language provides special support for the string
jaroslav@49: * concatenation operator ( + ), and for conversion of
jaroslav@49: * other objects to strings. String concatenation is implemented
jaroslav@49: * through the StringBuilder
(or StringBuffer
)
jaroslav@49: * class and its append
method.
jaroslav@49: * String conversions are implemented through the method
jaroslav@49: * toString
, defined by Object
and
jaroslav@49: * inherited by all classes in Java. For additional information on
jaroslav@49: * string concatenation and conversion, see Gosling, Joy, and Steele,
jaroslav@49: * The Java Language Specification.
jaroslav@49: *
jaroslav@49: *
Unless otherwise noted, passing a null argument to a constructor jaroslav@49: * or method in this class will cause a {@link NullPointerException} to be jaroslav@49: * thrown. jaroslav@49: * jaroslav@49: *
A String
represents a string in the UTF-16 format
jaroslav@49: * in which supplementary characters are represented by surrogate
jaroslav@49: * pairs (see the section Unicode
jaroslav@49: * Character Representations in the Character
class for
jaroslav@49: * more information).
jaroslav@49: * Index values refer to char
code units, so a supplementary
jaroslav@49: * character uses two positions in a String
.
jaroslav@49: *
The The {@code offset} argument is the index of the first byte of the
jaroslav@49: * subarray, and the {@code count} argument specifies the length of the
jaroslav@49: * subarray.
jaroslav@49: *
jaroslav@49: * Each {@code byte} in the subarray is converted to a {@code char} as
jaroslav@49: * specified in the method above.
jaroslav@49: *
jaroslav@49: * @deprecated This method does not properly convert bytes into characters.
jaroslav@49: * As of JDK 1.1, the preferred way to do this is via the
jaroslav@49: * {@code String} constructors that take a {@link
jaroslav@49: * java.nio.charset.Charset}, charset name, or that use the platform's
jaroslav@49: * default charset.
jaroslav@49: *
jaroslav@49: * @param ascii
jaroslav@49: * The bytes to be converted to characters
jaroslav@49: *
jaroslav@49: * @param hibyte
jaroslav@49: * The top 8 bits of each 16-bit Unicode code unit
jaroslav@49: *
jaroslav@49: * @param offset
jaroslav@49: * The initial offset
jaroslav@49: * @param count
jaroslav@49: * The length
jaroslav@49: *
jaroslav@49: * @throws IndexOutOfBoundsException
jaroslav@49: * If the {@code offset} or {@code count} argument is invalid
jaroslav@49: *
jaroslav@49: * @see #String(byte[], int)
jaroslav@49: * @see #String(byte[], int, int, java.lang.String)
jaroslav@49: * @see #String(byte[], int, int, java.nio.charset.Charset)
jaroslav@49: * @see #String(byte[], int, int)
jaroslav@49: * @see #String(byte[], java.lang.String)
jaroslav@49: * @see #String(byte[], java.nio.charset.Charset)
jaroslav@49: * @see #String(byte[])
jaroslav@49: */
jaroslav@49: @Deprecated
jaroslav@49: public String(byte ascii[], int hibyte, int offset, int count) {
jaroslav@49: checkBounds(ascii, offset, count);
jaroslav@49: char value[] = new char[count];
jaroslav@49:
jaroslav@49: if (hibyte == 0) {
jaroslav@49: for (int i = count ; i-- > 0 ;) {
jaroslav@49: value[i] = (char) (ascii[i + offset] & 0xff);
jaroslav@49: }
jaroslav@49: } else {
jaroslav@49: hibyte <<= 8;
jaroslav@49: for (int i = count ; i-- > 0 ;) {
jaroslav@49: value[i] = (char) (hibyte | (ascii[i + offset] & 0xff));
jaroslav@49: }
jaroslav@49: }
jaroslav@49: this.offset = 0;
jaroslav@49: this.count = count;
jaroslav@49: this.value = value;
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Allocates a new {@code String} containing characters constructed from
jaroslav@49: * an array of 8-bit integer values. Each character cin the
jaroslav@49: * resulting string is constructed from the corresponding component
jaroslav@49: * b in the byte array such that:
jaroslav@49: *
jaroslav@49: * The behavior of this constructor when the given bytes are not valid
jaroslav@49: * in the given charset is unspecified. The {@link
jaroslav@49: * java.nio.charset.CharsetDecoder} class should be used when more control
jaroslav@49: * over the decoding process is required.
jaroslav@49: *
jaroslav@49: * @param bytes
jaroslav@49: * The bytes to be decoded into characters
jaroslav@49: *
jaroslav@49: * @param offset
jaroslav@49: * The index of the first byte to decode
jaroslav@49: *
jaroslav@49: * @param length
jaroslav@49: * The number of bytes to decode
jaroslav@49:
jaroslav@49: * @param charsetName
jaroslav@49: * The name of a supported {@linkplain java.nio.charset.Charset
jaroslav@49: * charset}
jaroslav@49: *
jaroslav@49: * @throws UnsupportedEncodingException
jaroslav@49: * If the named charset is not supported
jaroslav@49: *
jaroslav@49: * @throws IndexOutOfBoundsException
jaroslav@49: * If the {@code offset} and {@code length} arguments index
jaroslav@49: * characters outside the bounds of the {@code bytes} array
jaroslav@49: *
jaroslav@49: * @since JDK1.1
jaroslav@49: */
jaroslav@49: public String(byte bytes[], int offset, int length, String charsetName)
jaroslav@49: throws UnsupportedEncodingException
jaroslav@49: {
jaroslav@49: if (charsetName == null)
jaroslav@49: throw new NullPointerException("charsetName");
jaroslav@49: checkBounds(bytes, offset, length);
jaroslav@49: char[] v = StringCoding.decode(charsetName, bytes, offset, length);
jaroslav@49: this.offset = 0;
jaroslav@49: this.count = v.length;
jaroslav@49: this.value = v;
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Constructs a new {@code String} by decoding the specified subarray of
jaroslav@49: * bytes using the specified {@linkplain java.nio.charset.Charset charset}.
jaroslav@49: * The length of the new {@code String} is a function of the charset, and
jaroslav@49: * hence may not be equal to the length of the subarray.
jaroslav@49: *
jaroslav@49: * This method always replaces malformed-input and unmappable-character
jaroslav@49: * sequences with this charset's default replacement string. The {@link
jaroslav@49: * java.nio.charset.CharsetDecoder} class should be used when more control
jaroslav@49: * over the decoding process is required.
jaroslav@49: *
jaroslav@49: * @param bytes
jaroslav@49: * The bytes to be decoded into characters
jaroslav@49: *
jaroslav@49: * @param offset
jaroslav@49: * The index of the first byte to decode
jaroslav@49: *
jaroslav@49: * @param length
jaroslav@49: * The number of bytes to decode
jaroslav@49: *
jaroslav@49: * @param charset
jaroslav@49: * The {@linkplain java.nio.charset.Charset charset} to be used to
jaroslav@49: * decode the {@code bytes}
jaroslav@49: *
jaroslav@49: * @throws IndexOutOfBoundsException
jaroslav@49: * If the {@code offset} and {@code length} arguments index
jaroslav@49: * characters outside the bounds of the {@code bytes} array
jaroslav@49: *
jaroslav@49: * @since 1.6
jaroslav@49: */
jaroslav@61: /* don't want dependnecy on Charset
jaroslav@49: public String(byte bytes[], int offset, int length, Charset charset) {
jaroslav@49: if (charset == null)
jaroslav@49: throw new NullPointerException("charset");
jaroslav@49: checkBounds(bytes, offset, length);
jaroslav@49: char[] v = StringCoding.decode(charset, bytes, offset, length);
jaroslav@49: this.offset = 0;
jaroslav@49: this.count = v.length;
jaroslav@49: this.value = v;
jaroslav@49: }
jaroslav@61: */
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Constructs a new {@code String} by decoding the specified array of bytes
jaroslav@49: * using the specified {@linkplain java.nio.charset.Charset charset}. The
jaroslav@49: * length of the new {@code String} is a function of the charset, and hence
jaroslav@49: * may not be equal to the length of the byte array.
jaroslav@49: *
jaroslav@49: * The behavior of this constructor when the given bytes are not valid
jaroslav@49: * in the given charset is unspecified. The {@link
jaroslav@49: * java.nio.charset.CharsetDecoder} class should be used when more control
jaroslav@49: * over the decoding process is required.
jaroslav@49: *
jaroslav@49: * @param bytes
jaroslav@49: * The bytes to be decoded into characters
jaroslav@49: *
jaroslav@49: * @param charsetName
jaroslav@49: * The name of a supported {@linkplain java.nio.charset.Charset
jaroslav@49: * charset}
jaroslav@49: *
jaroslav@49: * @throws UnsupportedEncodingException
jaroslav@49: * If the named charset is not supported
jaroslav@49: *
jaroslav@49: * @since JDK1.1
jaroslav@49: */
jaroslav@49: public String(byte bytes[], String charsetName)
jaroslav@49: throws UnsupportedEncodingException
jaroslav@49: {
jaroslav@49: this(bytes, 0, bytes.length, charsetName);
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Constructs a new {@code String} by decoding the specified array of
jaroslav@49: * bytes using the specified {@linkplain java.nio.charset.Charset charset}.
jaroslav@49: * The length of the new {@code String} is a function of the charset, and
jaroslav@49: * hence may not be equal to the length of the byte array.
jaroslav@49: *
jaroslav@49: * This method always replaces malformed-input and unmappable-character
jaroslav@49: * sequences with this charset's default replacement string. The {@link
jaroslav@49: * java.nio.charset.CharsetDecoder} class should be used when more control
jaroslav@49: * over the decoding process is required.
jaroslav@49: *
jaroslav@49: * @param bytes
jaroslav@49: * The bytes to be decoded into characters
jaroslav@49: *
jaroslav@49: * @param charset
jaroslav@49: * The {@linkplain java.nio.charset.Charset charset} to be used to
jaroslav@49: * decode the {@code bytes}
jaroslav@49: *
jaroslav@49: * @since 1.6
jaroslav@49: */
jaroslav@61: /* don't want dep on Charset
jaroslav@49: public String(byte bytes[], Charset charset) {
jaroslav@49: this(bytes, 0, bytes.length, charset);
jaroslav@49: }
jaroslav@61: */
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Constructs a new {@code String} by decoding the specified subarray of
jaroslav@49: * bytes using the platform's default charset. The length of the new
jaroslav@49: * {@code String} is a function of the charset, and hence may not be equal
jaroslav@49: * to the length of the subarray.
jaroslav@49: *
jaroslav@49: * The behavior of this constructor when the given bytes are not valid
jaroslav@49: * in the default charset is unspecified. The {@link
jaroslav@49: * java.nio.charset.CharsetDecoder} class should be used when more control
jaroslav@49: * over the decoding process is required.
jaroslav@49: *
jaroslav@49: * @param bytes
jaroslav@49: * The bytes to be decoded into characters
jaroslav@49: *
jaroslav@49: * @param offset
jaroslav@49: * The index of the first byte to decode
jaroslav@49: *
jaroslav@49: * @param length
jaroslav@49: * The number of bytes to decode
jaroslav@49: *
jaroslav@49: * @throws IndexOutOfBoundsException
jaroslav@49: * If the {@code offset} and the {@code length} arguments index
jaroslav@49: * characters outside the bounds of the {@code bytes} array
jaroslav@49: *
jaroslav@49: * @since JDK1.1
jaroslav@49: */
jaroslav@49: public String(byte bytes[], int offset, int length) {
jaroslav@49: checkBounds(bytes, offset, length);
jaroslav@49: char[] v = StringCoding.decode(bytes, offset, length);
jaroslav@49: this.offset = 0;
jaroslav@49: this.count = v.length;
jaroslav@49: this.value = v;
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Constructs a new {@code String} by decoding the specified array of bytes
jaroslav@49: * using the platform's default charset. The length of the new {@code
jaroslav@49: * String} is a function of the charset, and hence may not be equal to the
jaroslav@49: * length of the byte array.
jaroslav@49: *
jaroslav@49: * The behavior of this constructor when the given bytes are not valid
jaroslav@49: * in the default charset is unspecified. The {@link
jaroslav@49: * java.nio.charset.CharsetDecoder} class should be used when more control
jaroslav@49: * over the decoding process is required.
jaroslav@49: *
jaroslav@49: * @param bytes
jaroslav@49: * The bytes to be decoded into characters
jaroslav@49: *
jaroslav@49: * @since JDK1.1
jaroslav@49: */
jaroslav@49: public String(byte bytes[]) {
jaroslav@49: this(bytes, 0, bytes.length);
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Allocates a new string that contains the sequence of characters
jaroslav@49: * currently contained in the string buffer argument. The contents of the
jaroslav@49: * string buffer are copied; subsequent modification of the string buffer
jaroslav@49: * does not affect the newly created string.
jaroslav@49: *
jaroslav@49: * @param buffer
jaroslav@49: * A {@code StringBuffer}
jaroslav@49: */
jaroslav@49: public String(StringBuffer buffer) {
jaroslav@49: String result = buffer.toString();
jaroslav@49: this.value = result.value;
jaroslav@49: this.count = result.count;
jaroslav@49: this.offset = result.offset;
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Allocates a new string that contains the sequence of characters
jaroslav@49: * currently contained in the string builder argument. The contents of the
jaroslav@49: * string builder are copied; subsequent modification of the string builder
jaroslav@49: * does not affect the newly created string.
jaroslav@49: *
jaroslav@49: * This constructor is provided to ease migration to {@code
jaroslav@49: * StringBuilder}. Obtaining a string from a string builder via the {@code
jaroslav@49: * toString} method is likely to run faster and is generally preferred.
jaroslav@49: *
jaroslav@49: * @param builder
jaroslav@49: * A {@code StringBuilder}
jaroslav@49: *
jaroslav@49: * @since 1.5
jaroslav@49: */
jaroslav@49: public String(StringBuilder builder) {
jaroslav@49: String result = builder.toString();
jaroslav@49: this.value = result.value;
jaroslav@49: this.count = result.count;
jaroslav@49: this.offset = result.offset;
jaroslav@49: }
jaroslav@49:
jaroslav@49:
jaroslav@49: // Package private constructor which shares value array for speed.
jaroslav@49: String(int offset, int count, char value[]) {
jaroslav@49: this.value = value;
jaroslav@49: this.offset = offset;
jaroslav@49: this.count = count;
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Returns the length of this string.
jaroslav@49: * The length is equal to the number of Unicode
jaroslav@49: * code units in the string.
jaroslav@49: *
jaroslav@49: * @return the length of the sequence of characters represented by this
jaroslav@49: * object.
jaroslav@49: */
jaroslav@49: public int length() {
jaroslav@49: return count;
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Returns true if, and only if, {@link #length()} is 0.
jaroslav@49: *
jaroslav@49: * @return true if {@link #length()} is 0, otherwise
jaroslav@49: * false
jaroslav@49: *
jaroslav@49: * @since 1.6
jaroslav@49: */
jaroslav@49: public boolean isEmpty() {
jaroslav@49: return count == 0;
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Returns the If the If the If the
jaroslav@49: * The first character to be copied is at index The first character to be copied is at index {@code srcBegin}; the
jaroslav@49: * last character to be copied is at index {@code srcEnd-1}. The total
jaroslav@49: * number of characters to be copied is {@code srcEnd-srcBegin}. The
jaroslav@49: * characters, converted to bytes, are copied into the subarray of {@code
jaroslav@49: * dst} starting at index {@code dstBegin} and ending at index:
jaroslav@49: *
jaroslav@49: * The behavior of this method when this string cannot be encoded in
jaroslav@49: * the given charset is unspecified. The {@link
jaroslav@49: * java.nio.charset.CharsetEncoder} class should be used when more control
jaroslav@49: * over the encoding process is required.
jaroslav@49: *
jaroslav@49: * @param charsetName
jaroslav@49: * The name of a supported {@linkplain java.nio.charset.Charset
jaroslav@49: * charset}
jaroslav@49: *
jaroslav@49: * @return The resultant byte array
jaroslav@49: *
jaroslav@49: * @throws UnsupportedEncodingException
jaroslav@49: * If the named charset is not supported
jaroslav@49: *
jaroslav@49: * @since JDK1.1
jaroslav@49: */
jaroslav@49: public byte[] getBytes(String charsetName)
jaroslav@49: throws UnsupportedEncodingException
jaroslav@49: {
jaroslav@49: if (charsetName == null) throw new NullPointerException();
jaroslav@49: return StringCoding.encode(charsetName, value, offset, count);
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Encodes this {@code String} into a sequence of bytes using the given
jaroslav@49: * {@linkplain java.nio.charset.Charset charset}, storing the result into a
jaroslav@49: * new byte array.
jaroslav@49: *
jaroslav@49: * This method always replaces malformed-input and unmappable-character
jaroslav@49: * sequences with this charset's default replacement byte array. The
jaroslav@49: * {@link java.nio.charset.CharsetEncoder} class should be used when more
jaroslav@49: * control over the encoding process is required.
jaroslav@49: *
jaroslav@49: * @param charset
jaroslav@49: * The {@linkplain java.nio.charset.Charset} to be used to encode
jaroslav@49: * the {@code String}
jaroslav@49: *
jaroslav@49: * @return The resultant byte array
jaroslav@49: *
jaroslav@49: * @since 1.6
jaroslav@49: */
jaroslav@61: /* don't want dep on Charset
jaroslav@49: public byte[] getBytes(Charset charset) {
jaroslav@49: if (charset == null) throw new NullPointerException();
jaroslav@49: return StringCoding.encode(charset, value, offset, count);
jaroslav@49: }
jaroslav@61: */
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Encodes this {@code String} into a sequence of bytes using the
jaroslav@49: * platform's default charset, storing the result into a new byte array.
jaroslav@49: *
jaroslav@49: * The behavior of this method when this string cannot be encoded in
jaroslav@49: * the default charset is unspecified. The {@link
jaroslav@49: * java.nio.charset.CharsetEncoder} class should be used when more control
jaroslav@49: * over the encoding process is required.
jaroslav@49: *
jaroslav@49: * @return The resultant byte array
jaroslav@49: *
jaroslav@49: * @since JDK1.1
jaroslav@49: */
jaroslav@49: public byte[] getBytes() {
jaroslav@49: return StringCoding.encode(value, offset, count);
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Compares this string to the specified object. The result is {@code
jaroslav@49: * true} if and only if the argument is not {@code null} and is a {@code
jaroslav@49: * String} object that represents the same sequence of characters as this
jaroslav@49: * object.
jaroslav@49: *
jaroslav@49: * @param anObject
jaroslav@49: * The object to compare this {@code String} against
jaroslav@49: *
jaroslav@49: * @return {@code true} if the given object represents a {@code String}
jaroslav@49: * equivalent to this string, {@code false} otherwise
jaroslav@49: *
jaroslav@49: * @see #compareTo(String)
jaroslav@49: * @see #equalsIgnoreCase(String)
jaroslav@49: */
jaroslav@49: public boolean equals(Object anObject) {
jaroslav@49: if (this == anObject) {
jaroslav@49: return true;
jaroslav@49: }
jaroslav@49: if (anObject instanceof String) {
jaroslav@49: String anotherString = (String)anObject;
jaroslav@49: int n = count;
jaroslav@49: if (n == anotherString.count) {
jaroslav@49: char v1[] = value;
jaroslav@49: char v2[] = anotherString.value;
jaroslav@49: int i = offset;
jaroslav@49: int j = anotherString.offset;
jaroslav@49: while (n-- != 0) {
jaroslav@49: if (v1[i++] != v2[j++])
jaroslav@49: return false;
jaroslav@49: }
jaroslav@49: return true;
jaroslav@49: }
jaroslav@49: }
jaroslav@49: return false;
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Compares this string to the specified {@code StringBuffer}. The result
jaroslav@49: * is {@code true} if and only if this {@code String} represents the same
jaroslav@49: * sequence of characters as the specified {@code StringBuffer}.
jaroslav@49: *
jaroslav@49: * @param sb
jaroslav@49: * The {@code StringBuffer} to compare this {@code String} against
jaroslav@49: *
jaroslav@49: * @return {@code true} if this {@code String} represents the same
jaroslav@49: * sequence of characters as the specified {@code StringBuffer},
jaroslav@49: * {@code false} otherwise
jaroslav@49: *
jaroslav@49: * @since 1.4
jaroslav@49: */
jaroslav@49: public boolean contentEquals(StringBuffer sb) {
jaroslav@49: synchronized(sb) {
jaroslav@49: return contentEquals((CharSequence)sb);
jaroslav@49: }
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Compares this string to the specified {@code CharSequence}. The result
jaroslav@49: * is {@code true} if and only if this {@code String} represents the same
jaroslav@49: * sequence of char values as the specified sequence.
jaroslav@49: *
jaroslav@49: * @param cs
jaroslav@49: * The sequence to compare this {@code String} against
jaroslav@49: *
jaroslav@49: * @return {@code true} if this {@code String} represents the same
jaroslav@49: * sequence of char values as the specified sequence, {@code
jaroslav@49: * false} otherwise
jaroslav@49: *
jaroslav@49: * @since 1.5
jaroslav@49: */
jaroslav@49: public boolean contentEquals(CharSequence cs) {
jaroslav@49: if (count != cs.length())
jaroslav@49: return false;
jaroslav@49: // Argument is a StringBuffer, StringBuilder
jaroslav@49: if (cs instanceof AbstractStringBuilder) {
jaroslav@49: char v1[] = value;
jaroslav@49: char v2[] = ((AbstractStringBuilder)cs).getValue();
jaroslav@49: int i = offset;
jaroslav@49: int j = 0;
jaroslav@49: int n = count;
jaroslav@49: while (n-- != 0) {
jaroslav@49: if (v1[i++] != v2[j++])
jaroslav@49: return false;
jaroslav@49: }
jaroslav@49: return true;
jaroslav@49: }
jaroslav@49: // Argument is a String
jaroslav@49: if (cs.equals(this))
jaroslav@49: return true;
jaroslav@49: // Argument is a generic CharSequence
jaroslav@49: char v1[] = value;
jaroslav@49: int i = offset;
jaroslav@49: int j = 0;
jaroslav@49: int n = count;
jaroslav@49: while (n-- != 0) {
jaroslav@49: if (v1[i++] != cs.charAt(j++))
jaroslav@49: return false;
jaroslav@49: }
jaroslav@49: return true;
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Compares this {@code String} to another {@code String}, ignoring case
jaroslav@49: * considerations. Two strings are considered equal ignoring case if they
jaroslav@49: * are of the same length and corresponding characters in the two strings
jaroslav@49: * are equal ignoring case.
jaroslav@49: *
jaroslav@49: * Two characters {@code c1} and {@code c2} are considered the same
jaroslav@49: * ignoring case if at least one of the following is true:
jaroslav@49: *
jaroslav@49: * This is the definition of lexicographic ordering. If two strings are
jaroslav@49: * different, then either they have different characters at some index
jaroslav@49: * that is a valid index for both strings, or their lengths are different,
jaroslav@49: * or both. If they have different characters at one or more index
jaroslav@49: * positions, let k be the smallest such index; then the string
jaroslav@49: * whose character at position k has the smaller value, as
jaroslav@49: * determined by using the < operator, lexicographically precedes the
jaroslav@49: * other string. In this case,
jaroslav@49: * Note that this Comparator does not take locale into account,
jaroslav@49: * and will result in an unsatisfactory ordering for certain locales.
jaroslav@49: * The java.text package provides Collators to allow
jaroslav@49: * locale-sensitive ordering.
jaroslav@49: *
jaroslav@49: * @see java.text.Collator#compare(String, String)
jaroslav@49: * @since 1.2
jaroslav@49: */
jaroslav@49: public static final Comparator
jaroslav@49: * Note that this method does not take locale into account,
jaroslav@49: * and will result in an unsatisfactory ordering for certain locales.
jaroslav@49: * The java.text package provides collators to allow
jaroslav@49: * locale-sensitive ordering.
jaroslav@49: *
jaroslav@49: * @param str the
jaroslav@49: * A substring of this String object is compared to a substring
jaroslav@49: * of the argument other. The result is true if these substrings
jaroslav@49: * represent identical character sequences. The substring of this
jaroslav@49: * String object to be compared begins at index toffset
jaroslav@49: * and has length len. The substring of other to be compared
jaroslav@49: * begins at index ooffset and has length len. The
jaroslav@49: * result is false if and only if at least one of the following
jaroslav@49: * is true:
jaroslav@49: *
jaroslav@49: * A substring of this String object is compared to a substring
jaroslav@49: * of the argument other. The result is true if these
jaroslav@49: * substrings represent character sequences that are the same, ignoring
jaroslav@49: * case if and only if ignoreCase is true. The substring of
jaroslav@49: * this String object to be compared begins at index
jaroslav@49: * toffset and has length len. The substring of
jaroslav@49: * other to be compared begins at index ooffset and
jaroslav@49: * has length len. The result is false if and only if
jaroslav@49: * at least one of the following is true:
jaroslav@49: *
jaroslav@49: * If a character with value
jaroslav@49: * There is no restriction on the value of All indices are specified in All indices are specified in The returned index is the smallest value k for which:
jaroslav@49: * The returned index is the smallest value k for which:
jaroslav@49: * The returned index is the largest value k for which:
jaroslav@49: * The returned index is the largest value k for which:
jaroslav@49: *
jaroslav@49: * Examples:
jaroslav@49: *
jaroslav@49: * Examples:
jaroslav@49: * An invocation of this method of the form
jaroslav@49: *
jaroslav@49: *
jaroslav@49: * If the length of the argument string is
jaroslav@49: * Examples:
jaroslav@49: *
jaroslav@49: * If the character
jaroslav@49: * Examples:
jaroslav@49: * An invocation of this method of the form
jaroslav@49: * str.matches(regex) yields exactly the
jaroslav@49: * same result as the expression
jaroslav@49: *
jaroslav@49: * An invocation of this method of the form
jaroslav@49: * str.replaceFirst(regex, repl)
jaroslav@49: * yields exactly the same result as the expression
jaroslav@49: *
jaroslav@49: *
jaroslav@49: * Note that backslashes (\) and dollar signs ($) in the
jaroslav@49: * replacement string may cause the results to be different than if it were
jaroslav@49: * being treated as a literal replacement string; see
jaroslav@49: * {@link java.util.regex.Matcher#replaceFirst}.
jaroslav@49: * Use {@link java.util.regex.Matcher#quoteReplacement} to suppress the special
jaroslav@49: * meaning of these characters, if desired.
jaroslav@49: *
jaroslav@49: * @param regex
jaroslav@49: * the regular expression to which this string is to be matched
jaroslav@49: * @param replacement
jaroslav@49: * the string to be substituted for the first match
jaroslav@49: *
jaroslav@49: * @return The resulting String
jaroslav@49: *
jaroslav@49: * @throws PatternSyntaxException
jaroslav@49: * if the regular expression's syntax is invalid
jaroslav@49: *
jaroslav@49: * @see java.util.regex.Pattern
jaroslav@49: *
jaroslav@49: * @since 1.4
jaroslav@49: * @spec JSR-51
jaroslav@49: */
jaroslav@49: public String replaceFirst(String regex, String replacement) {
jaroslav@64: throw new UnsupportedOperationException();
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Replaces each substring of this string that matches the given regular expression with the
jaroslav@49: * given replacement.
jaroslav@49: *
jaroslav@49: * An invocation of this method of the form
jaroslav@49: * str.replaceAll(regex, repl)
jaroslav@49: * yields exactly the same result as the expression
jaroslav@49: *
jaroslav@49: *
jaroslav@49: * Note that backslashes (\) and dollar signs ($) in the
jaroslav@49: * replacement string may cause the results to be different than if it were
jaroslav@49: * being treated as a literal replacement string; see
jaroslav@49: * {@link java.util.regex.Matcher#replaceAll Matcher.replaceAll}.
jaroslav@49: * Use {@link java.util.regex.Matcher#quoteReplacement} to suppress the special
jaroslav@49: * meaning of these characters, if desired.
jaroslav@49: *
jaroslav@49: * @param regex
jaroslav@49: * the regular expression to which this string is to be matched
jaroslav@49: * @param replacement
jaroslav@49: * the string to be substituted for each match
jaroslav@49: *
jaroslav@49: * @return The resulting String
jaroslav@49: *
jaroslav@49: * @throws PatternSyntaxException
jaroslav@49: * if the regular expression's syntax is invalid
jaroslav@49: *
jaroslav@49: * @see java.util.regex.Pattern
jaroslav@49: *
jaroslav@49: * @since 1.4
jaroslav@49: * @spec JSR-51
jaroslav@49: */
jaroslav@49: public String replaceAll(String regex, String replacement) {
jaroslav@64: throw new UnsupportedOperationException();
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Replaces each substring of this string that matches the literal target
jaroslav@49: * sequence with the specified literal replacement sequence. The
jaroslav@49: * replacement proceeds from the beginning of the string to the end, for
jaroslav@49: * example, replacing "aa" with "b" in the string "aaa" will result in
jaroslav@49: * "ba" rather than "ab".
jaroslav@49: *
jaroslav@49: * @param target The sequence of char values to be replaced
jaroslav@49: * @param replacement The replacement sequence of char values
jaroslav@49: * @return The resulting string
jaroslav@49: * @throws NullPointerException if The array returned by this method contains each substring of this
jaroslav@49: * string that is terminated by another substring that matches the given
jaroslav@49: * expression or is terminated by the end of the string. The substrings in
jaroslav@49: * the array are in the order in which they occur in this string. If the
jaroslav@49: * expression does not match any part of the input then the resulting array
jaroslav@49: * has just one element, namely this string.
jaroslav@49: *
jaroslav@49: * The limit parameter controls the number of times the
jaroslav@49: * pattern is applied and therefore affects the length of the resulting
jaroslav@49: * array. If the limit n is greater than zero then the pattern
jaroslav@49: * will be applied at most n - 1 times, the array's
jaroslav@49: * length will be no greater than n, and the array's last entry
jaroslav@49: * will contain all input beyond the last matched delimiter. If n
jaroslav@49: * is non-positive then the pattern will be applied as many times as
jaroslav@49: * possible and the array can have any length. If n is zero then
jaroslav@49: * the pattern will be applied as many times as possible, the array can
jaroslav@49: * have any length, and trailing empty strings will be discarded.
jaroslav@49: *
jaroslav@49: * The string "boo:and:foo", for example, yields the
jaroslav@49: * following results with these parameters:
jaroslav@49: *
jaroslav@49: * An invocation of this method of the form
jaroslav@49: * str.split(regex, n)
jaroslav@49: * yields the same result as the expression
jaroslav@49: *
jaroslav@49: * This method works as if by invoking the two-argument {@link
jaroslav@49: * #split(String, int) split} method with the given expression and a limit
jaroslav@49: * argument of zero. Trailing empty strings are therefore not included in
jaroslav@49: * the resulting array.
jaroslav@49: *
jaroslav@49: * The string "boo:and:foo", for example, yields the following
jaroslav@49: * results with these expressions:
jaroslav@49: *
jaroslav@49: *
jaroslav@49: * Examples of lowercase mappings are in the following table:
jaroslav@49: *
jaroslav@49: * Note: This method is locale sensitive, and may produce unexpected
jaroslav@49: * results if used for strings that are intended to be interpreted locale
jaroslav@49: * independently.
jaroslav@49: * Examples are programming language identifiers, protocol keys, and HTML
jaroslav@49: * tags.
jaroslav@49: * For instance,
jaroslav@49: * @return the
jaroslav@49: * Examples of locale-sensitive and 1:M case mappings are in the following table.
jaroslav@49: *
jaroslav@49: *
jaroslav@49: * Note: This method is locale sensitive, and may produce unexpected
jaroslav@49: * results if used for strings that are intended to be interpreted locale
jaroslav@49: * independently.
jaroslav@49: * Examples are programming language identifiers, protocol keys, and HTML
jaroslav@49: * tags.
jaroslav@49: * For instance,
jaroslav@49: * @return the
jaroslav@49: * If this
jaroslav@49: * Otherwise, if there is no character with a code greater than
jaroslav@49: *
jaroslav@49: * Otherwise, let k be the index of the first character in the
jaroslav@49: * string whose code is greater than
jaroslav@49: * This method may be used to trim whitespace (as defined above) from
jaroslav@49: * the beginning and end of a string.
jaroslav@49: *
jaroslav@49: * @return A copy of this string with leading and trailing white
jaroslav@49: * space removed, or this string if it has no leading or
jaroslav@49: * trailing white space.
jaroslav@49: */
jaroslav@49: public String trim() {
jaroslav@49: int len = count;
jaroslav@49: int st = 0;
jaroslav@49: int off = offset; /* avoid getfield opcode */
jaroslav@49: char[] val = value; /* avoid getfield opcode */
jaroslav@49:
jaroslav@49: while ((st < len) && (val[off + st] <= ' ')) {
jaroslav@49: st++;
jaroslav@49: }
jaroslav@49: while ((st < len) && (val[off + len - 1] <= ' ')) {
jaroslav@49: len--;
jaroslav@49: }
jaroslav@49: return ((st > 0) || (len < count)) ? substring(st, len) : this;
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * This object (which is already a string!) is itself returned.
jaroslav@49: *
jaroslav@49: * @return the string itself.
jaroslav@49: */
jaroslav@49: public String toString() {
jaroslav@49: return this;
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Converts this string to a new character array.
jaroslav@49: *
jaroslav@49: * @return a newly allocated character array whose length is the length
jaroslav@49: * of this string and whose contents are initialized to contain
jaroslav@49: * the character sequence represented by this string.
jaroslav@49: */
jaroslav@49: public char[] toCharArray() {
jaroslav@49: char result[] = new char[count];
jaroslav@49: getChars(0, count, result, 0);
jaroslav@49: return result;
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Returns a formatted string using the specified format string and
jaroslav@49: * arguments.
jaroslav@49: *
jaroslav@49: * The locale always used is the one returned by {@link
jaroslav@49: * java.util.Locale#getDefault() Locale.getDefault()}.
jaroslav@49: *
jaroslav@49: * @param format
jaroslav@49: * A format string
jaroslav@49: *
jaroslav@49: * @param args
jaroslav@49: * Arguments referenced by the format specifiers in the format
jaroslav@49: * string. If there are more arguments than format specifiers, the
jaroslav@49: * extra arguments are ignored. The number of arguments is
jaroslav@49: * variable and may be zero. The maximum number of arguments is
jaroslav@49: * limited by the maximum dimension of a Java array as defined by
jaroslav@49: * The Java™ Virtual Machine Specification.
jaroslav@49: * The behaviour on a
jaroslav@49: * null argument depends on the conversion.
jaroslav@49: *
jaroslav@49: * @throws IllegalFormatException
jaroslav@49: * If a format string contains an illegal syntax, a format
jaroslav@49: * specifier that is incompatible with the given arguments,
jaroslav@49: * insufficient arguments given the format string, or other
jaroslav@49: * illegal conditions. For specification of all possible
jaroslav@49: * formatting errors, see the Details section of the
jaroslav@49: * formatter class specification.
jaroslav@49: *
jaroslav@49: * @throws NullPointerException
jaroslav@49: * If the format is null
jaroslav@49: *
jaroslav@49: * @return A formatted string
jaroslav@49: *
jaroslav@49: * @see java.util.Formatter
jaroslav@49: * @since 1.5
jaroslav@49: */
jaroslav@49: public static String format(String format, Object ... args) {
jaroslav@64: throw new UnsupportedOperationException();
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Returns a formatted string using the specified locale, format string,
jaroslav@49: * and arguments.
jaroslav@49: *
jaroslav@49: * @param l
jaroslav@49: * The {@linkplain java.util.Locale locale} to apply during
jaroslav@49: * formatting. If l is null then no localization
jaroslav@49: * is applied.
jaroslav@49: *
jaroslav@49: * @param format
jaroslav@49: * A format string
jaroslav@49: *
jaroslav@49: * @param args
jaroslav@49: * Arguments referenced by the format specifiers in the format
jaroslav@49: * string. If there are more arguments than format specifiers, the
jaroslav@49: * extra arguments are ignored. The number of arguments is
jaroslav@49: * variable and may be zero. The maximum number of arguments is
jaroslav@49: * limited by the maximum dimension of a Java array as defined by
jaroslav@49: * The Java™ Virtual Machine Specification.
jaroslav@49: * The behaviour on a
jaroslav@49: * null argument depends on the conversion.
jaroslav@49: *
jaroslav@49: * @throws IllegalFormatException
jaroslav@49: * If a format string contains an illegal syntax, a format
jaroslav@49: * specifier that is incompatible with the given arguments,
jaroslav@49: * insufficient arguments given the format string, or other
jaroslav@49: * illegal conditions. For specification of all possible
jaroslav@49: * formatting errors, see the Details section of the
jaroslav@49: * formatter class specification
jaroslav@49: *
jaroslav@49: * @throws NullPointerException
jaroslav@49: * If the format is null
jaroslav@49: *
jaroslav@49: * @return A formatted string
jaroslav@49: *
jaroslav@49: * @see java.util.Formatter
jaroslav@49: * @since 1.5
jaroslav@49: */
jaroslav@61: // public static String format(Locale l, String format, Object ... args) {
jaroslav@61: // return new Formatter(l).format(format, args).toString();
jaroslav@61: // }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Returns the string representation of the
jaroslav@49: * The
jaroslav@49: * The representation is exactly the one returned by the
jaroslav@49: *
jaroslav@49: * The representation is exactly the one returned by the
jaroslav@49: *
jaroslav@49: * The representation is exactly the one returned by the
jaroslav@49: *
jaroslav@49: * The representation is exactly the one returned by the
jaroslav@49: *
jaroslav@49: * A pool of strings, initially empty, is maintained privately by the
jaroslav@49: * class
jaroslav@49: * When the intern method is invoked, if the pool already contains a
jaroslav@49: * string equal to this
jaroslav@49: * It follows that for any two strings
jaroslav@49: * All literal strings and string-valued constant expressions are
jaroslav@49: * interned. String literals are defined in section 3.10.5 of the
jaroslav@49: * The Java™ Language Specification.
jaroslav@49: *
jaroslav@49: * @return a string that has the same contents as this string, but is
jaroslav@49: * guaranteed to be from a pool of unique strings.
jaroslav@49: */
jaroslav@49: public native String intern();
jaroslav@49:
jaroslav@61: static char[] copyOfRange(char[] original, int from, int to) {
jaroslav@61: int newLength = to - from;
jaroslav@61: if (newLength < 0) {
jaroslav@61: throw new IllegalArgumentException(from + " > " + to);
jaroslav@61: }
jaroslav@61: char[] copy = new char[newLength];
jaroslav@72: arraycopy(original, from, copy, 0,
jaroslav@61: Math.min(original.length - from, newLength));
jaroslav@61: return copy;
jaroslav@61: }
jaroslav@61: static char[] copyOf(char[] original, int newLength) {
jaroslav@61: char[] copy = new char[newLength];
jaroslav@72: arraycopy(original, 0, copy, 0,
jaroslav@61: Math.min(original.length, newLength));
jaroslav@61: return copy;
jaroslav@61: }
jaroslav@72: static void arraycopy(
jaroslav@72: char[] value, int srcBegin, char[] dst, int dstBegin, int count
jaroslav@72: ) {
jaroslav@72: System.arraycopy(value, srcBegin, dst, dstBegin, count);
jaroslav@72: }
jaroslav@61:
jaroslav@49: }
String
class provides methods for dealing with
jaroslav@49: * Unicode code points (i.e., characters), in addition to those for
jaroslav@49: * dealing with Unicode code units (i.e., char
values).
jaroslav@49: *
jaroslav@49: * @author Lee Boynton
jaroslav@49: * @author Arthur van Hoff
jaroslav@49: * @author Martin Buchholz
jaroslav@49: * @author Ulf Zibis
jaroslav@49: * @see java.lang.Object#toString()
jaroslav@49: * @see java.lang.StringBuffer
jaroslav@49: * @see java.lang.StringBuilder
jaroslav@49: * @see java.nio.charset.Charset
jaroslav@49: * @since JDK1.0
jaroslav@49: */
jaroslav@49:
jaroslav@49: public final class String
jaroslav@49: implements java.io.Serializable, Comparable
jaroslav@49: *
jaroslav@49: * The String is written by method TC_STRING
(utf String)
jaroslav@49: * DataOutput.writeUTF
.
jaroslav@49: * A new handle is generated to refer to all future references to the
jaroslav@49: * string instance within the stream.
jaroslav@49: */
jaroslav@65: // private static final ObjectStreamField[] serialPersistentFields =
jaroslav@65: // new ObjectStreamField[0];
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Initializes a newly created {@code String} object so that it represents
jaroslav@49: * an empty character sequence. Note that use of this constructor is
jaroslav@49: * unnecessary since Strings are immutable.
jaroslav@49: */
jaroslav@49: public String() {
jaroslav@49: this.offset = 0;
jaroslav@49: this.count = 0;
jaroslav@49: this.value = new char[0];
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Initializes a newly created {@code String} object so that it represents
jaroslav@49: * the same sequence of characters as the argument; in other words, the
jaroslav@49: * newly created string is a copy of the argument string. Unless an
jaroslav@49: * explicit copy of {@code original} is needed, use of this constructor is
jaroslav@49: * unnecessary since Strings are immutable.
jaroslav@49: *
jaroslav@49: * @param original
jaroslav@49: * A {@code String}
jaroslav@49: */
jaroslav@49: public String(String original) {
jaroslav@49: int size = original.count;
jaroslav@49: char[] originalValue = original.value;
jaroslav@49: char[] v;
jaroslav@49: if (originalValue.length > size) {
jaroslav@49: // The array representing the String is bigger than the new
jaroslav@49: // String itself. Perhaps this constructor is being called
jaroslav@49: // in order to trim the baggage, so make a copy of the array.
jaroslav@49: int off = original.offset;
jaroslav@61: v = copyOfRange(originalValue, off, off+size);
jaroslav@49: } else {
jaroslav@49: // The array representing the String is the same
jaroslav@49: // size as the String, so no point in making a copy.
jaroslav@49: v = originalValue;
jaroslav@49: }
jaroslav@49: this.offset = 0;
jaroslav@49: this.count = size;
jaroslav@49: this.value = v;
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Allocates a new {@code String} so that it represents the sequence of
jaroslav@49: * characters currently contained in the character array argument. The
jaroslav@49: * contents of the character array are copied; subsequent modification of
jaroslav@49: * the character array does not affect the newly created string.
jaroslav@49: *
jaroslav@49: * @param value
jaroslav@49: * The initial value of the string
jaroslav@49: */
jaroslav@49: public String(char value[]) {
jaroslav@49: int size = value.length;
jaroslav@49: this.offset = 0;
jaroslav@49: this.count = size;
jaroslav@61: this.value = copyOf(value, size);
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Allocates a new {@code String} that contains characters from a subarray
jaroslav@49: * of the character array argument. The {@code offset} argument is the
jaroslav@49: * index of the first character of the subarray and the {@code count}
jaroslav@49: * argument specifies the length of the subarray. The contents of the
jaroslav@49: * subarray are copied; subsequent modification of the character array does
jaroslav@49: * not affect the newly created string.
jaroslav@49: *
jaroslav@49: * @param value
jaroslav@49: * Array that is the source of characters
jaroslav@49: *
jaroslav@49: * @param offset
jaroslav@49: * The initial offset
jaroslav@49: *
jaroslav@49: * @param count
jaroslav@49: * The length
jaroslav@49: *
jaroslav@49: * @throws IndexOutOfBoundsException
jaroslav@49: * If the {@code offset} and {@code count} arguments index
jaroslav@49: * characters outside the bounds of the {@code value} array
jaroslav@49: */
jaroslav@49: public String(char value[], int offset, int count) {
jaroslav@49: if (offset < 0) {
jaroslav@49: throw new StringIndexOutOfBoundsException(offset);
jaroslav@49: }
jaroslav@49: if (count < 0) {
jaroslav@49: throw new StringIndexOutOfBoundsException(count);
jaroslav@49: }
jaroslav@49: // Note: offset or count might be near -1>>>1.
jaroslav@49: if (offset > value.length - count) {
jaroslav@49: throw new StringIndexOutOfBoundsException(offset + count);
jaroslav@49: }
jaroslav@49: this.offset = 0;
jaroslav@49: this.count = count;
jaroslav@61: this.value = copyOfRange(value, offset, offset+count);
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Allocates a new {@code String} that contains characters from a subarray
jaroslav@49: * of the Unicode code point array
jaroslav@49: * argument. The {@code offset} argument is the index of the first code
jaroslav@49: * point of the subarray and the {@code count} argument specifies the
jaroslav@49: * length of the subarray. The contents of the subarray are converted to
jaroslav@49: * {@code char}s; subsequent modification of the {@code int} array does not
jaroslav@49: * affect the newly created string.
jaroslav@49: *
jaroslav@49: * @param codePoints
jaroslav@49: * Array that is the source of Unicode code points
jaroslav@49: *
jaroslav@49: * @param offset
jaroslav@49: * The initial offset
jaroslav@49: *
jaroslav@49: * @param count
jaroslav@49: * The length
jaroslav@49: *
jaroslav@49: * @throws IllegalArgumentException
jaroslav@49: * If any invalid Unicode code point is found in {@code
jaroslav@49: * codePoints}
jaroslav@49: *
jaroslav@49: * @throws IndexOutOfBoundsException
jaroslav@49: * If the {@code offset} and {@code count} arguments index
jaroslav@49: * characters outside the bounds of the {@code codePoints} array
jaroslav@49: *
jaroslav@49: * @since 1.5
jaroslav@49: */
jaroslav@49: public String(int[] codePoints, int offset, int count) {
jaroslav@49: if (offset < 0) {
jaroslav@49: throw new StringIndexOutOfBoundsException(offset);
jaroslav@49: }
jaroslav@49: if (count < 0) {
jaroslav@49: throw new StringIndexOutOfBoundsException(count);
jaroslav@49: }
jaroslav@49: // Note: offset or count might be near -1>>>1.
jaroslav@49: if (offset > codePoints.length - count) {
jaroslav@49: throw new StringIndexOutOfBoundsException(offset + count);
jaroslav@49: }
jaroslav@49:
jaroslav@49: final int end = offset + count;
jaroslav@49:
jaroslav@49: // Pass 1: Compute precise size of char[]
jaroslav@49: int n = count;
jaroslav@49: for (int i = offset; i < end; i++) {
jaroslav@49: int c = codePoints[i];
jaroslav@49: if (Character.isBmpCodePoint(c))
jaroslav@49: continue;
jaroslav@49: else if (Character.isValidCodePoint(c))
jaroslav@49: n++;
jaroslav@49: else throw new IllegalArgumentException(Integer.toString(c));
jaroslav@49: }
jaroslav@49:
jaroslav@49: // Pass 2: Allocate and fill in char[]
jaroslav@49: final char[] v = new char[n];
jaroslav@49:
jaroslav@49: for (int i = offset, j = 0; i < end; i++, j++) {
jaroslav@49: int c = codePoints[i];
jaroslav@49: if (Character.isBmpCodePoint(c))
jaroslav@49: v[j] = (char) c;
jaroslav@49: else
jaroslav@49: Character.toSurrogates(c, v, j++);
jaroslav@49: }
jaroslav@49:
jaroslav@49: this.value = v;
jaroslav@49: this.count = n;
jaroslav@49: this.offset = 0;
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Allocates a new {@code String} constructed from a subarray of an array
jaroslav@49: * of 8-bit integer values.
jaroslav@49: *
jaroslav@49: *
jaroslav@49: *
jaroslav@49: * @deprecated This method does not properly convert bytes into
jaroslav@49: * characters. As of JDK 1.1, the preferred way to do this is via the
jaroslav@49: * {@code String} constructors that take a {@link
jaroslav@49: * java.nio.charset.Charset}, charset name, or that use the platform's
jaroslav@49: * default charset.
jaroslav@49: *
jaroslav@49: * @param ascii
jaroslav@49: * The bytes to be converted to characters
jaroslav@49: *
jaroslav@49: * @param hibyte
jaroslav@49: * The top 8 bits of each 16-bit Unicode code unit
jaroslav@49: *
jaroslav@49: * @see #String(byte[], int, int, java.lang.String)
jaroslav@49: * @see #String(byte[], int, int, java.nio.charset.Charset)
jaroslav@49: * @see #String(byte[], int, int)
jaroslav@49: * @see #String(byte[], java.lang.String)
jaroslav@49: * @see #String(byte[], java.nio.charset.Charset)
jaroslav@49: * @see #String(byte[])
jaroslav@49: */
jaroslav@49: @Deprecated
jaroslav@49: public String(byte ascii[], int hibyte) {
jaroslav@49: this(ascii, hibyte, 0, ascii.length);
jaroslav@49: }
jaroslav@49:
jaroslav@49: /* Common private utility method used to bounds check the byte array
jaroslav@49: * and requested offset & length values used by the String(byte[],..)
jaroslav@49: * constructors.
jaroslav@49: */
jaroslav@49: private static void checkBounds(byte[] bytes, int offset, int length) {
jaroslav@49: if (length < 0)
jaroslav@49: throw new StringIndexOutOfBoundsException(length);
jaroslav@49: if (offset < 0)
jaroslav@49: throw new StringIndexOutOfBoundsException(offset);
jaroslav@49: if (offset > bytes.length - length)
jaroslav@49: throw new StringIndexOutOfBoundsException(offset + length);
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Constructs a new {@code String} by decoding the specified subarray of
jaroslav@49: * bytes using the specified charset. The length of the new {@code String}
jaroslav@49: * is a function of the charset, and hence may not be equal to the length
jaroslav@49: * of the subarray.
jaroslav@49: *
jaroslav@49: *
jaroslav@49: * c == (char)(((hibyte & 0xff) << 8)
jaroslav@49: * | (b & 0xff))
jaroslav@49: *
char
value at the
jaroslav@49: * specified index. An index ranges from 0
to
jaroslav@49: * length() - 1
. The first char
value of the sequence
jaroslav@49: * is at index 0
, the next at index 1
,
jaroslav@49: * and so on, as for array indexing.
jaroslav@49: *
jaroslav@49: * char
value specified by the index is a
jaroslav@49: * surrogate, the surrogate
jaroslav@49: * value is returned.
jaroslav@49: *
jaroslav@49: * @param index the index of the char
value.
jaroslav@49: * @return the char
value at the specified index of this string.
jaroslav@49: * The first char
value is at index 0
.
jaroslav@49: * @exception IndexOutOfBoundsException if the index
jaroslav@49: * argument is negative or not less than the length of this
jaroslav@49: * string.
jaroslav@49: */
jaroslav@49: public char charAt(int index) {
jaroslav@49: if ((index < 0) || (index >= count)) {
jaroslav@49: throw new StringIndexOutOfBoundsException(index);
jaroslav@49: }
jaroslav@49: return value[index + offset];
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Returns the character (Unicode code point) at the specified
jaroslav@49: * index. The index refers to char
values
jaroslav@49: * (Unicode code units) and ranges from 0
to
jaroslav@49: * {@link #length()} - 1
.
jaroslav@49: *
jaroslav@49: * char
value specified at the given index
jaroslav@49: * is in the high-surrogate range, the following index is less
jaroslav@49: * than the length of this String
, and the
jaroslav@49: * char
value at the following index is in the
jaroslav@49: * low-surrogate range, then the supplementary code point
jaroslav@49: * corresponding to this surrogate pair is returned. Otherwise,
jaroslav@49: * the char
value at the given index is returned.
jaroslav@49: *
jaroslav@49: * @param index the index to the char
values
jaroslav@49: * @return the code point value of the character at the
jaroslav@49: * index
jaroslav@49: * @exception IndexOutOfBoundsException if the index
jaroslav@49: * argument is negative or not less than the length of this
jaroslav@49: * string.
jaroslav@49: * @since 1.5
jaroslav@49: */
jaroslav@49: public int codePointAt(int index) {
jaroslav@49: if ((index < 0) || (index >= count)) {
jaroslav@49: throw new StringIndexOutOfBoundsException(index);
jaroslav@49: }
jaroslav@49: return Character.codePointAtImpl(value, offset + index, offset + count);
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Returns the character (Unicode code point) before the specified
jaroslav@49: * index. The index refers to char
values
jaroslav@49: * (Unicode code units) and ranges from 1
to {@link
jaroslav@49: * CharSequence#length() length}.
jaroslav@49: *
jaroslav@49: * char
value at (index - 1)
jaroslav@49: * is in the low-surrogate range, (index - 2)
is not
jaroslav@49: * negative, and the char
value at (index -
jaroslav@49: * 2)
is in the high-surrogate range, then the
jaroslav@49: * supplementary code point value of the surrogate pair is
jaroslav@49: * returned. If the char
value at index -
jaroslav@49: * 1
is an unpaired low-surrogate or a high-surrogate, the
jaroslav@49: * surrogate value is returned.
jaroslav@49: *
jaroslav@49: * @param index the index following the code point that should be returned
jaroslav@49: * @return the Unicode code point value before the given index.
jaroslav@49: * @exception IndexOutOfBoundsException if the index
jaroslav@49: * argument is less than 1 or greater than the length
jaroslav@49: * of this string.
jaroslav@49: * @since 1.5
jaroslav@49: */
jaroslav@49: public int codePointBefore(int index) {
jaroslav@49: int i = index - 1;
jaroslav@49: if ((i < 0) || (i >= count)) {
jaroslav@49: throw new StringIndexOutOfBoundsException(index);
jaroslav@49: }
jaroslav@49: return Character.codePointBeforeImpl(value, offset + index, offset);
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Returns the number of Unicode code points in the specified text
jaroslav@49: * range of this String
. The text range begins at the
jaroslav@49: * specified beginIndex
and extends to the
jaroslav@49: * char
at index endIndex - 1
. Thus the
jaroslav@49: * length (in char
s) of the text range is
jaroslav@49: * endIndex-beginIndex
. Unpaired surrogates within
jaroslav@49: * the text range count as one code point each.
jaroslav@49: *
jaroslav@49: * @param beginIndex the index to the first char
of
jaroslav@49: * the text range.
jaroslav@49: * @param endIndex the index after the last char
of
jaroslav@49: * the text range.
jaroslav@49: * @return the number of Unicode code points in the specified text
jaroslav@49: * range
jaroslav@49: * @exception IndexOutOfBoundsException if the
jaroslav@49: * beginIndex
is negative, or endIndex
jaroslav@49: * is larger than the length of this String
, or
jaroslav@49: * beginIndex
is larger than endIndex
.
jaroslav@49: * @since 1.5
jaroslav@49: */
jaroslav@49: public int codePointCount(int beginIndex, int endIndex) {
jaroslav@49: if (beginIndex < 0 || endIndex > count || beginIndex > endIndex) {
jaroslav@49: throw new IndexOutOfBoundsException();
jaroslav@49: }
jaroslav@49: return Character.codePointCountImpl(value, offset+beginIndex, endIndex-beginIndex);
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Returns the index within this String
that is
jaroslav@49: * offset from the given index
by
jaroslav@49: * codePointOffset
code points. Unpaired surrogates
jaroslav@49: * within the text range given by index
and
jaroslav@49: * codePointOffset
count as one code point each.
jaroslav@49: *
jaroslav@49: * @param index the index to be offset
jaroslav@49: * @param codePointOffset the offset in code points
jaroslav@49: * @return the index within this String
jaroslav@49: * @exception IndexOutOfBoundsException if index
jaroslav@49: * is negative or larger then the length of this
jaroslav@49: * String
, or if codePointOffset
is positive
jaroslav@49: * and the substring starting with index
has fewer
jaroslav@49: * than codePointOffset
code points,
jaroslav@49: * or if codePointOffset
is negative and the substring
jaroslav@49: * before index
has fewer than the absolute value
jaroslav@49: * of codePointOffset
code points.
jaroslav@49: * @since 1.5
jaroslav@49: */
jaroslav@49: public int offsetByCodePoints(int index, int codePointOffset) {
jaroslav@49: if (index < 0 || index > count) {
jaroslav@49: throw new IndexOutOfBoundsException();
jaroslav@49: }
jaroslav@49: return Character.offsetByCodePointsImpl(value, offset, count,
jaroslav@49: offset+index, codePointOffset) - offset;
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Copy characters from this string into dst starting at dstBegin.
jaroslav@49: * This method doesn't perform any range checking.
jaroslav@49: */
jaroslav@49: void getChars(char dst[], int dstBegin) {
jaroslav@72: arraycopy(value, offset, dst, dstBegin, count);
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Copies characters from this string into the destination character
jaroslav@49: * array.
jaroslav@49: * srcBegin
;
jaroslav@49: * the last character to be copied is at index srcEnd-1
jaroslav@49: * (thus the total number of characters to be copied is
jaroslav@49: * srcEnd-srcBegin
). The characters are copied into the
jaroslav@49: * subarray of dst
starting at index dstBegin
jaroslav@49: * and ending at index:
jaroslav@49: *
jaroslav@49: *
jaroslav@49: * @param srcBegin index of the first character in the string
jaroslav@49: * to copy.
jaroslav@49: * @param srcEnd index after the last character in the string
jaroslav@49: * to copy.
jaroslav@49: * @param dst the destination array.
jaroslav@49: * @param dstBegin the start offset in the destination array.
jaroslav@49: * @exception IndexOutOfBoundsException If any of the following
jaroslav@49: * is true:
jaroslav@49: *
jaroslav@49: * dstbegin + (srcEnd-srcBegin) - 1
jaroslav@49: *
jaroslav@49: */
jaroslav@49: public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {
jaroslav@49: if (srcBegin < 0) {
jaroslav@49: throw new StringIndexOutOfBoundsException(srcBegin);
jaroslav@49: }
jaroslav@49: if (srcEnd > count) {
jaroslav@49: throw new StringIndexOutOfBoundsException(srcEnd);
jaroslav@49: }
jaroslav@49: if (srcBegin > srcEnd) {
jaroslav@49: throw new StringIndexOutOfBoundsException(srcEnd - srcBegin);
jaroslav@49: }
jaroslav@72: arraycopy(value, offset + srcBegin, dst, dstBegin,
jaroslav@49: srcEnd - srcBegin);
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Copies characters from this string into the destination byte array. Each
jaroslav@49: * byte receives the 8 low-order bits of the corresponding character. The
jaroslav@49: * eight high-order bits of each character are not copied and do not
jaroslav@49: * participate in the transfer in any way.
jaroslav@49: *
jaroslav@49: * srcBegin
is negative.
jaroslav@49: * srcBegin
is greater than srcEnd
jaroslav@49: * srcEnd
is greater than the length of this
jaroslav@49: * string
jaroslav@49: * dstBegin
is negative
jaroslav@49: * dstBegin+(srcEnd-srcBegin)
is larger than
jaroslav@49: * dst.length
jaroslav@49: *
jaroslav@49: * @deprecated This method does not properly convert characters into
jaroslav@49: * bytes. As of JDK 1.1, the preferred way to do this is via the
jaroslav@49: * {@link #getBytes()} method, which uses the platform's default charset.
jaroslav@49: *
jaroslav@49: * @param srcBegin
jaroslav@49: * Index of the first character in the string to copy
jaroslav@49: *
jaroslav@49: * @param srcEnd
jaroslav@49: * Index after the last character in the string to copy
jaroslav@49: *
jaroslav@49: * @param dst
jaroslav@49: * The destination array
jaroslav@49: *
jaroslav@49: * @param dstBegin
jaroslav@49: * The start offset in the destination array
jaroslav@49: *
jaroslav@49: * @throws IndexOutOfBoundsException
jaroslav@49: * If any of the following is true:
jaroslav@49: *
jaroslav@49: * dstbegin + (srcEnd-srcBegin) - 1
jaroslav@49: *
jaroslav@49: *
jaroslav@49: */
jaroslav@49: @Deprecated
jaroslav@49: public void getBytes(int srcBegin, int srcEnd, byte dst[], int dstBegin) {
jaroslav@49: if (srcBegin < 0) {
jaroslav@49: throw new StringIndexOutOfBoundsException(srcBegin);
jaroslav@49: }
jaroslav@49: if (srcEnd > count) {
jaroslav@49: throw new StringIndexOutOfBoundsException(srcEnd);
jaroslav@49: }
jaroslav@49: if (srcBegin > srcEnd) {
jaroslav@49: throw new StringIndexOutOfBoundsException(srcEnd - srcBegin);
jaroslav@49: }
jaroslav@49: int j = dstBegin;
jaroslav@49: int n = offset + srcEnd;
jaroslav@49: int i = offset + srcBegin;
jaroslav@49: char[] val = value; /* avoid getfield opcode */
jaroslav@49:
jaroslav@49: while (i < n) {
jaroslav@49: dst[j++] = (byte)val[i++];
jaroslav@49: }
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Encodes this {@code String} into a sequence of bytes using the named
jaroslav@49: * charset, storing the result into a new byte array.
jaroslav@49: *
jaroslav@49: *
jaroslav@49: *
jaroslav@49: *
jaroslav@49: * @param anotherString
jaroslav@49: * The {@code String} to compare this {@code String} against
jaroslav@49: *
jaroslav@49: * @return {@code true} if the argument is not {@code null} and it
jaroslav@49: * represents an equivalent {@code String} ignoring case; {@code
jaroslav@49: * false} otherwise
jaroslav@49: *
jaroslav@49: * @see #equals(Object)
jaroslav@49: */
jaroslav@49: public boolean equalsIgnoreCase(String anotherString) {
jaroslav@49: return (this == anotherString) ? true :
jaroslav@49: (anotherString != null) && (anotherString.count == count) &&
jaroslav@49: regionMatches(true, 0, anotherString, 0, count);
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Compares two strings lexicographically.
jaroslav@49: * The comparison is based on the Unicode value of each character in
jaroslav@49: * the strings. The character sequence represented by this
jaroslav@49: * String
object is compared lexicographically to the
jaroslav@49: * character sequence represented by the argument string. The result is
jaroslav@49: * a negative integer if this String
object
jaroslav@49: * lexicographically precedes the argument string. The result is a
jaroslav@49: * positive integer if this String
object lexicographically
jaroslav@49: * follows the argument string. The result is zero if the strings
jaroslav@49: * are equal; compareTo
returns 0
exactly when
jaroslav@49: * the {@link #equals(Object)} method would return true
.
jaroslav@49: * compareTo
returns the
jaroslav@49: * difference of the two character values at position k
in
jaroslav@49: * the two string -- that is, the value:
jaroslav@49: *
jaroslav@49: * If there is no index position at which they differ, then the shorter
jaroslav@49: * string lexicographically precedes the longer string. In this case,
jaroslav@49: *
jaroslav@49: * this.charAt(k)-anotherString.charAt(k)
jaroslav@49: *
compareTo
returns the difference of the lengths of the
jaroslav@49: * strings -- that is, the value:
jaroslav@49: *
jaroslav@49: *
jaroslav@49: * @param anotherString the
jaroslav@49: * this.length()-anotherString.length()
jaroslav@49: *
String
to be compared.
jaroslav@49: * @return the value 0
if the argument string is equal to
jaroslav@49: * this string; a value less than 0
if this string
jaroslav@49: * is lexicographically less than the string argument; and a
jaroslav@49: * value greater than 0
if this string is
jaroslav@49: * lexicographically greater than the string argument.
jaroslav@49: */
jaroslav@49: public int compareTo(String anotherString) {
jaroslav@49: int len1 = count;
jaroslav@49: int len2 = anotherString.count;
jaroslav@49: int n = Math.min(len1, len2);
jaroslav@49: char v1[] = value;
jaroslav@49: char v2[] = anotherString.value;
jaroslav@49: int i = offset;
jaroslav@49: int j = anotherString.offset;
jaroslav@49:
jaroslav@49: if (i == j) {
jaroslav@49: int k = i;
jaroslav@49: int lim = n + i;
jaroslav@49: while (k < lim) {
jaroslav@49: char c1 = v1[k];
jaroslav@49: char c2 = v2[k];
jaroslav@49: if (c1 != c2) {
jaroslav@49: return c1 - c2;
jaroslav@49: }
jaroslav@49: k++;
jaroslav@49: }
jaroslav@49: } else {
jaroslav@49: while (n-- != 0) {
jaroslav@49: char c1 = v1[i++];
jaroslav@49: char c2 = v2[j++];
jaroslav@49: if (c1 != c2) {
jaroslav@49: return c1 - c2;
jaroslav@49: }
jaroslav@49: }
jaroslav@49: }
jaroslav@49: return len1 - len2;
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * A Comparator that orders String
objects as by
jaroslav@49: * compareToIgnoreCase
. This comparator is serializable.
jaroslav@49: * compareTo
with normalized versions of the strings
jaroslav@49: * where case differences have been eliminated by calling
jaroslav@49: * Character.toLowerCase(Character.toUpperCase(character))
on
jaroslav@49: * each character.
jaroslav@49: * String
to be compared.
jaroslav@49: * @return a negative integer, zero, or a positive integer as the
jaroslav@49: * specified String is greater than, equal to, or less
jaroslav@49: * than this String, ignoring case considerations.
jaroslav@49: * @see java.text.Collator#compare(String, String)
jaroslav@49: * @since 1.2
jaroslav@49: */
jaroslav@49: public int compareToIgnoreCase(String str) {
jaroslav@49: return CASE_INSENSITIVE_ORDER.compare(this, str);
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Tests if two string regions are equal.
jaroslav@49: *
jaroslav@49: *
jaroslav@49: * @param toffset the starting offset of the subregion in this string.
jaroslav@49: * @param other the string argument.
jaroslav@49: * @param ooffset the starting offset of the subregion in the string
jaroslav@49: * argument.
jaroslav@49: * @param len the number of characters to compare.
jaroslav@49: * @return true
if the specified subregion of this string
jaroslav@49: * exactly matches the specified subregion of the string argument;
jaroslav@49: * false
otherwise.
jaroslav@49: */
jaroslav@49: public boolean regionMatches(int toffset, String other, int ooffset,
jaroslav@49: int len) {
jaroslav@49: char ta[] = value;
jaroslav@49: int to = offset + toffset;
jaroslav@49: char pa[] = other.value;
jaroslav@49: int po = other.offset + ooffset;
jaroslav@49: // Note: toffset, ooffset, or len might be near -1>>>1.
jaroslav@49: if ((ooffset < 0) || (toffset < 0) || (toffset > (long)count - len)
jaroslav@49: || (ooffset > (long)other.count - len)) {
jaroslav@49: return false;
jaroslav@49: }
jaroslav@49: while (len-- > 0) {
jaroslav@49: if (ta[to++] != pa[po++]) {
jaroslav@49: return false;
jaroslav@49: }
jaroslav@49: }
jaroslav@49: return true;
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Tests if two string regions are equal.
jaroslav@49: *
jaroslav@49: *
jaroslav@49: * @param ignoreCase if
jaroslav@49: *
jaroslav@49: * this.charAt(toffset+k) != other.charAt(ooffset+k)
jaroslav@49: *
jaroslav@49: * and:
jaroslav@49: *
jaroslav@49: * Character.toLowerCase(this.charAt(toffset+k)) !=
jaroslav@49: Character.toLowerCase(other.charAt(ooffset+k))
jaroslav@49: *
jaroslav@49: *
jaroslav@49: * Character.toUpperCase(this.charAt(toffset+k)) !=
jaroslav@49: * Character.toUpperCase(other.charAt(ooffset+k))
jaroslav@49: *
true
, ignore case when comparing
jaroslav@49: * characters.
jaroslav@49: * @param toffset the starting offset of the subregion in this
jaroslav@49: * string.
jaroslav@49: * @param other the string argument.
jaroslav@49: * @param ooffset the starting offset of the subregion in the string
jaroslav@49: * argument.
jaroslav@49: * @param len the number of characters to compare.
jaroslav@49: * @return true
if the specified subregion of this string
jaroslav@49: * matches the specified subregion of the string argument;
jaroslav@49: * false
otherwise. Whether the matching is exact
jaroslav@49: * or case insensitive depends on the ignoreCase
jaroslav@49: * argument.
jaroslav@49: */
jaroslav@49: public boolean regionMatches(boolean ignoreCase, int toffset,
jaroslav@49: String other, int ooffset, int len) {
jaroslav@49: char ta[] = value;
jaroslav@49: int to = offset + toffset;
jaroslav@49: char pa[] = other.value;
jaroslav@49: int po = other.offset + ooffset;
jaroslav@49: // Note: toffset, ooffset, or len might be near -1>>>1.
jaroslav@49: if ((ooffset < 0) || (toffset < 0) || (toffset > (long)count - len) ||
jaroslav@49: (ooffset > (long)other.count - len)) {
jaroslav@49: return false;
jaroslav@49: }
jaroslav@49: while (len-- > 0) {
jaroslav@49: char c1 = ta[to++];
jaroslav@49: char c2 = pa[po++];
jaroslav@49: if (c1 == c2) {
jaroslav@49: continue;
jaroslav@49: }
jaroslav@49: if (ignoreCase) {
jaroslav@49: // If characters don't match but case may be ignored,
jaroslav@49: // try converting both characters to uppercase.
jaroslav@49: // If the results match, then the comparison scan should
jaroslav@49: // continue.
jaroslav@49: char u1 = Character.toUpperCase(c1);
jaroslav@49: char u2 = Character.toUpperCase(c2);
jaroslav@49: if (u1 == u2) {
jaroslav@49: continue;
jaroslav@49: }
jaroslav@49: // Unfortunately, conversion to uppercase does not work properly
jaroslav@49: // for the Georgian alphabet, which has strange rules about case
jaroslav@49: // conversion. So we need to make one last check before
jaroslav@49: // exiting.
jaroslav@49: if (Character.toLowerCase(u1) == Character.toLowerCase(u2)) {
jaroslav@49: continue;
jaroslav@49: }
jaroslav@49: }
jaroslav@49: return false;
jaroslav@49: }
jaroslav@49: return true;
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Tests if the substring of this string beginning at the
jaroslav@49: * specified index starts with the specified prefix.
jaroslav@49: *
jaroslav@49: * @param prefix the prefix.
jaroslav@49: * @param toffset where to begin looking in this string.
jaroslav@49: * @return true
if the character sequence represented by the
jaroslav@49: * argument is a prefix of the substring of this object starting
jaroslav@49: * at index toffset
; false
otherwise.
jaroslav@49: * The result is false
if toffset
is
jaroslav@49: * negative or greater than the length of this
jaroslav@49: * String
object; otherwise the result is the same
jaroslav@49: * as the result of the expression
jaroslav@49: *
jaroslav@49: * this.substring(toffset).startsWith(prefix)
jaroslav@49: *
jaroslav@49: */
jaroslav@49: public boolean startsWith(String prefix, int toffset) {
jaroslav@49: char ta[] = value;
jaroslav@49: int to = offset + toffset;
jaroslav@49: char pa[] = prefix.value;
jaroslav@49: int po = prefix.offset;
jaroslav@49: int pc = prefix.count;
jaroslav@49: // Note: toffset might be near -1>>>1.
jaroslav@49: if ((toffset < 0) || (toffset > count - pc)) {
jaroslav@49: return false;
jaroslav@49: }
jaroslav@49: while (--pc >= 0) {
jaroslav@49: if (ta[to++] != pa[po++]) {
jaroslav@49: return false;
jaroslav@49: }
jaroslav@49: }
jaroslav@49: return true;
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Tests if this string starts with the specified prefix.
jaroslav@49: *
jaroslav@49: * @param prefix the prefix.
jaroslav@49: * @return true
if the character sequence represented by the
jaroslav@49: * argument is a prefix of the character sequence represented by
jaroslav@49: * this string; false
otherwise.
jaroslav@49: * Note also that true
will be returned if the
jaroslav@49: * argument is an empty string or is equal to this
jaroslav@49: * String
object as determined by the
jaroslav@49: * {@link #equals(Object)} method.
jaroslav@49: * @since 1. 0
jaroslav@49: */
jaroslav@49: public boolean startsWith(String prefix) {
jaroslav@49: return startsWith(prefix, 0);
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Tests if this string ends with the specified suffix.
jaroslav@49: *
jaroslav@49: * @param suffix the suffix.
jaroslav@49: * @return true
if the character sequence represented by the
jaroslav@49: * argument is a suffix of the character sequence represented by
jaroslav@49: * this object; false
otherwise. Note that the
jaroslav@49: * result will be true
if the argument is the
jaroslav@49: * empty string or is equal to this String
object
jaroslav@49: * as determined by the {@link #equals(Object)} method.
jaroslav@49: */
jaroslav@49: public boolean endsWith(String suffix) {
jaroslav@49: return startsWith(suffix, count - suffix.count);
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Returns a hash code for this string. The hash code for a
jaroslav@49: * String
object is computed as
jaroslav@49: *
jaroslav@49: * using
jaroslav@49: * s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
jaroslav@49: *
int
arithmetic, where s[i]
is the
jaroslav@49: * ith character of the string, n
is the length of
jaroslav@49: * the string, and ^
indicates exponentiation.
jaroslav@49: * (The hash value of the empty string is zero.)
jaroslav@49: *
jaroslav@49: * @return a hash code value for this object.
jaroslav@49: */
jaroslav@49: public int hashCode() {
jaroslav@49: int h = hash;
jaroslav@49: if (h == 0 && count > 0) {
jaroslav@49: int off = offset;
jaroslav@49: char val[] = value;
jaroslav@49: int len = count;
jaroslav@49:
jaroslav@49: for (int i = 0; i < len; i++) {
jaroslav@49: h = 31*h + val[off++];
jaroslav@49: }
jaroslav@49: hash = h;
jaroslav@49: }
jaroslav@49: return h;
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Returns the index within this string of the first occurrence of
jaroslav@49: * the specified character. If a character with value
jaroslav@49: * ch
occurs in the character sequence represented by
jaroslav@49: * this String
object, then the index (in Unicode
jaroslav@49: * code units) of the first such occurrence is returned. For
jaroslav@49: * values of ch
in the range from 0 to 0xFFFF
jaroslav@49: * (inclusive), this is the smallest value k such that:
jaroslav@49: *
jaroslav@49: * is true. For other values of
jaroslav@49: * this.charAt(k) == ch
jaroslav@49: *
ch
, it is the
jaroslav@49: * smallest value k such that:
jaroslav@49: *
jaroslav@49: * is true. In either case, if no such character occurs in this
jaroslav@49: * string, then
jaroslav@49: * this.codePointAt(k) == ch
jaroslav@49: *
-1
is returned.
jaroslav@49: *
jaroslav@49: * @param ch a character (Unicode code point).
jaroslav@49: * @return the index of the first occurrence of the character in the
jaroslav@49: * character sequence represented by this object, or
jaroslav@49: * -1
if the character does not occur.
jaroslav@49: */
jaroslav@49: public int indexOf(int ch) {
jaroslav@49: return indexOf(ch, 0);
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Returns the index within this string of the first occurrence of the
jaroslav@49: * specified character, starting the search at the specified index.
jaroslav@49: * ch
occurs in the
jaroslav@49: * character sequence represented by this String
jaroslav@49: * object at an index no smaller than fromIndex
, then
jaroslav@49: * the index of the first such occurrence is returned. For values
jaroslav@49: * of ch
in the range from 0 to 0xFFFF (inclusive),
jaroslav@49: * this is the smallest value k such that:
jaroslav@49: *
jaroslav@49: * is true. For other values of
jaroslav@49: * (this.charAt(k) == ch) && (k >= fromIndex)
jaroslav@49: *
ch
, it is the
jaroslav@49: * smallest value k such that:
jaroslav@49: *
jaroslav@49: * is true. In either case, if no such character occurs in this
jaroslav@49: * string at or after position
jaroslav@49: * (this.codePointAt(k) == ch) && (k >= fromIndex)
jaroslav@49: *
fromIndex
, then
jaroslav@49: * -1
is returned.
jaroslav@49: *
jaroslav@49: * fromIndex
. If it
jaroslav@49: * is negative, it has the same effect as if it were zero: this entire
jaroslav@49: * string may be searched. If it is greater than the length of this
jaroslav@49: * string, it has the same effect as if it were equal to the length of
jaroslav@49: * this string: -1
is returned.
jaroslav@49: *
jaroslav@49: * char
values
jaroslav@49: * (Unicode code units).
jaroslav@49: *
jaroslav@49: * @param ch a character (Unicode code point).
jaroslav@49: * @param fromIndex the index to start the search from.
jaroslav@49: * @return the index of the first occurrence of the character in the
jaroslav@49: * character sequence represented by this object that is greater
jaroslav@49: * than or equal to fromIndex
, or -1
jaroslav@49: * if the character does not occur.
jaroslav@49: */
jaroslav@49: public int indexOf(int ch, int fromIndex) {
jaroslav@49: if (fromIndex < 0) {
jaroslav@49: fromIndex = 0;
jaroslav@49: } else if (fromIndex >= count) {
jaroslav@49: // Note: fromIndex might be near -1>>>1.
jaroslav@49: return -1;
jaroslav@49: }
jaroslav@49:
jaroslav@49: if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) {
jaroslav@49: // handle most cases here (ch is a BMP code point or a
jaroslav@49: // negative value (invalid code point))
jaroslav@49: final char[] value = this.value;
jaroslav@49: final int offset = this.offset;
jaroslav@49: final int max = offset + count;
jaroslav@49: for (int i = offset + fromIndex; i < max ; i++) {
jaroslav@49: if (value[i] == ch) {
jaroslav@49: return i - offset;
jaroslav@49: }
jaroslav@49: }
jaroslav@49: return -1;
jaroslav@49: } else {
jaroslav@49: return indexOfSupplementary(ch, fromIndex);
jaroslav@49: }
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Handles (rare) calls of indexOf with a supplementary character.
jaroslav@49: */
jaroslav@49: private int indexOfSupplementary(int ch, int fromIndex) {
jaroslav@49: if (Character.isValidCodePoint(ch)) {
jaroslav@49: final char[] value = this.value;
jaroslav@49: final int offset = this.offset;
jaroslav@49: final char hi = Character.highSurrogate(ch);
jaroslav@49: final char lo = Character.lowSurrogate(ch);
jaroslav@49: final int max = offset + count - 1;
jaroslav@49: for (int i = offset + fromIndex; i < max; i++) {
jaroslav@49: if (value[i] == hi && value[i+1] == lo) {
jaroslav@49: return i - offset;
jaroslav@49: }
jaroslav@49: }
jaroslav@49: }
jaroslav@49: return -1;
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Returns the index within this string of the last occurrence of
jaroslav@49: * the specified character. For values of ch
in the
jaroslav@49: * range from 0 to 0xFFFF (inclusive), the index (in Unicode code
jaroslav@49: * units) returned is the largest value k such that:
jaroslav@49: *
jaroslav@49: * is true. For other values of
jaroslav@49: * this.charAt(k) == ch
jaroslav@49: *
ch
, it is the
jaroslav@49: * largest value k such that:
jaroslav@49: *
jaroslav@49: * is true. In either case, if no such character occurs in this
jaroslav@49: * string, then
jaroslav@49: * this.codePointAt(k) == ch
jaroslav@49: *
-1
is returned. The
jaroslav@49: * String
is searched backwards starting at the last
jaroslav@49: * character.
jaroslav@49: *
jaroslav@49: * @param ch a character (Unicode code point).
jaroslav@49: * @return the index of the last occurrence of the character in the
jaroslav@49: * character sequence represented by this object, or
jaroslav@49: * -1
if the character does not occur.
jaroslav@49: */
jaroslav@49: public int lastIndexOf(int ch) {
jaroslav@49: return lastIndexOf(ch, count - 1);
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Returns the index within this string of the last occurrence of
jaroslav@49: * the specified character, searching backward starting at the
jaroslav@49: * specified index. For values of ch
in the range
jaroslav@49: * from 0 to 0xFFFF (inclusive), the index returned is the largest
jaroslav@49: * value k such that:
jaroslav@49: *
jaroslav@49: * is true. For other values of
jaroslav@49: * (this.charAt(k) == ch) && (k <= fromIndex)
jaroslav@49: *
ch
, it is the
jaroslav@49: * largest value k such that:
jaroslav@49: *
jaroslav@49: * is true. In either case, if no such character occurs in this
jaroslav@49: * string at or before position
jaroslav@49: * (this.codePointAt(k) == ch) && (k <= fromIndex)
jaroslav@49: *
fromIndex
, then
jaroslav@49: * -1
is returned.
jaroslav@49: *
jaroslav@49: * char
values
jaroslav@49: * (Unicode code units).
jaroslav@49: *
jaroslav@49: * @param ch a character (Unicode code point).
jaroslav@49: * @param fromIndex the index to start the search from. There is no
jaroslav@49: * restriction on the value of fromIndex
. If it is
jaroslav@49: * greater than or equal to the length of this string, it has
jaroslav@49: * the same effect as if it were equal to one less than the
jaroslav@49: * length of this string: this entire string may be searched.
jaroslav@49: * If it is negative, it has the same effect as if it were -1:
jaroslav@49: * -1 is returned.
jaroslav@49: * @return the index of the last occurrence of the character in the
jaroslav@49: * character sequence represented by this object that is less
jaroslav@49: * than or equal to fromIndex
, or -1
jaroslav@49: * if the character does not occur before that point.
jaroslav@49: */
jaroslav@49: public int lastIndexOf(int ch, int fromIndex) {
jaroslav@49: if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) {
jaroslav@49: // handle most cases here (ch is a BMP code point or a
jaroslav@49: // negative value (invalid code point))
jaroslav@49: final char[] value = this.value;
jaroslav@49: final int offset = this.offset;
jaroslav@49: int i = offset + Math.min(fromIndex, count - 1);
jaroslav@49: for (; i >= offset ; i--) {
jaroslav@49: if (value[i] == ch) {
jaroslav@49: return i - offset;
jaroslav@49: }
jaroslav@49: }
jaroslav@49: return -1;
jaroslav@49: } else {
jaroslav@49: return lastIndexOfSupplementary(ch, fromIndex);
jaroslav@49: }
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Handles (rare) calls of lastIndexOf with a supplementary character.
jaroslav@49: */
jaroslav@49: private int lastIndexOfSupplementary(int ch, int fromIndex) {
jaroslav@49: if (Character.isValidCodePoint(ch)) {
jaroslav@49: final char[] value = this.value;
jaroslav@49: final int offset = this.offset;
jaroslav@49: char hi = Character.highSurrogate(ch);
jaroslav@49: char lo = Character.lowSurrogate(ch);
jaroslav@49: int i = offset + Math.min(fromIndex, count - 2);
jaroslav@49: for (; i >= offset; i--) {
jaroslav@49: if (value[i] == hi && value[i+1] == lo) {
jaroslav@49: return i - offset;
jaroslav@49: }
jaroslav@49: }
jaroslav@49: }
jaroslav@49: return -1;
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Returns the index within this string of the first occurrence of the
jaroslav@49: * specified substring.
jaroslav@49: *
jaroslav@49: *
jaroslav@49: * If no such value of k exists, then {@code -1} is returned.
jaroslav@49: *
jaroslav@49: * @param str the substring to search for.
jaroslav@49: * @return the index of the first occurrence of the specified substring,
jaroslav@49: * or {@code -1} if there is no such occurrence.
jaroslav@49: */
jaroslav@49: public int indexOf(String str) {
jaroslav@49: return indexOf(str, 0);
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Returns the index within this string of the first occurrence of the
jaroslav@49: * specified substring, starting at the specified index.
jaroslav@49: *
jaroslav@49: *
jaroslav@49: * this.startsWith(str, k)
jaroslav@49: *
jaroslav@49: * If no such value of k exists, then {@code -1} is returned.
jaroslav@49: *
jaroslav@49: * @param str the substring to search for.
jaroslav@49: * @param fromIndex the index from which to start the search.
jaroslav@49: * @return the index of the first occurrence of the specified substring,
jaroslav@49: * starting at the specified index,
jaroslav@49: * or {@code -1} if there is no such occurrence.
jaroslav@49: */
jaroslav@49: public int indexOf(String str, int fromIndex) {
jaroslav@49: return indexOf(value, offset, count,
jaroslav@49: str.value, str.offset, str.count, fromIndex);
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Code shared by String and StringBuffer to do searches. The
jaroslav@49: * source is the character array being searched, and the target
jaroslav@49: * is the string being searched for.
jaroslav@49: *
jaroslav@49: * @param source the characters being searched.
jaroslav@49: * @param sourceOffset offset of the source string.
jaroslav@49: * @param sourceCount count of the source string.
jaroslav@49: * @param target the characters being searched for.
jaroslav@49: * @param targetOffset offset of the target string.
jaroslav@49: * @param targetCount count of the target string.
jaroslav@49: * @param fromIndex the index to begin searching from.
jaroslav@49: */
jaroslav@49: static int indexOf(char[] source, int sourceOffset, int sourceCount,
jaroslav@49: char[] target, int targetOffset, int targetCount,
jaroslav@49: int fromIndex) {
jaroslav@49: if (fromIndex >= sourceCount) {
jaroslav@49: return (targetCount == 0 ? sourceCount : -1);
jaroslav@49: }
jaroslav@49: if (fromIndex < 0) {
jaroslav@49: fromIndex = 0;
jaroslav@49: }
jaroslav@49: if (targetCount == 0) {
jaroslav@49: return fromIndex;
jaroslav@49: }
jaroslav@49:
jaroslav@49: char first = target[targetOffset];
jaroslav@49: int max = sourceOffset + (sourceCount - targetCount);
jaroslav@49:
jaroslav@49: for (int i = sourceOffset + fromIndex; i <= max; i++) {
jaroslav@49: /* Look for first character. */
jaroslav@49: if (source[i] != first) {
jaroslav@49: while (++i <= max && source[i] != first);
jaroslav@49: }
jaroslav@49:
jaroslav@49: /* Found first character, now look at the rest of v2 */
jaroslav@49: if (i <= max) {
jaroslav@49: int j = i + 1;
jaroslav@49: int end = j + targetCount - 1;
jaroslav@49: for (int k = targetOffset + 1; j < end && source[j] ==
jaroslav@49: target[k]; j++, k++);
jaroslav@49:
jaroslav@49: if (j == end) {
jaroslav@49: /* Found whole string. */
jaroslav@49: return i - sourceOffset;
jaroslav@49: }
jaroslav@49: }
jaroslav@49: }
jaroslav@49: return -1;
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Returns the index within this string of the last occurrence of the
jaroslav@49: * specified substring. The last occurrence of the empty string ""
jaroslav@49: * is considered to occur at the index value {@code this.length()}.
jaroslav@49: *
jaroslav@49: *
jaroslav@49: * k >= fromIndex && this.startsWith(str, k)
jaroslav@49: *
jaroslav@49: * If no such value of k exists, then {@code -1} is returned.
jaroslav@49: *
jaroslav@49: * @param str the substring to search for.
jaroslav@49: * @return the index of the last occurrence of the specified substring,
jaroslav@49: * or {@code -1} if there is no such occurrence.
jaroslav@49: */
jaroslav@49: public int lastIndexOf(String str) {
jaroslav@49: return lastIndexOf(str, count);
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Returns the index within this string of the last occurrence of the
jaroslav@49: * specified substring, searching backward starting at the specified index.
jaroslav@49: *
jaroslav@49: *
jaroslav@49: * this.startsWith(str, k)
jaroslav@49: *
jaroslav@49: * If no such value of k exists, then {@code -1} is returned.
jaroslav@49: *
jaroslav@49: * @param str the substring to search for.
jaroslav@49: * @param fromIndex the index to start the search from.
jaroslav@49: * @return the index of the last occurrence of the specified substring,
jaroslav@49: * searching backward from the specified index,
jaroslav@49: * or {@code -1} if there is no such occurrence.
jaroslav@49: */
jaroslav@49: public int lastIndexOf(String str, int fromIndex) {
jaroslav@49: return lastIndexOf(value, offset, count,
jaroslav@49: str.value, str.offset, str.count, fromIndex);
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Code shared by String and StringBuffer to do searches. The
jaroslav@49: * source is the character array being searched, and the target
jaroslav@49: * is the string being searched for.
jaroslav@49: *
jaroslav@49: * @param source the characters being searched.
jaroslav@49: * @param sourceOffset offset of the source string.
jaroslav@49: * @param sourceCount count of the source string.
jaroslav@49: * @param target the characters being searched for.
jaroslav@49: * @param targetOffset offset of the target string.
jaroslav@49: * @param targetCount count of the target string.
jaroslav@49: * @param fromIndex the index to begin searching from.
jaroslav@49: */
jaroslav@49: static int lastIndexOf(char[] source, int sourceOffset, int sourceCount,
jaroslav@49: char[] target, int targetOffset, int targetCount,
jaroslav@49: int fromIndex) {
jaroslav@49: /*
jaroslav@49: * Check arguments; return immediately where possible. For
jaroslav@49: * consistency, don't check for null str.
jaroslav@49: */
jaroslav@49: int rightIndex = sourceCount - targetCount;
jaroslav@49: if (fromIndex < 0) {
jaroslav@49: return -1;
jaroslav@49: }
jaroslav@49: if (fromIndex > rightIndex) {
jaroslav@49: fromIndex = rightIndex;
jaroslav@49: }
jaroslav@49: /* Empty string always matches. */
jaroslav@49: if (targetCount == 0) {
jaroslav@49: return fromIndex;
jaroslav@49: }
jaroslav@49:
jaroslav@49: int strLastIndex = targetOffset + targetCount - 1;
jaroslav@49: char strLastChar = target[strLastIndex];
jaroslav@49: int min = sourceOffset + targetCount - 1;
jaroslav@49: int i = min + fromIndex;
jaroslav@49:
jaroslav@49: startSearchForLastChar:
jaroslav@49: while (true) {
jaroslav@49: while (i >= min && source[i] != strLastChar) {
jaroslav@49: i--;
jaroslav@49: }
jaroslav@49: if (i < min) {
jaroslav@49: return -1;
jaroslav@49: }
jaroslav@49: int j = i - 1;
jaroslav@49: int start = j - (targetCount - 1);
jaroslav@49: int k = strLastIndex - 1;
jaroslav@49:
jaroslav@49: while (j > start) {
jaroslav@49: if (source[j--] != target[k--]) {
jaroslav@49: i--;
jaroslav@49: continue startSearchForLastChar;
jaroslav@49: }
jaroslav@49: }
jaroslav@49: return start - sourceOffset + 1;
jaroslav@49: }
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Returns a new string that is a substring of this string. The
jaroslav@49: * substring begins with the character at the specified index and
jaroslav@49: * extends to the end of this string.
jaroslav@49: * k <= fromIndex && this.startsWith(str, k)
jaroslav@49: *
jaroslav@49: *
jaroslav@49: * @param beginIndex the beginning index, inclusive.
jaroslav@49: * @return the specified substring.
jaroslav@49: * @exception IndexOutOfBoundsException if
jaroslav@49: *
jaroslav@49: * "unhappy".substring(2) returns "happy"
jaroslav@49: * "Harbison".substring(3) returns "bison"
jaroslav@49: * "emptiness".substring(9) returns "" (an empty string)
jaroslav@49: *
beginIndex
is negative or larger than the
jaroslav@49: * length of this String
object.
jaroslav@49: */
jaroslav@49: public String substring(int beginIndex) {
jaroslav@49: return substring(beginIndex, count);
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Returns a new string that is a substring of this string. The
jaroslav@49: * substring begins at the specified beginIndex
and
jaroslav@49: * extends to the character at index endIndex - 1
.
jaroslav@49: * Thus the length of the substring is endIndex-beginIndex
.
jaroslav@49: *
jaroslav@49: *
jaroslav@49: * @param beginIndex the beginning index, inclusive.
jaroslav@49: * @param endIndex the ending index, exclusive.
jaroslav@49: * @return the specified substring.
jaroslav@49: * @exception IndexOutOfBoundsException if the
jaroslav@49: *
jaroslav@49: * "hamburger".substring(4, 8) returns "urge"
jaroslav@49: * "smiles".substring(1, 5) returns "mile"
jaroslav@49: *
beginIndex
is negative, or
jaroslav@49: * endIndex
is larger than the length of
jaroslav@49: * this String
object, or
jaroslav@49: * beginIndex
is larger than
jaroslav@49: * endIndex
.
jaroslav@49: */
jaroslav@49: public String substring(int beginIndex, int endIndex) {
jaroslav@49: if (beginIndex < 0) {
jaroslav@49: throw new StringIndexOutOfBoundsException(beginIndex);
jaroslav@49: }
jaroslav@49: if (endIndex > count) {
jaroslav@49: throw new StringIndexOutOfBoundsException(endIndex);
jaroslav@49: }
jaroslav@49: if (beginIndex > endIndex) {
jaroslav@49: throw new StringIndexOutOfBoundsException(endIndex - beginIndex);
jaroslav@49: }
jaroslav@49: return ((beginIndex == 0) && (endIndex == count)) ? this :
jaroslav@49: new String(offset + beginIndex, endIndex - beginIndex, value);
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Returns a new character sequence that is a subsequence of this sequence.
jaroslav@49: *
jaroslav@49: *
jaroslav@49: *
jaroslav@49: * behaves in exactly the same way as the invocation
jaroslav@49: *
jaroslav@49: *
jaroslav@49: * str.subSequence(begin, end)
jaroslav@49: *
jaroslav@49: * This method is defined so that the String class can implement
jaroslav@49: * the {@link CharSequence} interface.
jaroslav@49: * str.substring(begin, end)
0
, then this
jaroslav@49: * String
object is returned. Otherwise, a new
jaroslav@49: * String
object is created, representing a character
jaroslav@49: * sequence that is the concatenation of the character sequence
jaroslav@49: * represented by this String
object and the character
jaroslav@49: * sequence represented by the argument string.
jaroslav@49: *
jaroslav@49: * @param str the
jaroslav@49: * "cares".concat("s") returns "caress"
jaroslav@49: * "to".concat("get").concat("her") returns "together"
jaroslav@49: *
String
that is concatenated to the end
jaroslav@49: * of this String
.
jaroslav@49: * @return a string that represents the concatenation of this object's
jaroslav@49: * characters followed by the string argument's characters.
jaroslav@49: */
jaroslav@49: public String concat(String str) {
jaroslav@49: int otherLen = str.length();
jaroslav@49: if (otherLen == 0) {
jaroslav@49: return this;
jaroslav@49: }
jaroslav@49: char buf[] = new char[count + otherLen];
jaroslav@49: getChars(0, count, buf, 0);
jaroslav@49: str.getChars(0, otherLen, buf, count);
jaroslav@49: return new String(0, count + otherLen, buf);
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Returns a new string resulting from replacing all occurrences of
jaroslav@49: * oldChar
in this string with newChar
.
jaroslav@49: * oldChar
does not occur in the
jaroslav@49: * character sequence represented by this String
object,
jaroslav@49: * then a reference to this String
object is returned.
jaroslav@49: * Otherwise, a new String
object is created that
jaroslav@49: * represents a character sequence identical to the character sequence
jaroslav@49: * represented by this String
object, except that every
jaroslav@49: * occurrence of oldChar
is replaced by an occurrence
jaroslav@49: * of newChar
.
jaroslav@49: *
jaroslav@49: *
jaroslav@49: * @param oldChar the old character.
jaroslav@49: * @param newChar the new character.
jaroslav@49: * @return a string derived from this string by replacing every
jaroslav@49: * occurrence of
jaroslav@49: * "mesquite in your cellar".replace('e', 'o')
jaroslav@49: * returns "mosquito in your collar"
jaroslav@49: * "the war of baronets".replace('r', 'y')
jaroslav@49: * returns "the way of bayonets"
jaroslav@49: * "sparring with a purple porpoise".replace('p', 't')
jaroslav@49: * returns "starring with a turtle tortoise"
jaroslav@49: * "JonL".replace('q', 'x') returns "JonL" (no change)
jaroslav@49: *
oldChar
with newChar
.
jaroslav@49: */
jaroslav@49: public String replace(char oldChar, char newChar) {
jaroslav@49: if (oldChar != newChar) {
jaroslav@49: int len = count;
jaroslav@49: int i = -1;
jaroslav@49: char[] val = value; /* avoid getfield opcode */
jaroslav@49: int off = offset; /* avoid getfield opcode */
jaroslav@49:
jaroslav@49: while (++i < len) {
jaroslav@49: if (val[off + i] == oldChar) {
jaroslav@49: break;
jaroslav@49: }
jaroslav@49: }
jaroslav@49: if (i < len) {
jaroslav@49: char buf[] = new char[len];
jaroslav@49: for (int j = 0 ; j < i ; j++) {
jaroslav@49: buf[j] = val[off+j];
jaroslav@49: }
jaroslav@49: while (i < len) {
jaroslav@49: char c = val[off + i];
jaroslav@49: buf[i] = (c == oldChar) ? newChar : c;
jaroslav@49: i++;
jaroslav@49: }
jaroslav@49: return new String(0, len, buf);
jaroslav@49: }
jaroslav@49: }
jaroslav@49: return this;
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Tells whether or not this string matches the given regular expression.
jaroslav@49: *
jaroslav@49: * {@link java.util.regex.Pattern}.{@link
jaroslav@49: * java.util.regex.Pattern#matches(String,CharSequence)
jaroslav@49: * matches}(regex, str)
jaroslav@49: *
jaroslav@49: * @param regex
jaroslav@49: * the regular expression to which this string is to be matched
jaroslav@49: *
jaroslav@49: * @return true if, and only if, this string matches the
jaroslav@49: * given regular expression
jaroslav@49: *
jaroslav@49: * @throws PatternSyntaxException
jaroslav@49: * if the regular expression's syntax is invalid
jaroslav@49: *
jaroslav@49: * @see java.util.regex.Pattern
jaroslav@49: *
jaroslav@49: * @since 1.4
jaroslav@49: * @spec JSR-51
jaroslav@49: */
jaroslav@49: public boolean matches(String regex) {
jaroslav@64: throw new UnsupportedOperationException();
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Returns true if and only if this string contains the specified
jaroslav@49: * sequence of char values.
jaroslav@49: *
jaroslav@49: * @param s the sequence to search for
jaroslav@49: * @return true if this string contains s
, false otherwise
jaroslav@49: * @throws NullPointerException if s
is null
jaroslav@49: * @since 1.5
jaroslav@49: */
jaroslav@49: public boolean contains(CharSequence s) {
jaroslav@49: return indexOf(s.toString()) > -1;
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Replaces the first substring of this string that matches the given regular expression with the
jaroslav@49: * given replacement.
jaroslav@49: *
jaroslav@49: *
jaroslav@49: * {@link java.util.regex.Pattern}.{@link java.util.regex.Pattern#compile
jaroslav@49: * compile}(regex).{@link
jaroslav@49: * java.util.regex.Pattern#matcher(java.lang.CharSequence)
jaroslav@49: * matcher}(str).{@link java.util.regex.Matcher#replaceFirst
jaroslav@49: * replaceFirst}(repl)
jaroslav@49: *
jaroslav@49: *
jaroslav@49: * {@link java.util.regex.Pattern}.{@link java.util.regex.Pattern#compile
jaroslav@49: * compile}(regex).{@link
jaroslav@49: * java.util.regex.Pattern#matcher(java.lang.CharSequence)
jaroslav@49: * matcher}(str).{@link java.util.regex.Matcher#replaceAll
jaroslav@49: * replaceAll}(repl)
jaroslav@49: *
jaroslav@49: *target
or
jaroslav@49: * replacement
is null
.
jaroslav@49: * @since 1.5
jaroslav@49: */
jaroslav@49: public String replace(CharSequence target, CharSequence replacement) {
jaroslav@64: throw new UnsupportedOperationException("This one should be supported, but without dep on rest of regexp");
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Splits this string around matches of the given
jaroslav@49: * regular expression.
jaroslav@49: *
jaroslav@49: *
jaroslav@49: *
jaroslav@49: *
jaroslav@49: *
jaroslav@49: *
jaroslav@49: * Regex
jaroslav@49: * Limit
jaroslav@49: * Result
jaroslav@49: *
jaroslav@49: * :
jaroslav@49: * 2
jaroslav@49: * { "boo", "and:foo" }
jaroslav@49: * :
jaroslav@49: * 5
jaroslav@49: * { "boo", "and", "foo" }
jaroslav@49: * :
jaroslav@49: * -2
jaroslav@49: * { "boo", "and", "foo" }
jaroslav@49: * o
jaroslav@49: * 5
jaroslav@49: * { "b", "", ":and:f", "", "" }
jaroslav@49: * o
jaroslav@49: * -2
jaroslav@49: * { "b", "", ":and:f", "", "" }
jaroslav@49: * o
jaroslav@49: * 0
jaroslav@49: * { "b", "", ":and:f" }
jaroslav@49: * {@link java.util.regex.Pattern}.{@link java.util.regex.Pattern#compile
jaroslav@49: * compile}(regex).{@link
jaroslav@49: * java.util.regex.Pattern#split(java.lang.CharSequence,int)
jaroslav@49: * split}(str, n)
jaroslav@49: *
jaroslav@49: *
jaroslav@49: *
jaroslav@49: * @param regex
jaroslav@49: * the delimiting regular expression
jaroslav@49: *
jaroslav@49: * @param limit
jaroslav@49: * the result threshold, as described above
jaroslav@49: *
jaroslav@49: * @return the array of strings computed by splitting this string
jaroslav@49: * around matches of the given regular expression
jaroslav@49: *
jaroslav@49: * @throws PatternSyntaxException
jaroslav@49: * if the regular expression's syntax is invalid
jaroslav@49: *
jaroslav@49: * @see java.util.regex.Pattern
jaroslav@49: *
jaroslav@49: * @since 1.4
jaroslav@49: * @spec JSR-51
jaroslav@49: */
jaroslav@49: public String[] split(String regex, int limit) {
jaroslav@64: throw new UnsupportedOperationException("Needs regexp");
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Splits this string around matches of the given regular expression.
jaroslav@49: *
jaroslav@49: *
jaroslav@49: *
jaroslav@49: *
jaroslav@49: * @param regex
jaroslav@49: * the delimiting regular expression
jaroslav@49: *
jaroslav@49: * @return the array of strings computed by splitting this string
jaroslav@49: * around matches of the given regular expression
jaroslav@49: *
jaroslav@49: * @throws PatternSyntaxException
jaroslav@49: * if the regular expression's syntax is invalid
jaroslav@49: *
jaroslav@49: * @see java.util.regex.Pattern
jaroslav@49: *
jaroslav@49: * @since 1.4
jaroslav@49: * @spec JSR-51
jaroslav@49: */
jaroslav@49: public String[] split(String regex) {
jaroslav@49: return split(regex, 0);
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Converts all of the characters in this
jaroslav@49: *
jaroslav@49: *
jaroslav@49: * Regex
jaroslav@49: * Result
jaroslav@49: *
jaroslav@49: * :
jaroslav@49: * { "boo", "and", "foo" }
jaroslav@49: * o
jaroslav@49: * { "b", "", ":and:f" } String
to lower
jaroslav@49: * case using the rules of the given Locale
. Case mapping is based
jaroslav@49: * on the Unicode Standard version specified by the {@link java.lang.Character Character}
jaroslav@49: * class. Since case mappings are not always 1:1 char mappings, the resulting
jaroslav@49: * String
may be a different length than the original String
.
jaroslav@49: *
jaroslav@49: *
jaroslav@49: *
jaroslav@49: * @param locale use the case transformation rules for this locale
jaroslav@49: * @return the
jaroslav@49: *
jaroslav@49: * Language Code of Locale
jaroslav@49: * Upper Case
jaroslav@49: * Lower Case
jaroslav@49: * Description
jaroslav@49: *
jaroslav@49: *
jaroslav@49: * tr (Turkish)
jaroslav@49: * \u0130
jaroslav@49: * \u0069
jaroslav@49: * capital letter I with dot above -> small letter i
jaroslav@49: *
jaroslav@49: *
jaroslav@49: * tr (Turkish)
jaroslav@49: * \u0049
jaroslav@49: * \u0131
jaroslav@49: * capital letter I -> small letter dotless i
jaroslav@49: *
jaroslav@49: *
jaroslav@49: * (all)
jaroslav@49: * French Fries
jaroslav@49: * french fries
jaroslav@49: * lowercased all chars in String
jaroslav@49: *
jaroslav@49: *
jaroslav@49: * (all)
jaroslav@49: *
jaroslav@49: *
jaroslav@49: *
jaroslav@49: *
jaroslav@49: *
jaroslav@49: *
jaroslav@49: * lowercased all chars in String
jaroslav@49: * String
, converted to lowercase.
jaroslav@49: * @see java.lang.String#toLowerCase()
jaroslav@49: * @see java.lang.String#toUpperCase()
jaroslav@49: * @see java.lang.String#toUpperCase(Locale)
jaroslav@49: * @since 1.1
jaroslav@49: */
jaroslav@61: // public String toLowerCase(Locale locale) {
jaroslav@61: // if (locale == null) {
jaroslav@61: // throw new NullPointerException();
jaroslav@61: // }
jaroslav@61: //
jaroslav@61: // int firstUpper;
jaroslav@61: //
jaroslav@61: // /* Now check if there are any characters that need to be changed. */
jaroslav@61: // scan: {
jaroslav@61: // for (firstUpper = 0 ; firstUpper < count; ) {
jaroslav@61: // char c = value[offset+firstUpper];
jaroslav@61: // if ((c >= Character.MIN_HIGH_SURROGATE) &&
jaroslav@61: // (c <= Character.MAX_HIGH_SURROGATE)) {
jaroslav@61: // int supplChar = codePointAt(firstUpper);
jaroslav@61: // if (supplChar != Character.toLowerCase(supplChar)) {
jaroslav@61: // break scan;
jaroslav@61: // }
jaroslav@61: // firstUpper += Character.charCount(supplChar);
jaroslav@61: // } else {
jaroslav@61: // if (c != Character.toLowerCase(c)) {
jaroslav@61: // break scan;
jaroslav@61: // }
jaroslav@61: // firstUpper++;
jaroslav@61: // }
jaroslav@61: // }
jaroslav@61: // return this;
jaroslav@61: // }
jaroslav@61: //
jaroslav@61: // char[] result = new char[count];
jaroslav@61: // int resultOffset = 0; /* result may grow, so i+resultOffset
jaroslav@61: // * is the write location in result */
jaroslav@61: //
jaroslav@61: // /* Just copy the first few lowerCase characters. */
jaroslav@72: // arraycopy(value, offset, result, 0, firstUpper);
jaroslav@61: //
jaroslav@61: // String lang = locale.getLanguage();
jaroslav@61: // boolean localeDependent =
jaroslav@61: // (lang == "tr" || lang == "az" || lang == "lt");
jaroslav@61: // char[] lowerCharArray;
jaroslav@61: // int lowerChar;
jaroslav@61: // int srcChar;
jaroslav@61: // int srcCount;
jaroslav@61: // for (int i = firstUpper; i < count; i += srcCount) {
jaroslav@61: // srcChar = (int)value[offset+i];
jaroslav@61: // if ((char)srcChar >= Character.MIN_HIGH_SURROGATE &&
jaroslav@61: // (char)srcChar <= Character.MAX_HIGH_SURROGATE) {
jaroslav@61: // srcChar = codePointAt(i);
jaroslav@61: // srcCount = Character.charCount(srcChar);
jaroslav@61: // } else {
jaroslav@61: // srcCount = 1;
jaroslav@61: // }
jaroslav@61: // if (localeDependent || srcChar == '\u03A3') { // GREEK CAPITAL LETTER SIGMA
jaroslav@61: // lowerChar = ConditionalSpecialCasing.toLowerCaseEx(this, i, locale);
jaroslav@61: // } else if (srcChar == '\u0130') { // LATIN CAPITAL LETTER I DOT
jaroslav@61: // lowerChar = Character.ERROR;
jaroslav@61: // } else {
jaroslav@61: // lowerChar = Character.toLowerCase(srcChar);
jaroslav@61: // }
jaroslav@61: // if ((lowerChar == Character.ERROR) ||
jaroslav@61: // (lowerChar >= Character.MIN_SUPPLEMENTARY_CODE_POINT)) {
jaroslav@61: // if (lowerChar == Character.ERROR) {
jaroslav@61: // if (!localeDependent && srcChar == '\u0130') {
jaroslav@61: // lowerCharArray =
jaroslav@61: // ConditionalSpecialCasing.toLowerCaseCharArray(this, i, Locale.ENGLISH);
jaroslav@61: // } else {
jaroslav@61: // lowerCharArray =
jaroslav@61: // ConditionalSpecialCasing.toLowerCaseCharArray(this, i, locale);
jaroslav@61: // }
jaroslav@61: // } else if (srcCount == 2) {
jaroslav@61: // resultOffset += Character.toChars(lowerChar, result, i + resultOffset) - srcCount;
jaroslav@61: // continue;
jaroslav@61: // } else {
jaroslav@61: // lowerCharArray = Character.toChars(lowerChar);
jaroslav@61: // }
jaroslav@61: //
jaroslav@61: // /* Grow result if needed */
jaroslav@61: // int mapLen = lowerCharArray.length;
jaroslav@61: // if (mapLen > srcCount) {
jaroslav@61: // char[] result2 = new char[result.length + mapLen - srcCount];
jaroslav@72: // arraycopy(result, 0, result2, 0,
jaroslav@61: // i + resultOffset);
jaroslav@61: // result = result2;
jaroslav@61: // }
jaroslav@61: // for (int x=0; xtoLowerCase(Locale.getDefault())
.
jaroslav@49: * "TITLE".toLowerCase()
in a Turkish locale
jaroslav@49: * returns "t\u005Cu0131tle"
, where '\u005Cu0131' is the
jaroslav@49: * LATIN SMALL LETTER DOTLESS I character.
jaroslav@49: * To obtain correct results for locale insensitive strings, use
jaroslav@49: * toLowerCase(Locale.ENGLISH)
.
jaroslav@49: * String
, converted to lowercase.
jaroslav@49: * @see java.lang.String#toLowerCase(Locale)
jaroslav@49: */
jaroslav@49: public String toLowerCase() {
jaroslav@64: throw new UnsupportedOperationException("Should be supported but without connection to locale");
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Converts all of the characters in this String
to upper
jaroslav@49: * case using the rules of the given Locale
. Case mapping is based
jaroslav@49: * on the Unicode Standard version specified by the {@link java.lang.Character Character}
jaroslav@49: * class. Since case mappings are not always 1:1 char mappings, the resulting
jaroslav@49: * String
may be a different length than the original String
.
jaroslav@49: *
jaroslav@49: *
jaroslav@49: * @param locale use the case transformation rules for this locale
jaroslav@49: * @return the
jaroslav@49: *
jaroslav@49: * Language Code of Locale
jaroslav@49: * Lower Case
jaroslav@49: * Upper Case
jaroslav@49: * Description
jaroslav@49: *
jaroslav@49: *
jaroslav@49: * tr (Turkish)
jaroslav@49: * \u0069
jaroslav@49: * \u0130
jaroslav@49: * small letter i -> capital letter I with dot above
jaroslav@49: *
jaroslav@49: *
jaroslav@49: * tr (Turkish)
jaroslav@49: * \u0131
jaroslav@49: * \u0049
jaroslav@49: * small letter dotless i -> capital letter I
jaroslav@49: *
jaroslav@49: *
jaroslav@49: * (all)
jaroslav@49: * \u00df
jaroslav@49: * \u0053 \u0053
jaroslav@49: * small letter sharp s -> two letters: SS
jaroslav@49: *
jaroslav@49: *
jaroslav@49: * (all)
jaroslav@49: * Fahrvergnügen
jaroslav@49: * FAHRVERGNÜGEN
jaroslav@49: *
jaroslav@49: * String
, converted to uppercase.
jaroslav@49: * @see java.lang.String#toUpperCase()
jaroslav@49: * @see java.lang.String#toLowerCase()
jaroslav@49: * @see java.lang.String#toLowerCase(Locale)
jaroslav@49: * @since 1.1
jaroslav@49: */
jaroslav@61: /* not for javascript
jaroslav@49: public String toUpperCase(Locale locale) {
jaroslav@49: if (locale == null) {
jaroslav@49: throw new NullPointerException();
jaroslav@49: }
jaroslav@49:
jaroslav@49: int firstLower;
jaroslav@49:
jaroslav@61: // Now check if there are any characters that need to be changed.
jaroslav@49: scan: {
jaroslav@49: for (firstLower = 0 ; firstLower < count; ) {
jaroslav@49: int c = (int)value[offset+firstLower];
jaroslav@49: int srcCount;
jaroslav@49: if ((c >= Character.MIN_HIGH_SURROGATE) &&
jaroslav@49: (c <= Character.MAX_HIGH_SURROGATE)) {
jaroslav@49: c = codePointAt(firstLower);
jaroslav@49: srcCount = Character.charCount(c);
jaroslav@49: } else {
jaroslav@49: srcCount = 1;
jaroslav@49: }
jaroslav@49: int upperCaseChar = Character.toUpperCaseEx(c);
jaroslav@49: if ((upperCaseChar == Character.ERROR) ||
jaroslav@49: (c != upperCaseChar)) {
jaroslav@49: break scan;
jaroslav@49: }
jaroslav@49: firstLower += srcCount;
jaroslav@49: }
jaroslav@49: return this;
jaroslav@49: }
jaroslav@49:
jaroslav@61: char[] result = new char[count]; /* may grow *
jaroslav@49: int resultOffset = 0; /* result may grow, so i+resultOffset
jaroslav@61: * is the write location in result *
jaroslav@49:
jaroslav@61: /* Just copy the first few upperCase characters. *
jaroslav@72: arraycopy(value, offset, result, 0, firstLower);
jaroslav@49:
jaroslav@49: String lang = locale.getLanguage();
jaroslav@49: boolean localeDependent =
jaroslav@49: (lang == "tr" || lang == "az" || lang == "lt");
jaroslav@49: char[] upperCharArray;
jaroslav@49: int upperChar;
jaroslav@49: int srcChar;
jaroslav@49: int srcCount;
jaroslav@49: for (int i = firstLower; i < count; i += srcCount) {
jaroslav@49: srcChar = (int)value[offset+i];
jaroslav@49: if ((char)srcChar >= Character.MIN_HIGH_SURROGATE &&
jaroslav@49: (char)srcChar <= Character.MAX_HIGH_SURROGATE) {
jaroslav@49: srcChar = codePointAt(i);
jaroslav@49: srcCount = Character.charCount(srcChar);
jaroslav@49: } else {
jaroslav@49: srcCount = 1;
jaroslav@49: }
jaroslav@49: if (localeDependent) {
jaroslav@49: upperChar = ConditionalSpecialCasing.toUpperCaseEx(this, i, locale);
jaroslav@49: } else {
jaroslav@49: upperChar = Character.toUpperCaseEx(srcChar);
jaroslav@49: }
jaroslav@49: if ((upperChar == Character.ERROR) ||
jaroslav@49: (upperChar >= Character.MIN_SUPPLEMENTARY_CODE_POINT)) {
jaroslav@49: if (upperChar == Character.ERROR) {
jaroslav@49: if (localeDependent) {
jaroslav@49: upperCharArray =
jaroslav@49: ConditionalSpecialCasing.toUpperCaseCharArray(this, i, locale);
jaroslav@49: } else {
jaroslav@49: upperCharArray = Character.toUpperCaseCharArray(srcChar);
jaroslav@49: }
jaroslav@49: } else if (srcCount == 2) {
jaroslav@49: resultOffset += Character.toChars(upperChar, result, i + resultOffset) - srcCount;
jaroslav@49: continue;
jaroslav@49: } else {
jaroslav@49: upperCharArray = Character.toChars(upperChar);
jaroslav@49: }
jaroslav@49:
jaroslav@61: /* Grow result if needed *
jaroslav@49: int mapLen = upperCharArray.length;
jaroslav@49: if (mapLen > srcCount) {
jaroslav@49: char[] result2 = new char[result.length + mapLen - srcCount];
jaroslav@72: arraycopy(result, 0, result2, 0,
jaroslav@49: i + resultOffset);
jaroslav@49: result = result2;
jaroslav@49: }
jaroslav@49: for (int x=0; xtoUpperCase(Locale.getDefault())
.
jaroslav@49: * "title".toUpperCase()
in a Turkish locale
jaroslav@49: * returns "T\u005Cu0130TLE"
, where '\u005Cu0130' is the
jaroslav@49: * LATIN CAPITAL LETTER I WITH DOT ABOVE character.
jaroslav@49: * To obtain correct results for locale insensitive strings, use
jaroslav@49: * toUpperCase(Locale.ENGLISH)
.
jaroslav@49: * String
, converted to uppercase.
jaroslav@49: * @see java.lang.String#toUpperCase(Locale)
jaroslav@49: */
jaroslav@49: public String toUpperCase() {
jaroslav@61: throw new UnsupportedOperationException();
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Returns a copy of the string, with leading and trailing whitespace
jaroslav@49: * omitted.
jaroslav@49: * String
object represents an empty character
jaroslav@49: * sequence, or the first and last characters of character sequence
jaroslav@49: * represented by this String
object both have codes
jaroslav@49: * greater than '\u0020'
(the space character), then a
jaroslav@49: * reference to this String
object is returned.
jaroslav@49: * '\u0020'
in the string, then a new
jaroslav@49: * String
object representing an empty string is created
jaroslav@49: * and returned.
jaroslav@49: * '\u0020'
, and let
jaroslav@49: * m be the index of the last character in the string whose code
jaroslav@49: * is greater than '\u0020'
. A new String
jaroslav@49: * object is created, representing the substring of this string that
jaroslav@49: * begins with the character at index k and ends with the
jaroslav@49: * character at index m-that is, the result of
jaroslav@49: * this.substring(k, m+1)
.
jaroslav@49: * Object
argument.
jaroslav@49: *
jaroslav@49: * @param obj an Object
.
jaroslav@49: * @return if the argument is null
, then a string equal to
jaroslav@49: * "null"
; otherwise, the value of
jaroslav@49: * obj.toString()
is returned.
jaroslav@49: * @see java.lang.Object#toString()
jaroslav@49: */
jaroslav@49: public static String valueOf(Object obj) {
jaroslav@49: return (obj == null) ? "null" : obj.toString();
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Returns the string representation of the char
array
jaroslav@49: * argument. The contents of the character array are copied; subsequent
jaroslav@49: * modification of the character array does not affect the newly
jaroslav@49: * created string.
jaroslav@49: *
jaroslav@49: * @param data a char
array.
jaroslav@49: * @return a newly allocated string representing the same sequence of
jaroslav@49: * characters contained in the character array argument.
jaroslav@49: */
jaroslav@49: public static String valueOf(char data[]) {
jaroslav@49: return new String(data);
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Returns the string representation of a specific subarray of the
jaroslav@49: * char
array argument.
jaroslav@49: * offset
argument is the index of the first
jaroslav@49: * character of the subarray. The count
argument
jaroslav@49: * specifies the length of the subarray. The contents of the subarray
jaroslav@49: * are copied; subsequent modification of the character array does not
jaroslav@49: * affect the newly created string.
jaroslav@49: *
jaroslav@49: * @param data the character array.
jaroslav@49: * @param offset the initial offset into the value of the
jaroslav@49: * String
.
jaroslav@49: * @param count the length of the value of the String
.
jaroslav@49: * @return a string representing the sequence of characters contained
jaroslav@49: * in the subarray of the character array argument.
jaroslav@49: * @exception IndexOutOfBoundsException if offset
is
jaroslav@49: * negative, or count
is negative, or
jaroslav@49: * offset+count
is larger than
jaroslav@49: * data.length
.
jaroslav@49: */
jaroslav@49: public static String valueOf(char data[], int offset, int count) {
jaroslav@49: return new String(data, offset, count);
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Returns a String that represents the character sequence in the
jaroslav@49: * array specified.
jaroslav@49: *
jaroslav@49: * @param data the character array.
jaroslav@49: * @param offset initial offset of the subarray.
jaroslav@49: * @param count length of the subarray.
jaroslav@49: * @return a String
that contains the characters of the
jaroslav@49: * specified subarray of the character array.
jaroslav@49: */
jaroslav@49: public static String copyValueOf(char data[], int offset, int count) {
jaroslav@49: // All public String constructors now copy the data.
jaroslav@49: return new String(data, offset, count);
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Returns a String that represents the character sequence in the
jaroslav@49: * array specified.
jaroslav@49: *
jaroslav@49: * @param data the character array.
jaroslav@49: * @return a String
that contains the characters of the
jaroslav@49: * character array.
jaroslav@49: */
jaroslav@49: public static String copyValueOf(char data[]) {
jaroslav@49: return copyValueOf(data, 0, data.length);
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Returns the string representation of the boolean
argument.
jaroslav@49: *
jaroslav@49: * @param b a boolean
.
jaroslav@49: * @return if the argument is true
, a string equal to
jaroslav@49: * "true"
is returned; otherwise, a string equal to
jaroslav@49: * "false"
is returned.
jaroslav@49: */
jaroslav@49: public static String valueOf(boolean b) {
jaroslav@49: return b ? "true" : "false";
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Returns the string representation of the char
jaroslav@49: * argument.
jaroslav@49: *
jaroslav@49: * @param c a char
.
jaroslav@49: * @return a string of length 1
containing
jaroslav@49: * as its single character the argument c
.
jaroslav@49: */
jaroslav@49: public static String valueOf(char c) {
jaroslav@49: char data[] = {c};
jaroslav@49: return new String(0, 1, data);
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Returns the string representation of the int
argument.
jaroslav@49: * Integer.toString
method of one argument.
jaroslav@49: *
jaroslav@49: * @param i an int
.
jaroslav@49: * @return a string representation of the int
argument.
jaroslav@49: * @see java.lang.Integer#toString(int, int)
jaroslav@49: */
jaroslav@49: public static String valueOf(int i) {
jaroslav@49: return Integer.toString(i);
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Returns the string representation of the long
argument.
jaroslav@49: * Long.toString
method of one argument.
jaroslav@49: *
jaroslav@49: * @param l a long
.
jaroslav@49: * @return a string representation of the long
argument.
jaroslav@49: * @see java.lang.Long#toString(long)
jaroslav@49: */
jaroslav@49: public static String valueOf(long l) {
jaroslav@49: return Long.toString(l);
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Returns the string representation of the float
argument.
jaroslav@49: * Float.toString
method of one argument.
jaroslav@49: *
jaroslav@49: * @param f a float
.
jaroslav@49: * @return a string representation of the float
argument.
jaroslav@49: * @see java.lang.Float#toString(float)
jaroslav@49: */
jaroslav@49: public static String valueOf(float f) {
jaroslav@49: return Float.toString(f);
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Returns the string representation of the double
argument.
jaroslav@49: * Double.toString
method of one argument.
jaroslav@49: *
jaroslav@49: * @param d a double
.
jaroslav@49: * @return a string representation of the double
argument.
jaroslav@49: * @see java.lang.Double#toString(double)
jaroslav@49: */
jaroslav@49: public static String valueOf(double d) {
jaroslav@49: return Double.toString(d);
jaroslav@49: }
jaroslav@49:
jaroslav@49: /**
jaroslav@49: * Returns a canonical representation for the string object.
jaroslav@49: * String
.
jaroslav@49: * String
object as determined by
jaroslav@49: * the {@link #equals(Object)} method, then the string from the pool is
jaroslav@49: * returned. Otherwise, this String
object is added to the
jaroslav@49: * pool and a reference to this String
object is returned.
jaroslav@49: * s
and t
,
jaroslav@49: * s.intern() == t.intern()
is true
jaroslav@49: * if and only if s.equals(t)
is true
.
jaroslav@49: *