【问题标题】:Substitute {0}, {1} .. {n} in a template with given varargs用给定的可变参数替换模板中的 {0}、{1} .. {n}
【发布时间】:2010-08-13 15:38:52
【问题描述】:

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

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 apache-flex string


    【解决方案1】:

    【讨论】:

      【解决方案2】:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-09-13
        • 2013-09-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多