简体   繁体   English

这是什么 %<div id="text_translate"><p> 我最近遇到了以下字符串:“%<s”。 我注意到这具有以下效果:</p><pre> String sampleString = "%s, %<s %<s"; String output = String.format(sampleString, "A"); System.out.println(output); // A, A A.</pre><p> 我试图用谷歌搜索这个 %<s 叫什么,但无济于事。 我从上面的代码中知道,我只需要输入一次格式字符串,它就会替换所有 %s & %<s 的实例。</p><p> 很好奇这个名字叫什么!</p></div>

[英]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.

相关问题 这个语言功能叫什么? - What's this language feature called? 这个java模式叫什么? - What's this java pattern called? 在Java中调用地图的containsKey(E e)时会发生什么 - what happend when the Map's containsKey(E e) is called in Java 验证使用Mockito调用函数的次数有什么意义? - What's the point of verifying the number of times a function is called with Mockito? 在什么条件下会调用ELResolver的setValue方法? - Under what conditions is an ELResolver's setValue method called? 多次调用时,Android PhoneStateListener 的行为是什么? - What is the Android PhoneStateListener's Behavior When Called Multiple Times? ByteBuffer的flip方法的目的是什么? (为什么它被称为“翻转”?) - What is the purpose of ByteBuffer's flip method? (And why is it called "flip"?) 座钟可滚动标签栏-它叫什么? - Desk clock scrollable tab bar - what's it called? 将参数添加到从我的项目中的100个不同位置调用的方法 - 什么是正确的方法? - Adding a parameter to a method that's used called from 100 different places in my project - What's the proper approach? Java - 为什么重写方法被调用两次(或者至少它看起来像这样)? - Java - Why is overriden method being called twice (or at least that's what it seems)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM