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