简体   繁体   English

String.format 或类似格式的正确格式字符串

[英]Correct format string for String.format or similar

I'm sure I've seen String.format used like this before:我确定我以前见过像这样使用String.format

String.format("Some {1}, {2}, {3}", var1, var2, var3);

Does this ring any bells for anyone?这是否为任何人敲响了警钟? Maybe I'm thinking of C#, is there a way of achieving the same in java?也许我在考虑 C#,有没有办法在 Java 中实现相同的目标?

I know you can do something like:我知道你可以这样做:

String.format("Some %s, %d, %s", aString, aNumber, aString)

but the syntax I'm after is for the former...但我所追求的语法是前者......

What you are looking for is MessageFormat , which uses a given format and input parameters, eg您正在寻找的是MessageFormat ,它使用给定的格式和输入参数,例如

MessageFormat.format("Some {0}, {1}, {2}", var1, var2, var3);

And as already mentioned, String.format can still do the job using the alternate syntax, but it is less powerful in functionality and not what you requested.正如已经提到的, String.format仍然可以使用替代语法来完成这项工作,但它的功能不那么强大,而不是你所要求的。

I do not like to specify both index of parameter or its type - mainly when throwing exception and preparing message for it.我不喜欢同时指定参数的索引或其类型 - 主要是在抛出异常并为其准备消息时。 I like way SLF4j does it.我喜欢 SLF4j 的做法。 So I wrapped org.slf4j.helpers.MessageFormatter like this:所以我像这样包装了 org.slf4j.helpers.MessageFormatter:

public static String subst(String string, Object...objects) {
    return MessageFormatter.arrayFormat(string, objects).getMessage();
}

Then you can use it like this:然后你可以像这样使用它:

public void main(String[] args) {
    throw new RuntimeException(MyUtils.subst("Problem with A={} and B={}!", a, b));
}

Yes, that's the typical format string of C# .是的,这是C#的典型格式字符串。 In Java, you can use the latter, that is, String.format("%s %d %d", ...) .在 Java 中,您可以使用后者,即String.format("%s %d %d", ...)

An alternative is to use MessageFormat.format("Some {0}, {1}, {2}", var1, var2, var3) , which uses the .NET curly braces notation, as mentioned by @Tobias, though it requires you to import java.text.MessageFormat .另一种方法是使用MessageFormat.format("Some {0}, {1}, {2}", var1, var2, var3) ,它使用 .NET 花括号表示法,如@Tobias 所述,尽管它需要您导入java.text.MessageFormat They are also more appropriate for when you are dealing with localized resources, where you typically have external .properties files with messages in the format Error {0} ocurred due to {1} .当您处理本地化资源时,它们也更适合,在这种情况下,您通常有外部.properties文件,其中包含格式为Error {0} ocurred due to {1}

If you want to use empty placeholders (without positions), you could write a small utility around Message.format() , like this如果您想使用空占位符(没有位置),您可以围绕Message.format()编写一个小实用程序,如下所示

    String format(String s, Object... var2) {
        int i = 0;
        while(s.contains("{}")) {
            s = s.replaceFirst(Pattern.quote("{}"), "{"+ i++ +"}");
        }
        return MessageFormat.format(s, var2);
    }

And then, can use it like,然后,可以像这样使用它,

format("Some {} {} {}", var1, var2, var3);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM