Java List Replace replace(String sourceStr, List indexList, String replaceStr)

Here you can find the source of replace(String sourceStr, List indexList, String replaceStr)

Description

replace

License

Open Source License

Parameter

Parameter Description
sourceStr sourceStr
indexList indexList
replaceStr replaceString

Return

new String

Declaration

public static String replace(String sourceStr, List<int[]> indexList, String replaceStr) 

Method Source Code

//package com.java2s;

import java.util.List;

public class Main {
    /**/*from   w w  w  .  j av a2 s . co  m*/
     * 
     * @param sourceStr
     *            sourceStr
     * @param indexList
     *            indexList
     * @param replaceStr
     *            replaceString
     * @return new String
     * @author j00105185
     */
    public static String replace(String sourceStr, List<int[]> indexList, String replaceStr) {
        if (indexList.isEmpty()) {
            return sourceStr;
        }
        StringBuffer bf = new StringBuffer();
        int t = 0;
        int i = 0;
        for (int[] ii : indexList) {
            bf.append(sourceStr.substring(t, ii[0]).trim()).append(replaceStr);
            if (i == indexList.size() - 1) {
                bf.append(sourceStr.substring(ii[1] + 1).trim());
            }
            i++;
            t = ii[1] + 1;
        }
        return bf.toString().trim();
    }
}

Related

  1. replace(List list, Object o, Object n)
  2. replace(List list, T element, List replacements)
  3. replace(List list, char target, char replacement)
  4. replace(List list, T from, T to)
  5. replace(String s, List replacementVars)
  6. replaceAll(final List list, final T element0, final T element1)
  7. replaceAllInList(T a, T b, List list)
  8. replaceAndAdd(List par3List, String translateToLocal)
  9. replaceAtSymbol(String s, List params)