Java Array Replace replaceAll(String src, String[] replace, String[] by)

Here you can find the source of replaceAll(String src, String[] replace, String[] by)

Description

Replaces all Strings in replace by the corresponding String in by, that is the ith String in replace is replaced by the ith String in by, in order.

License

Open Source License

Parameter

Parameter Description
src the string to replace values in
replace the substrings to find
by what to replace the substrings with

Return

the replaced string

Declaration

public static String replaceAll(String src, String[] replace, String[] by) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Arrays;

public class Main {
    /**//  w  w w  .  j a  v a  2  s . c  om
     * Replaces all Strings in replace by the corresponding String in by, that
     * is the ith String in replace is replaced by the ith String in by, in
     * order.
     *
     * @param src     the string to replace values in
     * @param replace the substrings to find
     * @param by      what to replace the substrings with
     * @return the replaced string
     */
    public static String replaceAll(String src, String[] replace, String[] by) {
        for (int i = 0; i < replace.length; i++) {
            src = src.replace(replace[i], by[i]);
        }
        return src;
    }

    /**
     * Replaces all Strings in replace by by.
     *
     * @param src     the string to replace values in
     * @param replace the substrings to find
     * @param by      what to replace the substrings with
     * @return the replaced string
     */
    public static String replaceAll(String src, String[] replace, String by) {
        String[] rb = new String[replace.length];
        Arrays.fill(rb, by);
        return replaceAll(src, replace, rb);
    }
}

Related

  1. replace(char[] array, char[] toBeReplaced, char[] replacementChars)
  2. replace(char[][] arrays, char character, char[][] replacements)
  3. replace(char[][] arrays, char character, char[][] replacements)
  4. replaceAll(final Object[] objs, final String str)
  5. replaceAll(final String[] args, final String from, final String to)
  6. replaceChars(String s, char[] from, char[] to)
  7. replaceIgnoreCase(final String s, final String[] sub, final String[] with)
  8. replaceInArray(String[] thisArray, String findThis, String replaceWithThis)
  9. replaceWithValues(String subject, String needle, String[] values)