From the docs:
The format specifiers for general, character, and numeric types have the following syntax:
%[argument_index$][flags][width][.precision]conversionThe 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.
String.format("%1
s %1
s %1
s", hello);
Answer from Ignacio Vazquez-Abrams on Stack OverflowFrom the docs:
The format specifiers for general, character, and numeric types have the following syntax:
%[argument_index$][flags][width][.precision]conversionThe 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.
String.format("%1
s %1
s %1
s", hello);
Another option is to use relative indexing: The format specifier references the same argument as the last format specifier.
For example:
String.format("%s %<s %<s %<s", "hello")
results in hello hello hello hello.
Videos
Yes, you can use the $ specifier for this. The number preceding the $ indicates the argument number, starting from 1:
String.format("%1$s FOO %1$s %1$s", "test")
Just as a complement to Keppils answer: When you've started numbering one of your arguments, you have to number them all, or else the result will not be as expected.
String.format("Hello %1$s! What a %2$s %1$s!", "world", "wonderful");
// "Hello world! What a wonderful world!"
would work. While
String.format("Hello %1$s! What a %s %1$s!", "world", "wonderful");
// "Hello world! What a world world!"
would not work. (But does not throw any errors, so this might go unnoticed.)
If you want to put the result in a String you can use :
String result = String.format("%-30s %-30s %-30s", divName, heading1, heading2);
Then you can print it or use it in another places
To get formatting on each of them you need to pass them separately to printf rather than combining them into one string with +.
System.out.printf("%-30s %-30s %-30s", divName, heading1, heading2);
Try:
String formatted = String.format(str, args.toArray());
This gives:
Entered number = 1 and string = abcd
You can use the following:
String str = "Entered number = %d and string = %s";
List<Object> args = new ArrayList<Object>();
args.add(1);
args.add("abcd");
System.out.println(String.format(str, args.toArray()));
will give the output:
Entered number = 1 and string = abcd
From JLS 8.4.1 Format parameters:
The last formal parameter in a list is special;
it may be a variable arity parameter, indicated by an
elipsis following the type.
If the last formal parameter is a variable arity parameter of type T,
it is considered to define a formal parameter of type T[].
The method is then a variable arity method. Otherwise, it is a fixed arity
method. Invocations of a variable arity method may contain more actual
argument expressions than formal parameters. All the actual argument
expressions that do not correspond to the formal parameters preceding
the variable arity parameter will be evaluated and the results stored
into an array that will be passed to the method invocation.
See this question on StackOverflow!