Instead of looking at the source code, you should read the javadoc String.format() and Formatter syntax.
You specify the format of the value after the %. For instance for decimal integer it is d
, and for String it is s
:
String aString = "world";
int aInt = 20;
String.format("Hello, %s on line %d", aString, aInt );
Output:
Hello, world on line 20
To do what you tried (use an argument index), you use: *n*$
,
String.format("Line:%2$d. Value:%1$s. Result: Hello %1$s at line %2$d", aString, aInt );
Output:
Line:20. Value:world. Result: Hello world at line 20