Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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 <a href="http://velocity.apache.org/" rel="nofollow noreferrer">Velocity</a>. It uses a different syntax for values however. See <a href="http://velocity.apache.org/engine/devel/webapps.html" rel="nofollow noreferrer">Getting Started</a>.</p> <p>If you do want to write this yourself try:</p> <pre><code>public static String replaceAll(String text, Map&lt;String, String&gt; params) { return replaceAll(text, params, '%', '%'); } public static String replaceAll(String text, Map&lt;String, String&gt; 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; } </code></pre> <p>For example:</p> <pre><code>String in = "Hi %FIRST_NAME% %LAST_NAME%."; Map&lt;String, String&gt; params = new HashMap&lt;String, String&gt;(); params.put("FIRST_NAME", "John"); params.put("LAST_NAME", "Smith"); String out = replaceAll(in, params); System.out.println(out); </code></pre> <p>Output:</p> <pre><code>Hi John Smith. </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload