简体   繁体   English

标志,System.out.format Java

[英]Flags, System.out.format Java

Talking about flags, it's described in the javadoc that the flag ' ' (empty space) will insert automatically an empty space instead of the sign "+" just at the beginning of the number. 谈到标志,在Javadoc中描述了标志''(空白)将在数字的开头自动插入一个空白而不是符号“ +”。 However when I try it it works only when the first optional argument of the format specifier is filled. 但是,当我尝试它时,它仅在格式说明符的第一个可选参数被填充时才有效。

  System.out.printf("% d%n %1$ d%n",0x3);

Even though there is an empty space in both the formatters, it works only in the latter one %1$ d%n whereas in the former % d%n the leading empty space meaning the positive number is absent (even though the flag ' ' is on). 即使两个格式化程序中都有一个空格,它也仅在后一个%1$ d%n起作用,而在前一个% d%n ,前导空格意味着没有正数(即使标志''开启)。

Is there any specific reason why it happens like that? 发生这种情况有什么具体原因吗? Thanks in advance. 提前致谢。

Here is the output of that line of code: 这是该行代码的输出:

 3
  3

Both lines have a leading space, however, the second line has two spaces because you have a space between the second and third conversion, ie if I split the String to make it more readable: 这两行都有一个前导空格,但是,第二行有两个空格,因为在第二个和第三个转换之间有一个空格,即如果我分割String使其更具可读性:

"% d" + "%n" + " " + "%1$ d" + "%n"

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

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