95
 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("%4$2s %3$2s %2$2s %1$2s", "a", "b", "c", "d")
 // -> " d  c  b  a"

In this case, why is a 2 appended to $?

Joey
  • 344,408
  • 85
  • 689
  • 683
andandandand
  • 21,946
  • 60
  • 170
  • 271

4 Answers4

154

The 2 has nothing to do with the $:

  • %     =   Start of format string
  • 4$   =   Fourth argument ('d')
  • 2     =   width of two (right-aligned)
  • s     =   type of String
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
Sven Lilienthal
  • 6,396
  • 4
  • 29
  • 25
  • 1
    That really crisp explanation thanks. However, when I use %4$2s and my argument value is 'ABCDEFGH' it is not retrieving only 2 characters. It retrieved full string. Why? – kandarp May 07 '20 at 05:56
  • 2 = width of two (right-aligned) – mvmn Oct 09 '21 at 12:09
50

The 2$ means put the second argument from the list here. The $ follows a number not precedes it. Similarly, 4$ means put the forth argument here.

To clarify, we can break down the %2$2s format into its parts:

  • % - indicates this is a format string

  • 2$ - shows the second value argument should be put here

  • 2 - the format is two characters long

  • s - format the value as a String

You can find more information in the documentation.

David Webb
  • 190,537
  • 57
  • 313
  • 299
4

%: format string

4$: fourth value argument

2: width (length when argument is printed)

s: it's a string argument conversion

for example, the following snippet:

StringBuffer sb=new StringBuffer();

Formatter formatter=new Formatter(sb,Locale.UK);

formatter.format("-%4$5s-%3$5s-%2$5s-%1$5s-", "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)

and replacing 5 with 2, will produce the following output:

- d- c- b- a-

See the difference? :)

Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
4

Those are positional arguments where %4$2s signals to format the fourth argument 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.

The format specifiers for types which are used to represents dates and times have the following syntax:

%[argument_index$][flags][width]conversion

The optional argument_index is 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

Community
  • 1
  • 1
Joey
  • 344,408
  • 85
  • 689
  • 683
  • Still, I like .NET's format strings better as they make the position of arguments much clearer. – Joey Dec 16 '09 at 14:51