views:

46

answers:

2

Consider a string template of the following format:

String template = "The credentials you provided were username '{0}' with password '{1}'";

Substitution variable fields are of the form {n}, where n is a zero based index.

This is the template format used in Adobe Flex, see StringUtil.substitute(...). And also .NET, IIRC.

Since I want to re-use the templates used by the Flex code I'm looking for an Java equivalent. I'm aware of String.format(...) but the template structure is not identical.

What is the best way to get the same "Flex compatible" template functionality in Java?

Basically this is the desired end-result:

assert(StringUtil.substitute(template, "powerUser", "difficultPassword") == "The credentials you provided were username 'powerUser' with password 'difficultPassword'");
+3  A: 

Use MessageFormat

hhbarriuso
+1  A: 

You want java.text.MessageFormat http://download-llnw.oracle.com/javase/6/docs/api/java/text/MessageFormat.html

MatthieuF