Java String Substitute substitute(String str, Hashtable vars, String delimiter)

Here you can find the source of substitute(String str, Hashtable vars, String delimiter)

Description

Searches the line for any variables which should be substituted with values from the hashtable.

License

Open Source License

Parameter

Parameter Description
str String containing possible variable delimiters
vars Hashtable containing variable/value pairs to substitute
delimiter Delimits variables within the string

Declaration

public static String substitute(String str, Hashtable vars, String delimiter) 

Method Source Code


//package com.java2s;
import java.util.*;

public class Main {
    /**/*from  w  ww  .ja v  a2s. co  m*/
     * Searches the line for any variables which should be substituted with 
     * values from the hashtable.  The delimiter string is used to identify
     * variable placeholders within the string.  A delimiter will appear on
     * both sides of a key to indicate that the text between (and including) 
     * the delimiters should be substituted.  To prevent unexpected behavior,
     * hashtable keys which contain the delimiter string will not be
     * substituted.
     *
     * @param   str         String containing possible variable delimiters
     * @param   vars        Hashtable containing variable/value pairs to substitute
     * @param   delimiter   Delimits variables within the string
     */
    public static String substitute(String str, Hashtable vars, String delimiter) {
        String key = null;
        for (Enumeration keys = vars.keys(); keys.hasMoreElements();) {
            key = (String) keys.nextElement();
            // Replace the variable in the string
            if ((key != null) && (key.indexOf(delimiter) == -1)) {
                str = replaceVar(str, delimiter + key + delimiter, (String) vars.get(key));
            }
        }

        return str;
    }

    /**
     * Replaces all occurences of a variable within the string.
     *
     * @param   str     String containing the variable to be replaced
     * @param   var     Variable string to be replaced
     * @param   newVal  Value to be substituted for the variable
     *
     * @return  String  final string with all occurances of the variable removed
     */
    public static String replaceVar(String str, String var, String newVal) {
        // Verify that the input parameters are OK
        if ((str != null) && (var != null) && (var.length() > 0)) {
            // Locate the first occurrence of the variable within the string
            int varIndex = str.indexOf(var);

            // Replace all instances of the variable
            while ((str != null) && (varIndex >= 0)) {
                str = str.substring(0, varIndex) + newVal + str.substring(varIndex + var.length());

                // Locate the next occurrence of the variable in the string
                varIndex = str.indexOf(var);
            }
        }

        return str;
    }
}

Related

  1. stringSubstitution(String argStr, Hashtable vars)
  2. substitute(String original, String match, String subst)
  3. substituteProperty(String value, Properties coreProperties)