Java List Replace replaceFields(Map mapFields, String changeStr, List listString)

Here you can find the source of replaceFields(Map mapFields, String changeStr, List listString)

Description

replace Fields

License

Apache License

Declaration

public static String replaceFields(Map<String, String> mapFields, String changeStr, List<String> listString) 

Method Source Code


//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;
    }
}

Related

  1. replaceAll(final List list, final T element0, final T element1)
  2. replaceAllInList(T a, T b, List list)
  3. replaceAndAdd(List par3List, String translateToLocal)
  4. replaceAtSymbol(String s, List params)
  5. replaceColons(List unstructuredSentences)
  6. replaceIgnoreList(String text)
  7. replaceInLines(List lines, String from, String to, int fromIndex, int toIndex)
  8. replaceInList(T a, T b, List list)
  9. replaceList(String v, String[] patterns, String[] values)