Here you can find the source of replaceMap(String inStr, Map replaceMap)
Parameter | Description |
---|---|
inStr | Input character string |
replaceMap | Substitution map |
public static String replaceMap(String inStr, Map replaceMap)
//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; } }