Java Map Replace replaceByMap(String tpl, Map map)

Here you can find the source of replaceByMap(String tpl, Map map)

Description

replace like xxxx{key}yyy{key}zzzz

License

Apache License

Parameter

Parameter Description
tpl string like xxxx{key}yyy{key}zzzz
map a parameter

Declaration

public static String replaceByMap(String tpl, Map<String, Object> map) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Map;

public class Main {

    public static String replaceByMap(String tpl, Map<String, Object> map) {

        if (tpl != null && map != null && map.size() > 0) {

            for (String key : map.keySet()) {
                tpl = tpl.replace("{" + key + "}", String.valueOf(map.get(key)));
            }/*www.j  a  va  2  s  .  c o  m*/
        }

        return tpl;
    }

    public static String replace(String tpl, Object... vals) {

        if (tpl != null) {

            for (int i = 1; i <= vals.length; i++) {
                tpl = tpl.replace("{?" + i + "}", String.valueOf(vals[i - 1]));
            }
        }

        return tpl;
    }
}

Related

  1. replaceAll(String inTemplate, Map inVars)
  2. replaceAll(String template, Map variables)
  3. replaceAllOccurrences(String str, Map replacementMap)
  4. replaceAllRegex(String input, Map replacements)
  5. replaceByMap(String _searchStr, Map _replacements)
  6. replaceCharacters(String sequence, Map map, boolean reverse)
  7. replaceCharactersInArray(char[] characters, Map replacements)
  8. replaceElements(Map destinationMap, Map sourceMap)
  9. replaceEntities(final Map replacements, final String xml)