-3

Is there is any API which replace template string along with values using Spring or java.

For example:

Dear %FIRST_NAME% %LAST_NAME%,
---- remaining text-----------

where parameters (FIRST_NAME, LAST_NAME) in the form of Map.

1
  • +1 for the question -2 for the selected answer Commented Mar 3, 2010 at 21:59

4 Answers 4

3

It's relatively simple to write code that will do this. If this is something you're going to be doing a lot however you might want to look into using an existing library like Velocity. It uses a different syntax for values however. See Getting Started.

If you do want to write this yourself try:

public static String replaceAll(String text, Map<String, String> params) {
  return replaceAll(text, params, '%', '%');
}

public static String replaceAll(String text, Map<String, String> params,
    char leading, char trailing) {
  String pattern = "";
  if (leading != 0) {
    pattern += leading;
  }
  pattern += "(\\w+)";
  if (trailing != 0) {
    pattern += trailing;
  }
  Pattern p = Pattern.compile(pattern);
  Matcher m = p.matcher(text);
  boolean result = m.find();
  if (result) {
    StringBuffer sb = new StringBuffer();
    do {
      String replacement = params.get(m.group(1));
      if (replacement == null) {
        replacement = m.group();
      }
      m.appendReplacement(sb, replacement);
      result = m.find();
    } while (result);
    m.appendTail(sb);
    return sb.toString();
  }
  return text;
}

For example:

String in = "Hi %FIRST_NAME% %LAST_NAME%.";
Map<String, String> params = new HashMap<String, String>();
params.put("FIRST_NAME", "John");
params.put("LAST_NAME", "Smith");
String out = replaceAll(in, params);
System.out.println(out);

Output:

Hi John Smith.
Sign up to request clarification or add additional context in comments.

Comments

0

My favorite templating engine is Apache Velocity

Integrates nicely with Spring as well, theres an introductory article here

Comments

0

Check StringTemplate, http://www.stringtemplate.org/ and article here to get feel of it, http://www.codecapers.com/post/Generating-Structured-Text-with-StringTemplate.aspx

Comments

0

Usage:

replaceAll(string,
        "param1", "value 1",
        "param12", "value 12");

Implementation:

public static String replaceAll(String text, Object... replacements) {
    Assert.isTrue(replacements.length % 2 == 0, "Is not key - value pairs: " + replacements);

    Map< String, String> longToShortNamesMap = new TreeMap<>(new Comparator<String>() {
        @Override
        public int compare(String o1, String o2) {
            return o1.length() == o2.length() ? o1.compareTo(o2) : o2.length() - o1.length();
        }
    });
    for (int i = 0; i < replacements.length; i += 2) {
        longToShortNamesMap.put(Objects.toString(replacements[i]), Objects.toString(replacements[i+1]));
    }
    String result = text;
    for (String key : longToShortNamesMap.keySet()) {
        Assert.isTrue(text.contains(key), "Can not find key in the text " + key);
        result = result.replaceAll(key, longToShortNamesMap.get(key));
    }
    return result;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.