Java Map Replace replaceByMap(String _searchStr, Map _replacements)

Here you can find the source of replaceByMap(String _searchStr, Map _replacements)

Description

Replace all placeholders in given string by value of the corresponding key in given Map.

License

Open Source License

Parameter

Parameter Description
_searchStr search string
_replacements replacement

Return

String or null if _searchStr was null

Declaration

public static String replaceByMap(String _searchStr, Map<String, String> _replacements) 

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 w  w w .  j av  a  2s .  c  o m*/
     * Replace all placeholders in given string by value of the corresponding key in given Map.
     *
     * @param _searchStr search string
     * @param _replacements replacement
     * @return String or null if _searchStr was null
     */
    public static String replaceByMap(String _searchStr, Map<String, String> _replacements) {
        if (_searchStr == null) {
            return null;
        }
        if (_replacements == null || _replacements.isEmpty()) {
            return _searchStr;
        }

        String str = _searchStr;

        for (Entry<String, String> entry : _replacements.entrySet()) {
            str = str.replace(entry.getKey(), entry.getValue());
        }

        return str;
    }

    /**
     * Checks if the given String is either null or empty.
     * Blank means:<br>
     * <pre>
     * " " - false
     * "" - true
     * null - true
     * " xx" - false
     * </pre>
     * @param _str string to test
     * @return true if string is empty or null, false otherwise
     */
    public static boolean isEmpty(String _str) {
        if (_str == null) {
            return true;
        }

        return _str.isEmpty();
    }
}

Related

  1. replaceAll(Map globals, String value)
  2. replaceAll(String inTemplate, Map inVars)
  3. replaceAll(String template, Map variables)
  4. replaceAllOccurrences(String str, Map replacementMap)
  5. replaceAllRegex(String input, Map replacements)
  6. replaceByMap(String tpl, Map map)
  7. replaceCharacters(String sequence, Map map, boolean reverse)
  8. replaceCharactersInArray(char[] characters, Map replacements)
  9. replaceElements(Map destinationMap, Map sourceMap)