Java String Replace replace(String source, Properties properties)

Here you can find the source of replace(String source, Properties properties)

Description

replace

License

Apache License

Declaration

public static String replace(String source, Properties properties) 

Method Source Code

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

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

public class Main {
    public static String replace(String source, Properties properties) {
        HashMap<String, String> map = new HashMap<String, String>(
                properties.size());// ww w .  j ava  2  s .c om
        Iterator<Object> iter = properties.keySet().iterator();
        while (iter.hasNext()) {
            String key = (String) iter.next();
            map.put(key, properties.getProperty(key));
        }
        return replace(source, map);
    }

    /**
     * Replaces ${__} with a map according to what is provided.
     * 
     * @param source
     * @param replacements
     * @return
     */
    public static String replace(String source,
            Map<String, String> replacements) {

        if (replacements == null)
            return source;
        if (replacements.size() == 0)
            return source;

        char[] ch = source.toCharArray();
        StringBuilder formatted = new StringBuilder();
        boolean skip = false;

        for (int i = 0; i < ch.length; i++) {

            if (i >= 1 && ch[i - 1] == '$' && ch[i] == '{') {
                //formatted.delete(i-1, i);
                StringBuilder rep = new StringBuilder();
                while (ch[++i] != '}') {
                    rep.append(ch[i]);
                }
                String replace = replacements.get(rep.toString());
                if (replace == null)
                    replace = "${" + rep.toString() + "}";
                formatted.append(replace);

            } else if (ch[i] == '$') {
                skip = true;
            } else {
                formatted.append(ch[i]);
            }

        }
        return formatted.toString();

    }
}

Related

  1. getLineBreakOffsets(String replacementString)
  2. replace(final String text, final String search, final String replace)
  3. replace(String origStr, char oldChar, String newStr)
  4. replace(String source, String oldChars, String newChars)
  5. replace(String source, String stringToReplace, String replacementString, boolean matchCase)
  6. replace(String str)
  7. replace(String string, String pattern, String value)