Here you can find the source of replace(String sourceStr, List
Parameter | Description |
---|---|
sourceStr | sourceStr |
indexList | indexList |
replaceStr | replaceString |
public static String replace(String sourceStr, List<int[]> indexList, String replaceStr)
//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(); } }