理解 Java 格式字符串中的 $
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1915074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Understanding the $ in Java's format strings
提问by andandandand
StringBuilder sb = new StringBuilder();
// Send all output to the Appendable object sb
Formatter formatter = new Formatter(sb, Locale.US);
// Explicit argument indices may be used to re-order output.
formatter.format("%4s %3s %2s %1s", "a", "b", "c", "d")
// -> " d c b a"
In this case, why is a 2 appended to $?
在这种情况下,为什么 2 附加到 $?
采纳答案by Sven Lilienthal
The 2has nothing to do with the $:
该2有什么做的$:
%= Start of format string4$= Fourth argument ('d')2= width of two (right-aligned)s= type of String
%= 格式字符串的开始4$= 第四个参数 ('d')2= 两个的宽度(右对齐)s= 字符串类型
回答by Dave Webb
The 2$means put the second argument from the list here. The $followsa number not precedes it. Similarly, 4$means put the forth argument here.
该2$方法把第二个参数从列表在这里。该$遵循其人数不得先于它。同样,4$意思是在这里提出第四个论点。
To clarify, we can break down the %2$2sformat into its parts:
为了澄清,我们可以将%2$2s格式分解为几个部分:
%- indicates this is a format string2$- shows the second value argument should be put here2- the format is two characters longs- format the value as a String
%- 表示这是一个格式字符串2$- 显示第二个值参数应该放在这里2- 格式为两个字符长s- 将值格式化为字符串
You can find more information in the documentation.
您可以在文档中找到更多信息。
回答by Joey
Those are positional arguments where %4$2ssignals to format the fourthargument as a string with width 2. This is especially helpful when providing strings for localization where arguments need to be reordered without touching the source code.
这些是位置参数,其中%4$2s信号将第四个参数格式化为宽度为 2 的字符串。这在提供用于本地化的字符串时特别有用,其中参数需要在不接触源代码的情况下重新排序。
The format specifiers for types which are used to represents dates and times have the following syntax:
%[argument_index$][flags][width]conversionThe optional
argument_indexis a decimal integer indicating the position of the argument in the argument list. The first argument is referenced by"1$", the second by"2$", etc. —Formatter documentation
用于表示日期和时间的类型的格式说明符具有以下语法:
%[argument_index$][flags][width]conversion可选
argument_index是一个十进制整数,表示参数在参数列表中的位置。第一个参数被引用"1$",第二个被引用"2$",等等。 — Formatter 文档
回答by Rahul Mishra
%: format string
%: 格式字符串
4$: fourth value argument
4$: 第四个值参数
2: width (length when argument is printed)
2:宽度(打印参数时的长度)
s: it's a string argument conversion
s: 这是一个字符串参数转换
for example, the following snippet:
例如,以下代码段:
StringBuffer sb=new StringBuffer();
Formatter formatter=new Formatter(sb,Locale.UK);
formatter.format("-%4s-%3s-%2s-%1s-", "a", "b", "c", "d");
System.out.println(sb);
produces an output of :
产生输出:
- d- c- b- a-
(width of 5 characters per argument, padded with spaces)
(每个参数 5 个字符的宽度,用空格填充)
and replacing 5with 2, will produce the following output:
并替换5为2, 将产生以下输出:
- d- c- b- a-
See the difference? :)
看到不同?:)

