[英]What is this %<s called?
I have recently come across this following string: "%<s".我最近遇到了以下字符串:“%<s”。 I noticed that this has the following effect:我注意到这具有以下效果:
String sampleString = "%s, %<s %<s";
String output = String.format(sampleString, "A");
System.out.println(output); // A, A A.
I tried to google what this %<s is called, but to no avail.我试图用谷歌搜索这个 %<s 叫什么,但无济于事。 I know from the above code that I just need to input the format string once, and it will replace all instances of %s & %<s.我从上面的代码中知道,我只需要输入一次格式字符串,它就会替换所有 %s & %<s 的实例。
Just curious what this name is called!很好奇这个名字叫什么!
They are called format specifiers.它们被称为格式说明符。 Here you are using relative indexing.这里你使用的是相对索引。
From the Java documentation :来自Java 文档:
Relative indexing is used when the format specifier contains a '<' ('<') flag which causes the argument for the previous format specifier to be re-used.当格式说明符包含“<”(“<”)标志时,使用相对索引,这会导致重新使用前一个格式说明符的参数。 If there is no previous argument, then a MissingFormatArgumentException is thrown.如果没有先前的参数,则抛出 MissingFormatArgumentException。
formatter.format("%s %s %<s %<s", "a", "b", "c", "d")
// -> "a b b b"
// "c" and "d" are ignored because they are not referenced
%<s
is still a "format specifier", just like %s
, except that %<s
has <
in its "argument index" position. Note that a format specifier in general has a syntax like this: %<s
仍然是一个“格式说明符”,就像%s
一样,除了%<s
在其“参数索引”position 中有<
之外。请注意,格式说明符通常具有如下语法:
%[argument_index$][flags][width][.precision]conversion
and the it is allowed to use <
as the argument_index
part ( documentation ):并且允许使用<
作为argument_index
部分( 文档):
Argument Index参数索引
[...] [...]
Another way to reference arguments by position is to use the '<' ('<') flag, which causes the argument for the previous format specifier to be re-used. position 引用 arguments 的另一种方法是使用 '<' ('<') 标志,这会导致重新使用先前格式说明符的参数。
The documentation doesn't call the <
part a special name either, just "the '<' flag".该文档也没有将<
部分称为特殊名称,只是“'<'标志”。
If you check the code from Formatter Class you will find:如果您检查Formatter Class 中的代码,您会发现:
Related description from the class documentation: class文档中的相关说明:
Relative indexing is used when the format specifier contains a '<' ('<') flag which causes the argument for the previous format specifier to be re-used.当格式说明符包含“<”(“<”)标志时,使用相对索引,这会导致重新使用前一个格式说明符的参数。 If there is no previous argument, then a MissingFormatArgumentException is thrown.如果没有先前的参数,则抛出 MissingFormatArgumentException。 formatter.format("%s %s %<s %<s", "a", "b", "c", "d") // -> "abbb" // "c" and "d" are ignored because they are not referenced formatter.format("%s %s %<s %<s", "a", "b", "c", "d") // -> "abbb" // "c" 和 "d"忽略,因为它们未被引用
Code reference:代码参考:
// indexing
static final Flags PREVIOUS = new Flags(1<<8); // '<'
The <
flag is a relative argument index. <
标志是相对参数索引。
Constructed as %<s
, it allows you to re-use the argument for the previous %s
format specifier, in this case.构造为%<s
,在这种情况下,它允许您重新使用前一个%s
格式说明符的参数。
You can read more about it and other formatting flags here: java.util.Formatter
你可以在这里阅读更多关于它和其他格式化标志的信息: java.util.Formatter
From the Javadoc:来自 Javadoc:
Relative indexing is used when the format specifier contains a '<' ('<') flag which causes the argument for the previous format specifier to be re-used.当格式说明符包含“<”(“<”)标志时,使用相对索引,这会导致重新使用前一个格式说明符的参数。 If there is no previous argument, then a MissingFormatArgumentException is thrown.如果没有先前的参数,则抛出 MissingFormatArgumentException。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.