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

Print this page




2218      *
2219      * @param  target The sequence of char values to be replaced
2220      * @param  replacement The replacement sequence of char values
2221      * @return  The resulting string
2222      * @since 1.5
2223      */
2224     public String replace(CharSequence target, CharSequence replacement) {
2225         return Pattern.compile(target.toString(), Pattern.LITERAL).matcher(
2226                 this).replaceAll(Matcher.quoteReplacement(replacement.toString()));
2227     }
2228 
2229     /**
2230      * Splits this string around matches of the given
2231      * <a href="../util/regex/Pattern.html#sum">regular expression</a>.
2232      *
2233      * <p> The array returned by this method contains each substring of this
2234      * string that is terminated by another substring that matches the given
2235      * expression or is terminated by the end of the string.  The substrings in
2236      * the array are in the order in which they occur in this string.  If the
2237      * expression does not match any part of the input then the resulting array
2238      * has just one element, namely this string.






2239      *
2240      * <p> The {@code limit} parameter controls the number of times the
2241      * pattern is applied and therefore affects the length of the resulting
2242      * array.  If the limit <i>n</i> is greater than zero then the pattern
2243      * will be applied at most <i>n</i>&nbsp;-&nbsp;1 times, the array's
2244      * length will be no greater than <i>n</i>, and the array's last entry
2245      * will contain all input beyond the last matched delimiter.  If <i>n</i>
2246      * is non-positive then the pattern will be applied as many times as
2247      * possible and the array can have any length.  If <i>n</i> is zero then
2248      * the pattern will be applied as many times as possible, the array can
2249      * have any length, and trailing empty strings will be discarded.
2250      *
2251      * <p> The string {@code "boo:and:foo"}, for example, yields the
2252      * following results with these parameters:
2253      *
2254      * <blockquote><table cellpadding=1 cellspacing=0 summary="Split example showing regex, limit, and result">
2255      * <tr>
2256      *     <th>Regex</th>
2257      *     <th>Limit</th>
2258      *     <th>Result</th>


2308      * @spec JSR-51
2309      */
2310     public String[] split(String regex, int limit) {
2311         /* fastpath if the regex is a
2312          (1)one-char String and this character is not one of the
2313             RegEx's meta characters ".$|()[{^?*+\\", or
2314          (2)two-char String and the first char is the backslash and
2315             the second is not the ascii digit or ascii letter.
2316          */
2317         char ch = 0;
2318         if (((regex.value.length == 1 &&
2319              ".$|()[{^?*+\\".indexOf(ch = regex.charAt(0)) == -1) ||
2320              (regex.length() == 2 &&
2321               regex.charAt(0) == '\\' &&
2322               (((ch = regex.charAt(1))-'0')|('9'-ch)) < 0 &&
2323               ((ch-'a')|('z'-ch)) < 0 &&
2324               ((ch-'A')|('Z'-ch)) < 0)) &&
2325             (ch < Character.MIN_HIGH_SURROGATE ||
2326              ch > Character.MAX_LOW_SURROGATE))
2327         {


2328             int off = 0;
2329             int next = 0;
2330             boolean limited = limit > 0;
2331             ArrayList<String> list = new ArrayList<>();
2332             while ((next = indexOf(ch, off)) != -1) {
2333                 if (!limited || list.size() < limit - 1) {
2334                     list.add(substring(off, next));
2335                     off = next + 1;
2336                 } else {    // last one
2337                     //assert (list.size() == limit - 1);
2338                     list.add(substring(off, value.length));
2339                     off = value.length;
2340                     break;
2341                 }
2342             }
2343             // If no match was found, return this
2344             if (off == 0)
2345                 return new String[]{this};
2346 
2347             // Add remaining segment




2218      *
2219      * @param  target The sequence of char values to be replaced
2220      * @param  replacement The replacement sequence of char values
2221      * @return  The resulting string
2222      * @since 1.5
2223      */
2224     public String replace(CharSequence target, CharSequence replacement) {
2225         return Pattern.compile(target.toString(), Pattern.LITERAL).matcher(
2226                 this).replaceAll(Matcher.quoteReplacement(replacement.toString()));
2227     }
2228 
2229     /**
2230      * Splits this string around matches of the given
2231      * <a href="../util/regex/Pattern.html#sum">regular expression</a>.
2232      *
2233      * <p> The array returned by this method contains each substring of this
2234      * string that is terminated by another substring that matches the given
2235      * expression or is terminated by the end of the string.  The substrings in
2236      * the array are in the order in which they occur in this string.  If the
2237      * expression does not match any part of the input then the resulting array
2238      * has just one element, namely this string. A zero-length input sequence
2239      * always results zero-length resulting array.
2240      *
2241      * <p> When there is a positive-width match at the beginning of this
2242      * string then an empty leading substring is included at the beginning
2243      * of the resulting array. A zero-width match at the beginning however
2244      * never produces such empty leading substring.
2245      *
2246      * <p> The {@code limit} parameter controls the number of times the
2247      * pattern is applied and therefore affects the length of the resulting
2248      * array.  If the limit <i>n</i> is greater than zero then the pattern
2249      * will be applied at most <i>n</i>&nbsp;-&nbsp;1 times, the array's
2250      * length will be no greater than <i>n</i>, and the array's last entry
2251      * will contain all input beyond the last matched delimiter.  If <i>n</i>
2252      * is non-positive then the pattern will be applied as many times as
2253      * possible and the array can have any length.  If <i>n</i> is zero then
2254      * the pattern will be applied as many times as possible, the array can
2255      * have any length, and trailing empty strings will be discarded.
2256      *
2257      * <p> The string {@code "boo:and:foo"}, for example, yields the
2258      * following results with these parameters:
2259      *
2260      * <blockquote><table cellpadding=1 cellspacing=0 summary="Split example showing regex, limit, and result">
2261      * <tr>
2262      *     <th>Regex</th>
2263      *     <th>Limit</th>
2264      *     <th>Result</th>


2314      * @spec JSR-51
2315      */
2316     public String[] split(String regex, int limit) {
2317         /* fastpath if the regex is a
2318          (1)one-char String and this character is not one of the
2319             RegEx's meta characters ".$|()[{^?*+\\", or
2320          (2)two-char String and the first char is the backslash and
2321             the second is not the ascii digit or ascii letter.
2322          */
2323         char ch = 0;
2324         if (((regex.value.length == 1 &&
2325              ".$|()[{^?*+\\".indexOf(ch = regex.charAt(0)) == -1) ||
2326              (regex.length() == 2 &&
2327               regex.charAt(0) == '\\' &&
2328               (((ch = regex.charAt(1))-'0')|('9'-ch)) < 0 &&
2329               ((ch-'a')|('z'-ch)) < 0 &&
2330               ((ch-'A')|('Z'-ch)) < 0)) &&
2331             (ch < Character.MIN_HIGH_SURROGATE ||
2332              ch > Character.MAX_LOW_SURROGATE))
2333         {
2334             if (value.length == 0)
2335                 return new String[0];
2336             int off = 0;
2337             int next = 0;
2338             boolean limited = limit > 0;
2339             ArrayList<String> list = new ArrayList<>();
2340             while ((next = indexOf(ch, off)) != -1) {
2341                 if (!limited || list.size() < limit - 1) {
2342                     list.add(substring(off, next));
2343                     off = next + 1;
2344                 } else {    // last one
2345                     //assert (list.size() == limit - 1);
2346                     list.add(substring(off, value.length));
2347                     off = value.length;
2348                     break;
2349                 }
2350             }
2351             // If no match was found, return this
2352             if (off == 0)
2353                 return new String[]{this};
2354 
2355             // Add remaining segment