Java Map Replace replacePlaceholders(String string, Map values)

Here you can find the source of replacePlaceholders(String string, Map values)

Description

Replaces placeholders of the form ${key} in the supplied string using key / value pairs from the Map provided.

License

Open Source License

Parameter

Parameter Description
string the String to evaluate.
values the values to use in the string.

Return

The evaluation result.

Declaration

@SuppressWarnings("rawtypes")
public static String replacePlaceholders(String string, Map values) 

Method Source Code

//package com.java2s;
/**/*  w  w w .ja  v  a2  s .  c  om*/
 * Copyright (C) 2005 - 2011  Eric Van Dewoestine
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.Map;

public class Main {
    private static final String PLACEHOLDER_PREFIX = "${";
    private static final String PLACEHOLDER_SUFFIX = "}";

    /**
     * Replaces placeholders of the form ${key} in the supplied string using
     * key / value pairs from the Map provided.
     *
     * @param string the String to evaluate.
     * @param values the values to use in the string.
     * @return The evaluation result.
     */
    @SuppressWarnings("rawtypes")
    public static String replacePlaceholders(String string, Map values) {
        if (string == null || values == null) {
            return string;
        }

        StringBuffer buffer = new StringBuffer(string);

        int start = buffer.indexOf(PLACEHOLDER_PREFIX);
        int end = buffer.indexOf(PLACEHOLDER_SUFFIX);
        while (start != -1 && end != -1) {
            String placeholder = buffer.substring(start + PLACEHOLDER_PREFIX.length(), end);

            Object value = values.get(placeholder);
            if (value != null) {
                buffer.replace(start, end + 1, value.toString());
            }

            start = buffer.indexOf(PLACEHOLDER_PREFIX, start + 1);
            end = buffer.indexOf(PLACEHOLDER_SUFFIX, start + 1);
        }

        return buffer.toString();
    }
}

Related

  1. replaceParameters(Map map, Map originals, Map defaults, Map hards)
  2. replaceParameters(String query, Map parameters)
  3. replaceParameters(String str, Map parameters)
  4. replaceParams(String str, Map map)
  5. replacePlaceholders(String script, Map configurations)
  6. replaceProperties(final CharSequence string, final Map properties)
  7. replaceQueryParameterInUrl(String fullUrl, Map newParameter)
  8. replaceSeparator(String value, String separator, Map map)
  9. replaceString(Map bodyToReplace, String replacingContent)