Java List Replace replace(final String source, final List search, final List repl)

Here you can find the source of replace(final String source, final List search, final List repl)

Description

replace

License

Apache License

Declaration

public static String replace(final String source,
        final List<String> search, final List<String> repl) 

Method Source Code

//package com.java2s;
/*// w  w w.  j ava2  s.  co  m
 * ? Copyright Foconis AG, 2013
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at:
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
 * implied. See the License for the specific language governing 
 * permissions and limitations under the License.
 */

import java.util.List;

public class Main {

    public static String replace(final String source,
            final List<String> search, final List<String> repl) {
        String result = source;
        for (int i = 0; i < search.size(); ++i) {
            if (i < repl.size()) {
                result = result.replace(search.get(i), repl.get(i));
            } else {
                result = result.replace(search.get(i),
                        repl.get(repl.size() - 1));
            }
        }
        return result;
    }

    public static String replace(final String source,
            final List<String> search, final String repl) {
        String result = source;
        for (String srch : search) {
            result = result.replace(srch, repl);
        }
        return result;
    }

    public static void replace(final List<String> source,
            final List<String> search, final List<String> repl) {
        for (int i = 0; i < source.size(); ++i) {
            source.set(i, replace(source.get(i), search, repl));
        }
    }
}

Related

  1. replace(final List list, final T newElement, final int index)
  2. replace(List list, Object o, Object n)
  3. replace(List list, T element, List replacements)
  4. replace(List list, char target, char replacement)
  5. replace(List list, T from, T to)