java - 使用给定的可变参数替换模板中的 {0}、{1} .. {n}

标签 java apache-flex string

考虑以下格式的字符串模板:

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

替换变量字段的形式为 {n},其中 n 是从零开始的索引。

这是 Adob​​e 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/

相关文章:

javascript - 用于替换中间字母的正则表达式

.net - String.Split 的异常行为

java - 图像到视频 Xuggler

java - 是否可以打开包含类的 .txt/.java 文件,并对其使用反射?

apache-flex - 为什么 Alert.show() 给我两个警报框?

javascript - 柔性 : Children components disapear when SkinnableContainer is skinned

php - 如何避免/解决 PHP 自动换行删除空格的问题

java - 是否建议将 ExecutionCallBacks 用于分布式执行程序服务 - hazelcast

java - Spring Boot 以编程方式重启应用程序

javascript - ExternalInterface.call() 没有得到返回值