Java Map Replace replaceAll(String template, Map variables)

Here you can find the source of replaceAll(String template, Map variables)

Description

Given a raw input string template, this will do a mass search and replace for the map of variables to values.

License

Open Source License

Parameter

Parameter Description
template a parameter
variables a parameter

Declaration

public static String replaceAll(String template, Map<String, String> variables) 

Method Source Code

//package com.java2s;
/**//  w  w w.j  av  a2  s  .  c o m
 * Aptana Studio
 * Copyright (c) 2005-2012 by Appcelerator, Inc. All Rights Reserved.
 * Licensed under the terms of the GNU Public License (GPL) v3 (with exceptions).
 * Please see the license.html included with this distribution for details.
 * Any modifications to this file must keep this entire header intact.
 */

import java.util.Map;

public class Main {
    /**
     * EMPTY
     */
    public static final String EMPTY = "";

    /**
     * Given a raw input string template, this will do a mass search and replace for the map of variables to values.
     * Acts like {@link String#replaceAll(String, String)}
     * 
     * @param template
     * @param variables
     * @return
     */
    public static String replaceAll(String template, Map<String, String> variables) {
        if (template == null || variables == null || variables.isEmpty()) {
            return template;
        }
        for (Map.Entry<String, String> entry : variables.entrySet()) {
            String value = entry.getValue();
            if (value == null) {
                value = EMPTY;
            } else {
                value = value.replace('$', (char) 1); // To avoid illegal group reference issues if the text has
                // dollars!
            }
            template = template.replaceAll(entry.getKey(), value).replace((char) 1, '$');
        }
        return template;
    }

    public static boolean isEmpty(String text) {
        return text == null || text.trim().length() == 0;
    }

    /**
     * Replace one string with another
     * 
     * @param str
     * @param pattern
     * @param replace
     * @return String
     */
    public static String replace(String str, String pattern, String replace) {
        if (str == null) {
            return null;
        }

        int s = 0;
        int e = 0;
        StringBuilder result = new StringBuilder();
        while ((e = str.indexOf(pattern, s)) >= 0) {
            result.append(str.substring(s, e));
            result.append(replace);
            s = e + pattern.length();
        }
        result.append(str.substring(s));

        return result.toString();
    }

    /**
     * This is the equivalent of {@link String#indexOf(int)} but for searching for one of many characters (not a
     * substring).
     * 
     * @param string
     *            the string to search
     * @param chars
     *            The set of characters we're looking for. If we find any of these characters we stop and return the
     *            index.
     * @return
     */
    public static int indexOf(String string, char... chars) {
        return indexOf(string, 0, chars);
    }

    public static int indexOf(String string, int offset, char... chars) {
        if (chars == null || chars.length == 0) {
            return -1;
        }
        int length = string.length();
        if (length == 0) {
            return -1;
        }

        if (offset < 0) {
            offset = 0;
        }

        for (int i = offset; i < length; i++) {
            char c = string.charAt(i);
            for (char x : chars) {
                if (c == x) {
                    return i;
                }
            }
        }
        return -1;
    }
}

Related

  1. replace(String template, Map funcAndDic)
  2. replace(String text, Map pairs)
  3. replace2(String str, Map data)
  4. replaceAll(Map globals, String value)
  5. replaceAll(String inTemplate, Map inVars)
  6. replaceAllOccurrences(String str, Map replacementMap)
  7. replaceAllRegex(String input, Map replacements)
  8. replaceByMap(String _searchStr, Map _replacements)
  9. replaceByMap(String tpl, Map map)