How to reuse a parameter in String.format?
In java util Formatter class The optional argument_index is a decimal integer that indicates the position of the argument in the argument list. The first argument is referenced by “1$”, the second by “2$”, etc.
In other word the argument index is specified as a decimal integer number ending with a “$” after the “%” and indicates the position of the argument in the argument list.
String firstUser = "Joe Smith"; String secondUser = "Jenny Smith"; String greetings = "This is"; System.out.println(String.format("%1$s %2$s. %1$s %3$s ", greetings, firstUser, secondUser)); // Output: This is Joe Smith. This is Jenny Smith
In the code above the variable greetings is on the first position in the argument list. This variable is reused multiple times using argument_index