Java Map Replace replace(String temp, Map tags)

Here you can find the source of replace(String temp, Map tags)

Description

This is almost a dummy method, although it works as desired.

License

Open Source License

Parameter

Parameter Description
temp the template to use
tags a map<tag, value> that will be used for replacing all "{{tag}}" with "value"

Return

the resulting script as a string

Declaration

public static String replace(String temp, Map<String, String> tags) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.Map;
import java.util.Map.Entry;

public class Main {
    /**//from ww  w. j  a v a2s  . c o  m
     * This is almost a dummy method, although it works as desired. The
     * replacement should be in O(N), this method uses O(N*M) where M >=
     * #replacing tags
     * 
     * @param temp
     *            the template to use
     * @param tags
     *      a map<tag, value> that will be used for replacing all "{{tag}}" with "value"
     * @return the resulting script as a string
     */
    public static String replace(String temp, Map<String, String> tags) {

        //System.out.println("temp: " + temp);
        // incredibly slow but working O(tags.size()*temp.length())
        // potential speed up O(temp.length())
        for (Entry<String, String> e : tags.entrySet()) {
            temp = temp.replaceAll("\\{\\{" + e.getKey() + "\\}\\}", e.getValue());
        }

        return temp;
    }
}

Related

  1. replace(String s, Map map)
  2. replace(String src, String prefix, String suffix, Map props)
  3. replace(String str, Map values)
  4. replace(String string, Map values)
  5. replace(String target, Map arguments)
  6. replace(String template, Map funcAndDic)
  7. replace(String text, Map pairs)
  8. replace2(String str, Map data)
  9. replaceAll(Map globals, String value)