2010-08-13 23 views
2

考虑以下格式的字符串模板:替代{0},{1} .. {N}与给定的模板可变参数

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

换人变量字段的形式为{N},其中的n是基于零的索引。

这是Adobe Flex中使用的模板格式,请参见StringUtil.substitute(...)。还有.NET,IIRC。

由于我想重新使用Flex代码使用的模板,我正在寻找一个Java的等价物。我知道String.format(...),但模板结构不相同。

在Java中获得相同的“Flex兼容”模板功能的最佳方式是什么?

基本上,这是理想的最终结果是:

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

回答