Java MessageFormat-替换索引处的值

Java MessageFormat-替换索引处的值,java,messageformat,Java,Messageformat,我有一根这样的线: {0}/suhdp run -command "suhilb" -input /sufiles/{1} -output /seismicdata/mr_files/{2}/ -cwproot {3} 需要首先替换第0个和第3个索引处的值。稍后,将替换第1和第2个索引(在已部分格式化的字符串上)并最终使用 我和同学们玩了一会儿,但没能和同学们一起玩,以达到我的目的 欢迎任何指点 如果您确定特定字符串{something}没有在您的字符串中使用(似乎是这样),为什么不保持字符串

我有一根这样的线:

{0}/suhdp run -command "suhilb" -input /sufiles/{1} -output /seismicdata/mr_files/{2}/ -cwproot {3}
需要首先替换第0个和第3个索引处的值。稍后,将替换第1和第2个索引(在已部分格式化的字符串上)并最终使用

我和同学们玩了一会儿,但没能和同学们一起玩,以达到我的目的


欢迎任何指点

如果您确定特定字符串
{something}
没有在您的字符串中使用(似乎是这样),为什么不保持字符串的原样,并在以后使用它将其更改为任何值?

因为您不会一次填充所有值,我建议您使用生成器:

public class MessageBuilder
{
    private final String fmt;
    private final Object[] args;

    public MessageBuilder(final String fmt, final int nrArgs)
    {
        this.fmt = fmt;
        args = new Object[nrArgs];
    }

    public MessageBuilder addArgument(final Object arg, final int index)
    {
        if (index < 0 || index >= args.length)
            throw new IllegalArgumentException("illegal index " + index);
        args[index] = arg;
        return this;
    }

    public String build()
    {
        return MessageFormat.format(fmt, args);
    }
}
这段代码可能缺少一些错误检查等,而且远不是最佳的,但是你明白了。

这有帮助吗

应在第二阶段替换的占位符最初引用

public static void main(String[] args) {
    final String partialResult = MessageFormat.format("{0} '{0}' '{1}' {1}", "zero", "three");
    System.out.println(partialResult);
    final String finalResult = MessageFormat.format(partialResult, "one", "two");
    System.out.println(finalResult);
}
然后,格式字符串变为:

{0}/suhdp run -command "suhilb" -input /sufiles/'{0}' -output /seismicdata/mr_files/'{1}'/ -cwproot {1}

这将处理名称中带有空格的文件吗?我稍微修改了你的问题,以使其更容易理解(这将为你提供更多答案)。请确保所有的事情都在你的意愿中,因为你对“代码> MeasAd格式化< /代码>的参数被延迟了,你可以考虑使用一个BuffeldRead我的答案,它可以给你一些关于如何做的提示。it@artbristol是的,文件可能有空格谢谢!它达到了我的目的!!!为了使代码通用,如何才能找到字符串中要替换的索引数(在我的例子中为4),以便MessageBuilder可以被具有不同索引数的字符串使用?老实说,我不知道,我并不是真的使用
MessageFormat
。你最好的办法可能是尝试和实验如果你输入了错误数量的参数会发生什么。。。至于查找占位符,我想一个简单的正则表达式可以帮助您做到这一点。
{0}/suhdp run -command "suhilb" -input /sufiles/'{0}' -output /seismicdata/mr_files/'{1}'/ -cwproot {1}