Here you can find the source of replaceFields(Map
public static String replaceFields(Map<String, String> mapFields, String changeStr, List<String> listString)
//package com.java2s; //License from project: Apache License import java.util.Arrays; import java.util.Comparator; import java.util.List; import java.util.Map; public class Main { public static String replaceFields(Map<String, String> mapFields, String changeStr, List<String> listString) { Object[] keys = mapFields.keySet().toArray(); Comparator comparator = new Comparator() { public int compare(Object o1, Object o2) { int length1 = ((String) o1).length(); int length2 = ((String) o2).length(); if (length1 > length2) return -1; if (length1 == length2) { return 0; }/* w ww.ja v a 2 s .c om*/ return 1; } }; Arrays.sort(keys, comparator); while (true) { int minIndex = 10000; String field = null; for (Object obj : keys) { String name = (String) obj; int index = changeStr.toLowerCase().indexOf(name); if ((index != -1) && (index < minIndex)) { minIndex = index; field = name; } } if (field == null) break; changeStr = changeStr.replaceFirst(field, "F"); listString.add(field); } return changeStr; } }