How to replace a set of tokens in a Java String? How to replace a set of tokens in a Java String? java java

How to replace a set of tokens in a Java String?


I really don't think you need to use a templating engine or anything like that for this. You can use the String.format method, like so:

String template = "Hello %s Please find attached %s which is due on %s";String message = String.format(template, name, invoiceNumber, dueDate);


The most efficient way would be using a matcher to continually find the expressions and replace them, then append the text to a string builder:

Pattern pattern = Pattern.compile("\\[(.+?)\\]");Matcher matcher = pattern.matcher(text);HashMap<String,String> replacements = new HashMap<String,String>();//populate the replacements map ...StringBuilder builder = new StringBuilder();int i = 0;while (matcher.find()) {    String replacement = replacements.get(matcher.group(1));    builder.append(text.substring(i, matcher.start()));    if (replacement == null)        builder.append(matcher.group(0));    else        builder.append(replacement);    i = matcher.end();}builder.append(text.substring(i, text.length()));return builder.toString();


Unfortunately the comfortable method String.format mentioned above is only available starting with Java 1.5 (which should be pretty standard nowadays, but you never know). Instead of that you might also use Java's class MessageFormat for replacing the placeholders.

It supports placeholders in the form '{number}', so your message would look like "Hello {0} Please find attached {1} which is due on {2}". These Strings can easily be externalized using ResourceBundles (e. g. for localization with multiple locales). The replacing would be done using the static'format' method of class MessageFormat:

String msg = "Hello {0} Please find attached {1} which is due on {2}";String[] values = {  "John Doe", "invoice #123", "2009-06-30"};System.out.println(MessageFormat.format(msg, values));