1881 i--;
1882 }
1883 if (i < min) {
1884 return -1;
1885 }
1886 int j = i - 1;
1887 int start = j - (targetCount - 1);
1888 int k = strLastIndex - 1;
1889
1890 while (j > start) {
1891 if (source[j--] != target[k--]) {
1892 i--;
1893 continue startSearchForLastChar;
1894 }
1895 }
1896 return start - sourceOffset + 1;
1897 }
1898 }
1899
1900 /**
1901 * Returns a new string that is a substring of this string. The
1902 * substring begins with the character at the specified index and
1903 * extends to the end of this string. <p>
1904 * Examples:
1905 * <blockquote><pre>
1906 * "unhappy".substring(2) returns "happy"
1907 * "Harbison".substring(3) returns "bison"
1908 * "emptiness".substring(9) returns "" (an empty string)
1909 * </pre></blockquote>
1910 *
1911 * @param beginIndex the beginning index, inclusive.
1912 * @return the specified substring.
1913 * @exception IndexOutOfBoundsException if
1914 * {@code beginIndex} is negative or larger than the
1915 * length of this {@code String} object.
1916 */
1917 public String substring(int beginIndex) {
1918 if (beginIndex < 0) {
1919 throw new StringIndexOutOfBoundsException(beginIndex);
1920 }
1921 int subLen = value.length - beginIndex;
1922 if (subLen < 0) {
1923 throw new StringIndexOutOfBoundsException(subLen);
1924 }
1925 return (beginIndex == 0) ? this : new String(value, beginIndex, subLen);
1926 }
1927
1928 /**
1929 * Returns a new string that is a substring of this string. The
1930 * substring begins at the specified {@code beginIndex} and
1931 * extends to the character at index {@code endIndex - 1}.
1932 * Thus the length of the substring is {@code endIndex-beginIndex}.
1933 * <p>
1934 * Examples:
1935 * <blockquote><pre>
1936 * "hamburger".substring(4, 8) returns "urge"
1937 * "smiles".substring(1, 5) returns "mile"
1938 * </pre></blockquote>
1939 *
1940 * @param beginIndex the beginning index, inclusive.
1941 * @param endIndex the ending index, exclusive.
1942 * @return the specified substring.
1943 * @exception IndexOutOfBoundsException if the
1944 * {@code beginIndex} is negative, or
1945 * {@code endIndex} is larger than the length of
1946 * this {@code String} object, or
1947 * {@code beginIndex} is larger than
1948 * {@code endIndex}.
1949 */
1950 public String substring(int beginIndex, int endIndex) {
1951 if (beginIndex < 0) {
1952 throw new StringIndexOutOfBoundsException(beginIndex);
1953 }
1954 if (endIndex > value.length) {
1955 throw new StringIndexOutOfBoundsException(endIndex);
1956 }
1957 int subLen = endIndex - beginIndex;
1958 if (subLen < 0) {
1959 throw new StringIndexOutOfBoundsException(subLen);
1960 }
1961 return ((beginIndex == 0) && (endIndex == value.length)) ? this
1962 : new String(value, beginIndex, subLen);
1963 }
1964
1965 /**
1966 * Returns a new character sequence that is a subsequence of this sequence.
1967 *
1968 * <p> An invocation of this method of the form
1969 *
1970 * <blockquote><pre>
1971 * str.subSequence(begin, end)</pre></blockquote>
1972 *
1973 * behaves in exactly the same way as the invocation
1974 *
1975 * <blockquote><pre>
1976 * str.substring(begin, end)</pre></blockquote>
1977 *
1978 * This method is defined so that the {@code String} class can implement
1979 * the {@link CharSequence} interface.
1980 *
1981 * @param beginIndex the begin index, inclusive.
1982 * @param endIndex the end index, exclusive.
1983 * @return the specified subsequence.
1984 *
1985 * @throws IndexOutOfBoundsException
1986 * if {@code beginIndex} or {@code endIndex} is negative,
1987 * if {@code endIndex} is greater than {@code length()},
1988 * or if {@code beginIndex} is greater than {@code endIndex}
1989 *
1990 * @since 1.4
1991 * @spec JSR-51
1992 */
1993 public CharSequence subSequence(int beginIndex, int endIndex) {
1994 return this.substring(beginIndex, endIndex);
1995 }
1996
1997 /**
1998 * Concatenates the specified string to the end of this string.
1999 * <p>
2000 * If the length of the argument string is {@code 0}, then this
2001 * {@code String} object is returned. Otherwise, a new
2002 * {@code String} object is created, representing a character
2003 * sequence that is the concatenation of the character sequence
2004 * represented by this {@code String} object and the character
2005 * sequence represented by the argument string.<p>
2006 * Examples:
2007 * <blockquote><pre>
2008 * "cares".concat("s") returns "caress"
2009 * "to".concat("get").concat("her") returns "together"
2010 * </pre></blockquote>
2011 *
2012 * @param str the {@code String} that is concatenated to the end
2013 * of this {@code String}.
2014 * @return a string that represents the concatenation of this object's
2015 * characters followed by the string argument's characters.
2016 */
2017 public String concat(String str) {
2018 int otherLen = str.length();
2019 if (otherLen == 0) {
2020 return this;
2021 }
2022 int len = value.length;
2023 char buf[] = Arrays.copyOf(value, len + otherLen);
2024 str.getChars(buf, len);
2025 return new String(buf, true);
2026 }
2027
2028 /**
2029 * Returns a new string resulting from replacing all occurrences of
2030 * {@code oldChar} in this string with {@code newChar}.
2031 * <p>
2032 * If the character {@code oldChar} does not occur in the
2033 * character sequence represented by this {@code String} object,
2034 * then a reference to this {@code String} object is returned.
2035 * Otherwise, a new {@code String} object is created that
2036 * represents a character sequence identical to the character sequence
2037 * represented by this {@code String} object, except that every
2038 * occurrence of {@code oldChar} is replaced by an occurrence
2039 * of {@code newChar}.
2040 * <p>
2041 * Examples:
2042 * <blockquote><pre>
2043 * "mesquite in your cellar".replace('e', 'o')
2044 * returns "mosquito in your collar"
2045 * "the war of baronets".replace('r', 'y')
2046 * returns "the way of bayonets"
2047 * "sparring with a purple porpoise".replace('p', 't')
2048 * returns "starring with a turtle tortoise"
2049 * "JonL".replace('q', 'x') returns "JonL" (no change)
2050 * </pre></blockquote>
2051 *
2052 * @param oldChar the old character.
2053 * @param newChar the new character.
2054 * @return a string derived from this string by replacing every
2055 * occurrence of {@code oldChar} with {@code newChar}.
2801 * <p>
2802 * <b>Note:</b> This method is locale sensitive, and may produce unexpected
2803 * results if used for strings that are intended to be interpreted locale
2804 * independently.
2805 * Examples are programming language identifiers, protocol keys, and HTML
2806 * tags.
2807 * For instance, {@code "title".toUpperCase()} in a Turkish locale
2808 * returns {@code "T\u005Cu0130TLE"}, where '\u005Cu0130' is the
2809 * LATIN CAPITAL LETTER I WITH DOT ABOVE character.
2810 * To obtain correct results for locale insensitive strings, use
2811 * {@code toUpperCase(Locale.ROOT)}.
2812 * <p>
2813 * @return the {@code String}, converted to uppercase.
2814 * @see java.lang.String#toUpperCase(Locale)
2815 */
2816 public String toUpperCase() {
2817 return toUpperCase(Locale.getDefault());
2818 }
2819
2820 /**
2821 * Returns a copy of the string, with leading and trailing whitespace
2822 * omitted.
2823 * <p>
2824 * If this {@code String} object represents an empty character
2825 * sequence, or the first and last characters of character sequence
2826 * represented by this {@code String} object both have codes
2827 * greater than {@code '\u005Cu0020'} (the space character), then a
2828 * reference to this {@code String} object is returned.
2829 * <p>
2830 * Otherwise, if there is no character with a code greater than
2831 * {@code '\u005Cu0020'} in the string, then a new
2832 * {@code String} object representing an empty string is created
2833 * and returned.
2834 * <p>
2835 * Otherwise, let <i>k</i> be the index of the first character in the
2836 * string whose code is greater than {@code '\u005Cu0020'}, and let
2837 * <i>m</i> be the index of the last character in the string whose code
2838 * is greater than {@code '\u005Cu0020'}. A new {@code String}
2839 * object is created, representing the substring of this string that
2840 * begins with the character at index <i>k</i> and ends with the
2841 * character at index <i>m</i>-that is, the result of
2842 * {@code this.substring(k, m + 1)}.
2843 * <p>
2844 * This method may be used to trim whitespace (as defined above) from
2845 * the beginning and end of a string.
2846 *
2847 * @return A copy of this string with leading and trailing white
2848 * space removed, or this string if it has no leading or
2849 * trailing white space.
2850 */
2851 public String trim() {
2852 int len = value.length;
2853 int st = 0;
2854 char[] val = value; /* avoid getfield opcode */
2855
2856 while ((st < len) && (val[st] <= ' ')) {
2857 st++;
2858 }
2859 while ((st < len) && (val[len - 1] <= ' ')) {
2860 len--;
2861 }
2862 return ((st > 0) || (len < value.length)) ? substring(st, len) : this;
2863 }
2864
2865 /**
2866 * This object (which is already a string!) is itself returned.
2867 *
2964 public static String format(Locale l, String format, Object... args) {
2965 return new Formatter(l).format(format, args).toString();
2966 }
2967
2968 /**
2969 * Returns the string representation of the {@code Object} argument.
2970 *
2971 * @param obj an {@code Object}.
2972 * @return if the argument is {@code null}, then a string equal to
2973 * {@code "null"}; otherwise, the value of
2974 * {@code obj.toString()} is returned.
2975 * @see java.lang.Object#toString()
2976 */
2977 public static String valueOf(Object obj) {
2978 return (obj == null) ? "null" : obj.toString();
2979 }
2980
2981 /**
2982 * Returns the string representation of the {@code char} array
2983 * argument. The contents of the character array are copied; subsequent
2984 * modification of the character array does not affect the newly
2985 * created string.
2986 *
2987 * @param data a {@code char} array.
2988 * @return a newly allocated string representing the same sequence of
2989 * characters contained in the character array argument.
2990 */
2991 public static String valueOf(char data[]) {
2992 return new String(data);
2993 }
2994
2995 /**
2996 * Returns the string representation of a specific subarray of the
2997 * {@code char} array argument.
2998 * <p>
2999 * The {@code offset} argument is the index of the first
3000 * character of the subarray. The {@code count} argument
3001 * specifies the length of the subarray. The contents of the subarray
3002 * are copied; subsequent modification of the character array does not
3003 * affect the newly created string.
3004 *
3005 * @param data the character array.
3006 * @param offset the initial offset into the value of the
3007 * {@code String}.
3008 * @param count the length of the value of the {@code String}.
3009 * @return a string representing the sequence of characters contained
3010 * in the subarray of the character array argument.
3011 * @exception IndexOutOfBoundsException if {@code offset} is
3012 * negative, or {@code count} is negative, or
3013 * {@code offset+count} is larger than
3014 * {@code data.length}.
3015 */
3016 public static String valueOf(char data[], int offset, int count) {
3017 return new String(data, offset, count);
3018 }
3019
3020 /**
3021 * Returns a String that represents the character sequence in the
3022 * array specified.
3023 *
3024 * @param data the character array.
3025 * @param offset initial offset of the subarray.
3026 * @param count length of the subarray.
3027 * @return a {@code String} that contains the characters of the
3028 * specified subarray of the character array.
3029 */
3030 public static String copyValueOf(char data[], int offset, int count) {
3031 // All public String constructors now copy the data.
3032 return new String(data, offset, count);
3033 }
3034
3035 /**
3036 * Returns a String that represents the character sequence in the
3037 * array specified.
3038 *
3039 * @param data the character array.
3040 * @return a {@code String} that contains the characters of the
3041 * character array.
3042 */
3043 public static String copyValueOf(char data[]) {
3044 return new String(data);
3045 }
3046
3047 /**
3048 * Returns the string representation of the {@code boolean} argument.
3049 *
3050 * @param b a {@code boolean}.
3051 * @return if the argument is {@code true}, a string equal to
3052 * {@code "true"} is returned; otherwise, a string equal to
3053 * {@code "false"} is returned.
3054 */
3055 public static String valueOf(boolean b) {
3056 return b ? "true" : "false";
3057 }
|
1881 i--;
1882 }
1883 if (i < min) {
1884 return -1;
1885 }
1886 int j = i - 1;
1887 int start = j - (targetCount - 1);
1888 int k = strLastIndex - 1;
1889
1890 while (j > start) {
1891 if (source[j--] != target[k--]) {
1892 i--;
1893 continue startSearchForLastChar;
1894 }
1895 }
1896 return start - sourceOffset + 1;
1897 }
1898 }
1899
1900 /**
1901 * Returns a string that is a substring of this string. The
1902 * substring begins with the character at the specified index and
1903 * extends to the end of this string. <p>
1904 * Examples:
1905 * <blockquote><pre>
1906 * "unhappy".substring(2) returns "happy"
1907 * "Harbison".substring(3) returns "bison"
1908 * "emptiness".substring(9) returns "" (an empty string)
1909 * </pre></blockquote>
1910 *
1911 * @param beginIndex the beginning index, inclusive.
1912 * @return the specified substring.
1913 * @exception IndexOutOfBoundsException if
1914 * {@code beginIndex} is negative or larger than the
1915 * length of this {@code String} object.
1916 */
1917 public String substring(int beginIndex) {
1918 if (beginIndex < 0) {
1919 throw new StringIndexOutOfBoundsException(beginIndex);
1920 }
1921 int subLen = value.length - beginIndex;
1922 if (subLen < 0) {
1923 throw new StringIndexOutOfBoundsException(subLen);
1924 }
1925 return (beginIndex == 0) ? this : new String(value, beginIndex, subLen);
1926 }
1927
1928 /**
1929 * Returns a string that is a substring of this string. The
1930 * substring begins at the specified {@code beginIndex} and
1931 * extends to the character at index {@code endIndex - 1}.
1932 * Thus the length of the substring is {@code endIndex-beginIndex}.
1933 * <p>
1934 * Examples:
1935 * <blockquote><pre>
1936 * "hamburger".substring(4, 8) returns "urge"
1937 * "smiles".substring(1, 5) returns "mile"
1938 * </pre></blockquote>
1939 *
1940 * @param beginIndex the beginning index, inclusive.
1941 * @param endIndex the ending index, exclusive.
1942 * @return the specified substring.
1943 * @exception IndexOutOfBoundsException if the
1944 * {@code beginIndex} is negative, or
1945 * {@code endIndex} is larger than the length of
1946 * this {@code String} object, or
1947 * {@code beginIndex} is larger than
1948 * {@code endIndex}.
1949 */
1950 public String substring(int beginIndex, int endIndex) {
1951 if (beginIndex < 0) {
1952 throw new StringIndexOutOfBoundsException(beginIndex);
1953 }
1954 if (endIndex > value.length) {
1955 throw new StringIndexOutOfBoundsException(endIndex);
1956 }
1957 int subLen = endIndex - beginIndex;
1958 if (subLen < 0) {
1959 throw new StringIndexOutOfBoundsException(subLen);
1960 }
1961 return ((beginIndex == 0) && (endIndex == value.length)) ? this
1962 : new String(value, beginIndex, subLen);
1963 }
1964
1965 /**
1966 * Returns a character sequence that is a subsequence of this sequence.
1967 *
1968 * <p> An invocation of this method of the form
1969 *
1970 * <blockquote><pre>
1971 * str.subSequence(begin, end)</pre></blockquote>
1972 *
1973 * behaves in exactly the same way as the invocation
1974 *
1975 * <blockquote><pre>
1976 * str.substring(begin, end)</pre></blockquote>
1977 *
1978 * @apiNote
1979 * This method is defined so that the {@code String} class can implement
1980 * the {@link CharSequence} interface.
1981 *
1982 * @param beginIndex the begin index, inclusive.
1983 * @param endIndex the end index, exclusive.
1984 * @return the specified substring.
1985 *
1986 * @throws IndexOutOfBoundsException
1987 * if {@code beginIndex} or {@code endIndex} is negative,
1988 * if {@code endIndex} is greater than {@code length()},
1989 * or if {@code beginIndex} is greater than {@code endIndex}
1990 *
1991 * @since 1.4
1992 * @spec JSR-51
1993 */
1994 public CharSequence subSequence(int beginIndex, int endIndex) {
1995 return this.substring(beginIndex, endIndex);
1996 }
1997
1998 /**
1999 * Concatenates the specified string to the end of this string.
2000 * <p>
2001 * If the length of the argument string is {@code 0}, then this
2002 * {@code String} object is returned. Otherwise, a
2003 * {@code String} object is returned, representing a character
2004 * sequence that is the concatenation of the character sequence
2005 * represented by this {@code String} object and the character
2006 * sequence represented by the argument string.<p>
2007 * Examples:
2008 * <blockquote><pre>
2009 * "cares".concat("s") returns "caress"
2010 * "to".concat("get").concat("her") returns "together"
2011 * </pre></blockquote>
2012 *
2013 * @param str the {@code String} that is concatenated to the end
2014 * of this {@code String}.
2015 * @return a string that represents the concatenation of this object's
2016 * characters followed by the string argument's characters.
2017 */
2018 public String concat(String str) {
2019 int otherLen = str.length();
2020 if (otherLen == 0) {
2021 return this;
2022 }
2023 int len = value.length;
2024 char buf[] = Arrays.copyOf(value, len + otherLen);
2025 str.getChars(buf, len);
2026 return new String(buf, true);
2027 }
2028
2029 /**
2030 * Returns a string resulting from replacing all occurrences of
2031 * {@code oldChar} in this string with {@code newChar}.
2032 * <p>
2033 * If the character {@code oldChar} does not occur in the
2034 * character sequence represented by this {@code String} object,
2035 * then a reference to this {@code String} object is returned.
2036 * Otherwise, a {@code String} object is returned that
2037 * represents a character sequence identical to the character sequence
2038 * represented by this {@code String} object, except that every
2039 * occurrence of {@code oldChar} is replaced by an occurrence
2040 * of {@code newChar}.
2041 * <p>
2042 * Examples:
2043 * <blockquote><pre>
2044 * "mesquite in your cellar".replace('e', 'o')
2045 * returns "mosquito in your collar"
2046 * "the war of baronets".replace('r', 'y')
2047 * returns "the way of bayonets"
2048 * "sparring with a purple porpoise".replace('p', 't')
2049 * returns "starring with a turtle tortoise"
2050 * "JonL".replace('q', 'x') returns "JonL" (no change)
2051 * </pre></blockquote>
2052 *
2053 * @param oldChar the old character.
2054 * @param newChar the new character.
2055 * @return a string derived from this string by replacing every
2056 * occurrence of {@code oldChar} with {@code newChar}.
2802 * <p>
2803 * <b>Note:</b> This method is locale sensitive, and may produce unexpected
2804 * results if used for strings that are intended to be interpreted locale
2805 * independently.
2806 * Examples are programming language identifiers, protocol keys, and HTML
2807 * tags.
2808 * For instance, {@code "title".toUpperCase()} in a Turkish locale
2809 * returns {@code "T\u005Cu0130TLE"}, where '\u005Cu0130' is the
2810 * LATIN CAPITAL LETTER I WITH DOT ABOVE character.
2811 * To obtain correct results for locale insensitive strings, use
2812 * {@code toUpperCase(Locale.ROOT)}.
2813 * <p>
2814 * @return the {@code String}, converted to uppercase.
2815 * @see java.lang.String#toUpperCase(Locale)
2816 */
2817 public String toUpperCase() {
2818 return toUpperCase(Locale.getDefault());
2819 }
2820
2821 /**
2822 * Returns a string whose value is this string, with any leading and trailing
2823 * whitespace removed.
2824 * <p>
2825 * If this {@code String} object represents an empty character
2826 * sequence, or the first and last characters of character sequence
2827 * represented by this {@code String} object both have codes
2828 * greater than {@code '\u005Cu0020'} (the space character), then a
2829 * reference to this {@code String} object is returned.
2830 * <p>
2831 * Otherwise, if there is no character with a code greater than
2832 * {@code '\u005Cu0020'} in the string, then a
2833 * {@code String} object representing an empty string is
2834 * returned.
2835 * <p>
2836 * Otherwise, let <i>k</i> be the index of the first character in the
2837 * string whose code is greater than {@code '\u005Cu0020'}, and let
2838 * <i>m</i> be the index of the last character in the string whose code
2839 * is greater than {@code '\u005Cu0020'}. A {@code String}
2840 * object is returned, representing the substring of this string that
2841 * begins with the character at index <i>k</i> and ends with the
2842 * character at index <i>m</i>-that is, the result of
2843 * {@code this.substring(k, m + 1)}.
2844 * <p>
2845 * This method may be used to trim whitespace (as defined above) from
2846 * the beginning and end of a string.
2847 *
2848 * @return A string whose value is this string, with any leading and trailing white
2849 * space removed, or this string if it has no leading or
2850 * trailing white space.
2851 */
2852 public String trim() {
2853 int len = value.length;
2854 int st = 0;
2855 char[] val = value; /* avoid getfield opcode */
2856
2857 while ((st < len) && (val[st] <= ' ')) {
2858 st++;
2859 }
2860 while ((st < len) && (val[len - 1] <= ' ')) {
2861 len--;
2862 }
2863 return ((st > 0) || (len < value.length)) ? substring(st, len) : this;
2864 }
2865
2866 /**
2867 * This object (which is already a string!) is itself returned.
2868 *
2965 public static String format(Locale l, String format, Object... args) {
2966 return new Formatter(l).format(format, args).toString();
2967 }
2968
2969 /**
2970 * Returns the string representation of the {@code Object} argument.
2971 *
2972 * @param obj an {@code Object}.
2973 * @return if the argument is {@code null}, then a string equal to
2974 * {@code "null"}; otherwise, the value of
2975 * {@code obj.toString()} is returned.
2976 * @see java.lang.Object#toString()
2977 */
2978 public static String valueOf(Object obj) {
2979 return (obj == null) ? "null" : obj.toString();
2980 }
2981
2982 /**
2983 * Returns the string representation of the {@code char} array
2984 * argument. The contents of the character array are copied; subsequent
2985 * modification of the character array does not affect the returned
2986 * string.
2987 *
2988 * @param data the character array.
2989 * @return a {@code String} that contains the characters of the
2990 * character array.
2991 */
2992 public static String valueOf(char data[]) {
2993 return new String(data);
2994 }
2995
2996 /**
2997 * Returns the string representation of a specific subarray of the
2998 * {@code char} array argument.
2999 * <p>
3000 * The {@code offset} argument is the index of the first
3001 * character of the subarray. The {@code count} argument
3002 * specifies the length of the subarray. The contents of the subarray
3003 * are copied; subsequent modification of the character array does not
3004 * affect the returned string.
3005 *
3006 * @param data the character array.
3007 * @param offset initial offset of the subarray.
3008 * @param count length of the subarray.
3009 * @return a {@code String} that contains the characters of the
3010 * specified subarray of the character array.
3011 * @exception IndexOutOfBoundsException if {@code offset} is
3012 * negative, or {@code count} is negative, or
3013 * {@code offset+count} is larger than
3014 * {@code data.length}.
3015 */
3016 public static String valueOf(char data[], int offset, int count) {
3017 return new String(data, offset, count);
3018 }
3019
3020 /**
3021 * Identical to {@link #valueOf(char[], int, int)}.
3022 *
3023 * @param data the character array.
3024 * @param offset initial offset of the subarray.
3025 * @param count length of the subarray.
3026 * @return a {@code String} that contains the characters of the
3027 * specified subarray of the character array.
3028 * @exception IndexOutOfBoundsException if {@code offset} is
3029 * negative, or {@code count} is negative, or
3030 * {@code offset+count} is larger than
3031 * {@code data.length}.
3032 */
3033 public static String copyValueOf(char data[], int offset, int count) {
3034 return new String(data, offset, count);
3035 }
3036
3037 /**
3038 * Identical to {@link #valueOf(char[])}.
3039 *
3040 * @param data the character array.
3041 * @return a {@code String} that contains the characters of the
3042 * character array.
3043 */
3044 public static String copyValueOf(char data[]) {
3045 return new String(data);
3046 }
3047
3048 /**
3049 * Returns the string representation of the {@code boolean} argument.
3050 *
3051 * @param b a {@code boolean}.
3052 * @return if the argument is {@code true}, a string equal to
3053 * {@code "true"} is returned; otherwise, a string equal to
3054 * {@code "false"} is returned.
3055 */
3056 public static String valueOf(boolean b) {
3057 return b ? "true" : "false";
3058 }
|