Java String Replace replaceAll(String source, String toReplace, String replacement)

Here you can find the source of replaceAll(String source, String toReplace, String replacement)

Description

replace All

License

Open Source License

Declaration

public static String replaceAll(String source, String toReplace, String replacement) 

Method Source Code

//package com.java2s;
/*//w ww .  j ava  2 s.co m
 * Copyright (C) ${year} Omry Yadan <${email}>
 * All rights reserved.
 *
 * See https://github.com/omry/banana/blob/master/BSD-LICENSE for licensing information
 */

import java.util.*;

public class Main {
    public static String replaceAll(String source, String toReplace, String replacement) {
        int idx = source.lastIndexOf(toReplace);
        if (idx != -1) {
            StringBuffer ret = new StringBuffer(source);
            ret.replace(idx, idx + toReplace.length(), replacement);
            while ((idx = source.lastIndexOf(toReplace, idx - 1)) != -1) {
                ret.replace(idx, idx + toReplace.length(), replacement);
            }
            source = ret.toString();
        }

        return source;
    }

    public static String toString(Collection<String> list) {
        return toString(list.toArray(new String[list.size()]));
    }

    public static String toString(Object array[]) {
        return toString(array, ",");
    }

    public static String toString(int array[]) {
        return toString(array, ",");
    }

    public static String toString(long array[]) {
        return toString(array, ",");
    }

    public static String toString(double[] array) {
        return toString(array, ",");
    }

    public static String toString(Object array[], String sep) {
        return toString(array, sep, 0, array.length);
    }

    public static String toString(Object array[], String sep, int start, int end) {
        StringBuffer sb = new StringBuffer();
        for (int i = start; i < end; i++) {
            if (i > start)
                sb.append(sep);
            sb.append(array[i]);
        }
        return sb.toString();
    }

    public static String toString(long array[], String sep) {
        return toString(array, sep, 0, array.length);
    }

    public static String toString(double array[], String sep) {
        return toString(array, sep, 0, array.length);
    }

    public static String toString(int array[], String sep) {
        return toString(array, sep, 0, array.length);
    }

    public static String toString(double array[], String sep, int start, int end) {
        StringBuffer sb = new StringBuffer();
        for (int i = start; i < end; i++) {
            if (i > 0)
                sb.append(sep);
            sb.append(array[i]);
        }
        return sb.toString();
    }

    public static String toString(long array[], String sep, int start, int end) {
        StringBuffer sb = new StringBuffer();
        for (int i = start; i < end; i++) {
            if (i > 0)
                sb.append(sep);
            sb.append(array[i]);
        }
        return sb.toString();
    }

    public static String toString(int array[], String sep, int start, int end) {
        StringBuffer sb = new StringBuffer();
        for (int i = start; i < end; i++) {
            if (i > 0)
                sb.append(sep);
            sb.append(array[i]);
        }
        return sb.toString();
    }
}

Related

  1. replace(String source, String stringToReplace, String replacementString, boolean matchCase)
  2. replace(String str)
  3. replace(String string, String pattern, String value)
  4. replace(String string, String pattern, String value)
  5. replace(String target, String replacement, String... args)
  6. replaceEmitKeys(String map, Collection newKeys)
  7. replaceFirst(String input, String search, String replacement)
  8. replaceIgnoreCase(String line, String oldString, String newString)
  9. replaceIgnoreCase(String source, String strBeReplace, String strReplaced)