< prev index next >

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

Print this page
rev 12069 : [mq]: 8058779-Faster-implementation-of-String-replace-SB


2230      * @since 1.4
2231      * @spec JSR-51
2232      */
2233     public String replaceAll(String regex, String replacement) {
2234         return Pattern.compile(regex).matcher(this).replaceAll(replacement);
2235     }
2236 
2237     /**
2238      * Replaces each substring of this string that matches the literal target
2239      * sequence with the specified literal replacement sequence. The
2240      * replacement proceeds from the beginning of the string to the end, for
2241      * example, replacing "aa" with "b" in the string "aaa" will result in
2242      * "ba" rather than "ab".
2243      *
2244      * @param  target The sequence of char values to be replaced
2245      * @param  replacement The replacement sequence of char values
2246      * @return  The resulting string
2247      * @since 1.5
2248      */
2249     public String replace(CharSequence target, CharSequence replacement) {
2250         return Pattern.compile(target.toString(), Pattern.LITERAL).matcher(
2251                 this).replaceAll(Matcher.quoteReplacement(replacement.toString()));




















2252     }
2253 
2254     /**
2255      * Splits this string around matches of the given
2256      * <a href="../util/regex/Pattern.html#sum">regular expression</a>.
2257      *
2258      * <p> The array returned by this method contains each substring of this
2259      * string that is terminated by another substring that matches the given
2260      * expression or is terminated by the end of the string.  The substrings in
2261      * the array are in the order in which they occur in this string.  If the
2262      * expression does not match any part of the input then the resulting array
2263      * has just one element, namely this string.
2264      *
2265      * <p> When there is a positive-width match at the beginning of this
2266      * string then an empty leading substring is included at the beginning
2267      * of the resulting array. A zero-width match at the beginning however
2268      * never produces such empty leading substring.
2269      *
2270      * <p> The {@code limit} parameter controls the number of times the
2271      * pattern is applied and therefore affects the length of the resulting




2230      * @since 1.4
2231      * @spec JSR-51
2232      */
2233     public String replaceAll(String regex, String replacement) {
2234         return Pattern.compile(regex).matcher(this).replaceAll(replacement);
2235     }
2236 
2237     /**
2238      * Replaces each substring of this string that matches the literal target
2239      * sequence with the specified literal replacement sequence. The
2240      * replacement proceeds from the beginning of the string to the end, for
2241      * example, replacing "aa" with "b" in the string "aaa" will result in
2242      * "ba" rather than "ab".
2243      *
2244      * @param  target The sequence of char values to be replaced
2245      * @param  replacement The replacement sequence of char values
2246      * @return  The resulting string
2247      * @since 1.5
2248      */
2249     public String replace(CharSequence target, CharSequence replacement) {
2250         String starget = target.toString();
2251         String srepl = replacement.toString();
2252         int i = 0, j = indexOf(starget);
2253         if (j < 0) {
2254             return this;
2255         }
2256         int targLen = starget.length();
2257         int targLen1 = Math.max(targLen, 1);
2258         final char[] value = this.value;
2259         final char[] replValue = srepl.value;
2260         int newLenHint = value.length - targLen + replValue.length;
2261         if (newLenHint < 0) {
2262             throw new OutOfMemoryError();
2263         }
2264         StringBuilder sb = new StringBuilder(newLenHint);
2265         do {
2266             sb.append(value, i, j - i)
2267                 .append(replValue);
2268             i = j + targLen;
2269         } while (j < value.length && (j = indexOf(starget, j + targLen1)) > 0);
2270 
2271         return sb.append(value, i, value.length - i).toString();
2272     }
2273 
2274     /**
2275      * Splits this string around matches of the given
2276      * <a href="../util/regex/Pattern.html#sum">regular expression</a>.
2277      *
2278      * <p> The array returned by this method contains each substring of this
2279      * string that is terminated by another substring that matches the given
2280      * expression or is terminated by the end of the string.  The substrings in
2281      * the array are in the order in which they occur in this string.  If the
2282      * expression does not match any part of the input then the resulting array
2283      * has just one element, namely this string.
2284      *
2285      * <p> When there is a positive-width match at the beginning of this
2286      * string then an empty leading substring is included at the beginning
2287      * of the resulting array. A zero-width match at the beginning however
2288      * never produces such empty leading substring.
2289      *
2290      * <p> The {@code limit} parameter controls the number of times the
2291      * pattern is applied and therefore affects the length of the resulting


< prev index next >