考虑以下格式的字符串模板:
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'");
最佳答案
关于java - 使用给定的可变参数替换模板中的 {0}、{1} .. {n},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3478440/