Java Map Replace replaceMap(String inStr, Map replaceMap)

Here you can find the source of replaceMap(String inStr, Map replaceMap)

Description

We substitute string character according to a substitution map.

License

Open Source License

Parameter

Parameter Description
inStr Input character string
replaceMap Substitution map

Declaration

public static String replaceMap(String inStr, Map replaceMap) 

Method Source Code

//package com.java2s;
/* infoScoop OpenSource/* w w w.ja v  a  2s  . co  m*/
 * Copyright (C) 2010 Beacon IT Inc.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * as published by the Free Software Foundation.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/lgpl-3.0-standalone.html>.
 */

import java.util.Iterator;
import java.util.Map;

public class Main {
    /**
     * We substitute string character according to a substitution map.
     * @param inStr Input character string
     * @param replaceMap Substitution map
     * @return
     */
    public static String replaceMap(String inStr, Map replaceMap) {
        String str = inStr;
        Iterator itr = replaceMap.keySet().iterator();
        while (itr.hasNext()) {
            String key = (String) itr.next();
            String value = (String) replaceMap.get(key);
            if (key != null && value != null) {
                str = replaceStr(str, key, value);
            }
        }
        return str;
    }

    /**
     * We substitute character string.
     * @param inStr Input character string
     * @param fromStr Character string that is target to substitute.
     * @param toStr  Character string after substituted
     */
    public static String replaceStr(String inStr, String fromStr, String toStr) {
        String str = inStr;
        int fromLen = fromStr.length();
        int toLen = toStr.length();
        int idx = 0;
        while (true) {
            idx = str.indexOf(fromStr, idx);
            if (idx < 0) {
                break;
            }
            str = str.substring(0, idx) + toStr + str.substring(idx + fromLen);
            idx += toLen;
        }
        return str;
    }
}

Related

  1. replaceInvalid(String uri, Map reps)
  2. replaceKey(final Map map, final TKey oldkey, final TKey newkey)
  3. replaceKey(Map map, K oldKey, K newKey)
  4. replaceKeyCharacter(Map map, char oldChar, char newChar)
  5. replaceKeyword(String messageTemplate, Map args, String key)
  6. replaceMethodUriWithValues(String methodUri, Map urlParameterKeyValues)
  7. replaceNode(Map map, Map replacement, List pathItems)
  8. replaceNotContainedWithReplaced(String sentStr, Map mapNotContainedReplaced)
  9. replaceOpcodeNames(HashMap replacementValues, final String input)