简体   繁体   English

Java格式数组(使用Formatter类)

[英]Java format array (using the Formatter class)

We have method that looks like this : 我们有这样的方法:

public String getCommandString(Object...args) {
    return this.code + " " + String.format(this.pattern, args);
}

Where this.code:int and this.pattern:String . 其中this.code:intthis.pattern:String

This method is usually called like this : 这种方法通常称为:

// cmd.code = 20
// cmd.pattern = "%1$s = %2$d"
String str = cmd.getCommandString("foo", 3);   // -> "20 foo = 3"

and other string patterns (this is for a very simple text-based server-client program) 和其他字符串模式(这是一个非常简单的基于文本的服务器 - 客户端程序)

Now, is it possible for pattern to take into account a variable number of arguments, such as 现在,模式是否可以考虑可变数量的参数,例如

// cmd.code = 20
// cmd.pattern = ???
String s1 = cmd.getCommandString("a", "b", "c");               // -> 20 a b c
String s2 = cmd.getCommandString("Hello", "world");            // -> 20 Hello world
String s3 = cmd.getCommandString("1", "2, "3", "4", "5", "6"); // -> 20 1 2 3 4 5 6

Assuming perhaps that each argument is of the same type (strings)? 假设每个参数可能是相同的类型(字符串)? Or do I have to override the method and format the string manually? 或者我是否必须覆盖方法并手动格式化字符串? More specifically, I'm looking for a generic string pattern to format a variable number of arguments (of the same type). 更具体地说,我正在寻找一个通用的字符串模式来格式化可变数量的参数(相同类型)。 I remember making such a thing in C, but is this possible in Java? 我记得在C中做过这样的事情,但这在Java中是否可行?

The functionality you are asking for, if I understand it correctly, does not exist. 如果我理解正确,您要求的功能不存在。 Here is the javadoc section from the Formatter class: 这是Formatter类的javadoc部分:

Format specifiers can reference arguments in three ways: 格式说明符可以通过三种方式引用参数:

Explicit indexing is used when the format specifier contains an argument index. 当格式说明符包含参数索引时,使用显式索引。 The argument index is a decimal integer indicating the position of the argument in the argument list. 参数index是一个十进制整数,表示参数列表中参数的位置。 The first argument is referenced by "1$", the second by "2$", etc. An argument may be referenced more than once. 第一个参数由“1 $”引用,第二个参数由“2 $”引用,等等。参数可以多次引用。 For example: 例如:

formatter.format("%4$s %3$s %2$s %1$s %4$s %3$s %2$s %1$s", "a", "b", "c", "d") // -> "dcbadcba" formatter.format(“%4 $ s%3 $ s%2 $ s%1 $ s%4 $ s%3 $ s%2 $ s%1 $ s”,“a”,“b”,“c” ,“d”)// - >“dcbadcba”

Relative indexing is used when the format specifier contains a '<' ('\<') flag which causes the argument for the previous format specifier to be re-used. 当格式说明符包含'<'('\\ u003c')标志时,使用相对索引,该标志会导致重复使用前一格式说明符的参数。 If there is no previous argument, then a MissingFormatArgumentException is thrown. 如果没有先前的参数,则抛出MissingFormatArgumentException。

formatter.format("%s %s % "abbb" // "c" and "d" are ignored because they are not referenced formatter.format(“%s%s%”abbb“//”c“和”d“被忽略,因为它们未被引用

Ordinary indexing is used when the format specifier contains neither an argument index nor a '<' flag. 当格式说明符既不包含参数索引也不包含'<'标志时,使用普通索引。 Each format specifier which uses ordinary indexing is assigned a sequential implicit index into argument list which is independent of the indices used by explicit or relative indexing. 使用普通索引的每个格式说明符都被赋予一个顺序隐式索引到参数列表中,该索引独立于显式或相对索引使用的索引。

formatter.format("%s %s %s %s", "a", "b", "c", "d") // -> "abcd" formatter.format(“%s%s%s%s”,“a”,“b”,“c”,“d”)// - >“abcd”

It is possible to have a format string which uses all forms of indexing, for example: 可以使用格式字符串,该字符串使用所有形式的索引,例如:

formatter.format("%2$s %s % "baab" // "c" and "d" are ignored because they are not referenced formatter.format(“%2 $ s%s%”baab“//”c“和”d“被忽略,因为它们未被引用

The maximum number of arguments is limited by the maximum dimension of a Java array as defined by the Java Virtual Machine Specification. 参数的最大数量受Java虚拟机规范定义的Java数组的最大维数限制。 If the argument index is does not correspond to an available argument, then a MissingFormatArgumentException is thrown. 如果参数索引与可用参数不对应,则抛出MissingFormatArgumentException。

If there are more arguments than format specifiers, the extra arguments are ignored. 如果参数多于格式说明符,则忽略额外参数。

What you are asking for is a way to construct a pattern that will accept an arbitrary number of arguments and use tham all. 您要求的是一种构建模式的方法,该模式将接受任意数量的参数并使用全部。 The problem is that the Formatter class can only associate format specifiers with arguments either explicitly, relatively, or ordinarily. 问题是Formatter类只能显式地,相对地或通常地将格式说明符与参数相关联。 In each of these cases, the number of forma specifiers is fixed . 在每种情况下, 形式说明符的数量是固定的 There is no construct in which you can repeat or loop a format specifier. 没有可以重复或循环格式说明符的构造。 The relative technique looks promising, but there is no way to nest, or loop, it. 相关技术看起来很有希望,但没有办法嵌套或循环它。

Use this 用这个

public String getCommandString(Object...args) {
    // Want to have a "-" if args are not given
    return this.code + " " + args != null && args.length > 0
            ? Arrays.stream(args).map(o -> o.toString()).collect(Collectors.joining(" + ")) : "-";
}

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

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