< prev index next >

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

Print this page
rev 12026 : [mq]: 8058779-Faster-implementation-of-String-replace-MIDDLE


  23  * questions.
  24  */
  25 
  26 package java.lang;
  27 
  28 import java.io.ObjectStreamField;
  29 import java.io.UnsupportedEncodingException;
  30 import java.nio.charset.Charset;
  31 import java.util.ArrayList;
  32 import java.util.Arrays;
  33 import java.util.Comparator;
  34 import java.util.Formatter;
  35 import java.util.Locale;
  36 import java.util.Objects;
  37 import java.util.Spliterator;
  38 import java.util.StringJoiner;
  39 import java.util.function.IntConsumer;
  40 import java.util.regex.Matcher;
  41 import java.util.regex.Pattern;
  42 import java.util.regex.PatternSyntaxException;

  43 import java.util.stream.IntStream;
  44 import java.util.stream.StreamSupport;
  45 
  46 /**
  47  * The {@code String} class represents character strings. All
  48  * string literals in Java programs, such as {@code "abc"}, are
  49  * implemented as instances of this class.
  50  * <p>
  51  * Strings are constant; their values cannot be changed after they
  52  * are created. String buffers support mutable strings.
  53  * Because String objects are immutable they can be shared. For example:
  54  * <blockquote><pre>
  55  *     String str = "abc";
  56  * </pre></blockquote><p>
  57  * is equivalent to:
  58  * <blockquote><pre>
  59  *     char data[] = {'a', 'b', 'c'};
  60  *     String str = new String(data);
  61  * </pre></blockquote><p>
  62  * Here are some more examples of how strings can be used:


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




  23  * questions.
  24  */
  25 
  26 package java.lang;
  27 
  28 import java.io.ObjectStreamField;
  29 import java.io.UnsupportedEncodingException;
  30 import java.nio.charset.Charset;
  31 import java.util.ArrayList;
  32 import java.util.Arrays;
  33 import java.util.Comparator;
  34 import java.util.Formatter;
  35 import java.util.Locale;
  36 import java.util.Objects;
  37 import java.util.Spliterator;
  38 import java.util.StringJoiner;
  39 import java.util.function.IntConsumer;
  40 import java.util.regex.Matcher;
  41 import java.util.regex.Pattern;
  42 import java.util.regex.PatternSyntaxException;
  43 import java.util.stream.Collectors;
  44 import java.util.stream.IntStream;
  45 import java.util.stream.StreamSupport;
  46 
  47 /**
  48  * The {@code String} class represents character strings. All
  49  * string literals in Java programs, such as {@code "abc"}, are
  50  * implemented as instances of this class.
  51  * <p>
  52  * Strings are constant; their values cannot be changed after they
  53  * are created. String buffers support mutable strings.
  54  * Because String objects are immutable they can be shared. For example:
  55  * <blockquote><pre>
  56  *     String str = "abc";
  57  * </pre></blockquote><p>
  58  * is equivalent to:
  59  * <blockquote><pre>
  60  *     char data[] = {'a', 'b', 'c'};
  61  *     String str = new String(data);
  62  * </pre></blockquote><p>
  63  * Here are some more examples of how strings can be used:


2231      * @since 1.4
2232      * @spec JSR-51
2233      */
2234     public String replaceAll(String regex, String replacement) {
2235         return Pattern.compile(regex).matcher(this).replaceAll(replacement);
2236     }
2237 
2238     /**
2239      * Replaces each substring of this string that matches the literal target
2240      * sequence with the specified literal replacement sequence. The
2241      * replacement proceeds from the beginning of the string to the end, for
2242      * example, replacing "aa" with "b" in the string "aaa" will result in
2243      * "ba" rather than "ab".
2244      *
2245      * @param  target The sequence of char values to be replaced
2246      * @param  replacement The replacement sequence of char values
2247      * @return  The resulting string
2248      * @since 1.5
2249      */
2250     public String replace(CharSequence target, CharSequence replacement) {
2251         String starget = target.toString();
2252         int targLen = starget.length();
2253         String srepl = replacement.toString();
2254 
2255         // special case: replacing empty substrings
2256         if (targLen == 0) {
2257             return splitAndJoin(srepl);
2258         }
2259 
2260         int i = indexOf(starget);
2261         // special case: nothing to replace
2262         if (i < 0) {
2263             return this;
2264         }
2265 
2266         StringJoiner joiner = new StringJoiner(replacement);
2267         joiner.add(substring(0, i));
2268         i += targLen;
2269         int j;
2270         while ((j = indexOf(starget, i)) > 0) {
2271             joiner.add(substring(i, j));
2272             i = j + targLen;
2273         }
2274         return joiner.add(substring(i)).toString();
2275     }
2276 
2277     /**
2278      * Returns this string with every empty substring
2279      * replaced by the given delimiter.
2280      * If this string is empty, the delimiter is returned.
2281      * If the delimiter is empty, unchanged this string
2282      * is returned.
2283      */
2284     private String splitAndJoin(String delimiter) {
2285         if (isEmpty()) {
2286             return delimiter;
2287         }
2288         if (delimiter.isEmpty()) {
2289             return this;
2290         }
2291         return chars()
2292                 .mapToObj(i -> String.valueOf((char)i))
2293                 .collect(Collectors.joining(delimiter,
2294                                             delimiter,
2295                                             delimiter));
2296     }
2297 
2298     /**
2299      * Splits this string around matches of the given
2300      * <a href="../util/regex/Pattern.html#sum">regular expression</a>.
2301      *
2302      * <p> The array returned by this method contains each substring of this
2303      * string that is terminated by another substring that matches the given
2304      * expression or is terminated by the end of the string.  The substrings in
2305      * the array are in the order in which they occur in this string.  If the
2306      * expression does not match any part of the input then the resulting array
2307      * has just one element, namely this string.
2308      *
2309      * <p> When there is a positive-width match at the beginning of this
2310      * string then an empty leading substring is included at the beginning
2311      * of the resulting array. A zero-width match at the beginning however
2312      * never produces such empty leading substring.
2313      *
2314      * <p> The {@code limit} parameter controls the number of times the
2315      * pattern is applied and therefore affects the length of the resulting


< prev index next >