< prev index next >

src/java.base/share/classes/java/lang/String.java

Print this page
rev 54615 : [mq]: 8222955-Optimize-String-replace-CharSequence-CharSequence-for-Latin1-encoded-strings


2143      * @since 1.4
2144      * @spec JSR-51
2145      */
2146     public String replaceAll(String regex, String replacement) {
2147         return Pattern.compile(regex).matcher(this).replaceAll(replacement);
2148     }
2149 
2150     /**
2151      * Replaces each substring of this string that matches the literal target
2152      * sequence with the specified literal replacement sequence. The
2153      * replacement proceeds from the beginning of the string to the end, for
2154      * example, replacing "aa" with "b" in the string "aaa" will result in
2155      * "ba" rather than "ab".
2156      *
2157      * @param  target The sequence of char values to be replaced
2158      * @param  replacement The replacement sequence of char values
2159      * @return  The resulting string
2160      * @since 1.5
2161      */
2162     public String replace(CharSequence target, CharSequence replacement) {

2163         String tgtStr = target.toString();

2164         String replStr = replacement.toString();















2165         int j = indexOf(tgtStr);
2166         if (j < 0) {
2167             return this;
2168         }
2169         int tgtLen = tgtStr.length();
2170         int tgtLen1 = Math.max(tgtLen, 1);
2171         int thisLen = length();
2172 
2173         int newLenHint = thisLen - tgtLen + replStr.length();
2174         if (newLenHint < 0) {
2175             throw new OutOfMemoryError();
2176         }
2177         StringBuilder sb = new StringBuilder(newLenHint);
2178         int i = 0;
2179         do {
2180             sb.append(this, i, j).append(replStr);
2181             i = j + tgtLen;
2182         } while (j < thisLen && (j = indexOf(tgtStr, j + tgtLen1)) > 0);
2183         return sb.append(this, i, thisLen).toString();
2184     }
2185 
2186     /**
2187      * Splits this string around matches of the given
2188      * <a href="../util/regex/Pattern.html#sum">regular expression</a>.
2189      *
2190      * <p> The array returned by this method contains each substring of this
2191      * string that is terminated by another substring that matches the given
2192      * expression or is terminated by the end of the string.  The substrings in
2193      * the array are in the order in which they occur in this string.  If the




2143      * @since 1.4
2144      * @spec JSR-51
2145      */
2146     public String replaceAll(String regex, String replacement) {
2147         return Pattern.compile(regex).matcher(this).replaceAll(replacement);
2148     }
2149 
2150     /**
2151      * Replaces each substring of this string that matches the literal target
2152      * sequence with the specified literal replacement sequence. The
2153      * replacement proceeds from the beginning of the string to the end, for
2154      * example, replacing "aa" with "b" in the string "aaa" will result in
2155      * "ba" rather than "ab".
2156      *
2157      * @param  target The sequence of char values to be replaced
2158      * @param  replacement The replacement sequence of char values
2159      * @return  The resulting string
2160      * @since 1.5
2161      */
2162     public String replace(CharSequence target, CharSequence replacement) {
2163         int thisLen = length();
2164         String tgtStr = target.toString();
2165         int tgtLen = tgtStr.length();
2166         String replStr = replacement.toString();
2167         int replLen = replStr.length();
2168         if (tgtLen > 0) {
2169             if (tgtLen == 1 && replLen == 1) {
2170                 return replace(tgtStr.charAt(0), replStr.charAt(0));
2171             }
2172             if (this.isLatin1() && tgtStr.isLatin1() && replStr.isLatin1()) {
2173                 String ret = StringLatin1.replace(value, thisLen, tgtStr.value,
2174                                                   tgtLen, replStr.value, replLen);
2175                 if (ret != null) {
2176                     return ret;
2177                 }
2178                 return this;
2179             }
2180         }
2181 
2182         int j = indexOf(tgtStr);
2183         if (j < 0) {
2184             return this;
2185         }
2186         int tgtLen1 = tgtLen > 0 ? tgtLen : 1;


2187 
2188         int newLenHint = thisLen - tgtLen + replLen;
2189         if (newLenHint < 0) {
2190             throw new OutOfMemoryError();
2191         }
2192         StringBuilder sb = new StringBuilder(newLenHint);
2193         int i = 0;
2194         do {
2195             sb.append(this, i, j).append(replStr);
2196             i = j + tgtLen;
2197         } while (j < thisLen && (j = indexOf(tgtStr, j + tgtLen1)) > 0);
2198         return sb.append(this, i, thisLen).toString();
2199     }
2200 
2201     /**
2202      * Splits this string around matches of the given
2203      * <a href="../util/regex/Pattern.html#sum">regular expression</a>.
2204      *
2205      * <p> The array returned by this method contains each substring of this
2206      * string that is terminated by another substring that matches the given
2207      * expression or is terminated by the end of the string.  The substrings in
2208      * the array are in the order in which they occur in this string.  If the


< prev index next >