Java Map Replace replaceVariable(final String src, final Map value)

Here you can find the source of replaceVariable(final String src, final Map value)

Description

replace Variable

License

Open Source License

Declaration

public static String replaceVariable(final String src, final Map<String, String> value) 

Method Source Code

//package com.java2s;

import java.util.Map;

public class Main {

    public static String replaceVariable(final String src, final Map<String, String> value) {
        int len = src.length();
        StringBuffer buf = new StringBuffer(len);
        for (int i = 0; i < len; i++) {
            char c = src.charAt(i);
            if (c == '$') {
                i++;/*from  w w w  .  j a  v a  2 s .co m*/
                StringBuffer key = new StringBuffer();
                char temp = src.charAt(i);
                while (temp != '}') {
                    if (temp != '{') {
                        key.append(temp);
                    }
                    i++;
                    temp = src.charAt(i);
                }
                String variable = value.get(key.toString());
                if (null == variable) {
                    buf.append("");
                } else {
                    buf.append(variable);
                }
            } else {
                buf.append(c);
            }
        }
        return buf.toString();
    }
}

Related

  1. replaceTemplates(String template, Map entries)
  2. replaceTokens(String inputString, Map tokenMap)
  3. replaceToStringBuilder(String s, String begin, String end, Map values)
  4. replaceValues(String s, String begin, String end, Map values)
  5. replaceValuesToSynonyms(Map dic, Map synonims)
  6. replaceVariables(String chart, Map variables)
  7. replaceVars(String origString, Map vars)
  8. replaceVars(String s, Map m)
  9. stringReplace(Map map, String line)