From the docs:

  • The format specifiers for general, character, and numeric types have the following syntax:

    %[argument_index$][flags][width][.precision]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.

String.format("%1s %1s %1s", hello);
Answer from Ignacio Vazquez-Abrams on Stack Overflow
🌐
BeginnersBook
beginnersbook.com › 2017 › 10 › java-string-format-method
Java String format() method
June 9, 2024 - public class Example{ public static void main(String args[]){ int number = 123; String formattedNumber = String.format("%+08d", number); System.out.println(formattedNumber); // Output: +0000123 } } We can specify the argument positions using %1$, %2$,..format specifiers.
🌐
DZone
dzone.com › data engineering › data › a clarified string formatting cheatsheet
A Clarified String Formatting Cheatsheet - DZone
September 22, 2017 - Good to keep close by, this simplified guide to String formatting in Java covers numbers, the available flags, and handling multiple arguments.
🌐
Javaprogramto
javaprogramto.com › 2019 › 03 › java-string-format.html
Java String format() Examples JavaProgramTo.com
November 13, 2021 - ... String format method is used to format the string in the specified format. This method can be used as String output format by passing multiple arguments because the second parameter is the variable-arguments(Var-Args).
🌐
W3Schools
w3schools.com › java › ref_string_format.asp
Java String format() Method
A character which indicates how an argument's data should be represented. If the character is uppercase the data will be formatted in uppercase where possible. The list of possible characters is shown in the table below. ... double myNumber = 123456.78; String result; // Default result = String.format("%f", myNumber); System.out.println(result); // Two decimal digits result = String.format("%.2f", myNumber); System.out.println(result); // No decimal digits result = String.format("%.0f", myNumber); System.out.println(result); // No decimal digits but keep the decimal point result = String.format("%#.0f", myNumber); System.out.println(result); // Group digits result = String.format("%,.2f", myNumber); System.out.println(result); // Scientific notation with two digits of precision result = String.format("%.2e", myNumber); System.out.println(result);
🌐
Baeldung
baeldung.com › home › java › java string › java string.format()
Java String.format() | Baeldung
March 23, 2026 - The number of args should match the number of specifiers in the format string; extra arguments, if any, are ignored. Further, when using String.format() in Java, the order of arguments provided to the method must correspond to the order of the ...
🌐
Stack Abuse
stackabuse.com › how-to-format-a-string-in-java-with-examples
Format String in Java with printf(), format(), Formatter and MessageFormat
October 22, 2021 - Here, after the % character, we've passed a number and a format specifier. Specifically, we want a String with 10 characters, followed by a newline. Since stack only contains 5 characters, 5 more are added as padding to "fill up" the String to the character target: ... Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it! ... If no argument index is provided, the arguments will simply follow the order of presence in the method call:
🌐
Scaler
scaler.com › home › topics › java string format()
Java String format() | Java String format() Method - Scaler Topics
April 7, 2024 - Two syntaxes are available for using the format() function in Java. They are: loc is the Locale that needs to be applied by the format function. format is the String object representing the output String format. ...args are zero or more number of arguments that the String object "format" could need.
Find elsewhere
🌐
Alvin Alexander
alvinalexander.com › blog › post › java › use-string-format-java-string-output
Java String formatting with the String.format method (like ‘sprintf’) | alvinalexander.com
July 30, 2024 - String status = String.format("The rename status is (%d)", RENAME_SUCCEEDED); Finally, here is an example of how to use multiple variables with the String format method:
🌐
DZone
dzone.com › coding › java › formatting strings in java: string.format() method
Formatting Strings in Java: String.format() Method - DZone
November 13, 2024 - args: the parameter referenced by format specifiers in the format String. If the arguments are more than the format specifiers, the extra arguments are ignored. The number of arguments can vary and may be omitted completely.
🌐
Baeldung
baeldung.com › home › java › java string › guide to java.util.formatter
Guide to java.util.Formatter | Baeldung
January 8, 2024 - It’s a character indicating how the argument should be formatted. The set of valid conversions for a given argument depends on the argument’s data type · In our example above, if we want to specify the number of an argument explicitly, we can write it using 1$ and 2$ argument indices. Both these being the first and second argument respectively: String greetings = String.format( "Hello %2$s, welcome to %1$s !", "Baeldung", "Folks");
🌐
Developer.com
developer.com › dzone › data engineering › data › comprehensive guide to java string formatting
Comprehensive Guide to Java String Format in 2021
July 1, 2023 - Any arguments provided in an argument list for a non-argument specifier are ignored. For example, the following snippet will produce the same result — the String 100% complete.\nDone. being stored to formattedString — as our previous example: ... Apart from conversions, we can also format Java Strings using flags, widths, and precisions.
🌐
Medium
medium.com › @AlexanderObregon › javas-string-format-method-explained-82707214c953
Java’s String.format() Method Explained | Medium
August 20, 2024 - In this example, the 2$ indicates that the second argument (name) should be used first, and the 1$ indicates that the first argument (age) should be used second. You can combine these elements to create complex format specifiers that precisely control the output: double balance = 12345.6789; String formatted = String.format("Balance: $%,.2f", balance); System.out.println(formatted); // Output: Balance: $12,345.68
🌐
Baeldung
baeldung.com › home › java › java string › named placeholders in string formatting
Named Placeholders in String Formatting | Baeldung
August 27, 2025 - Java standard library provides the String.format() method to format a template-based string, such as String.format(“%s is awesome”, “Java”). In this tutorial, we’ll explore how to make string formatting support named parameters. The String.format() method is pretty straightforward to use. However, when the format() call has many arguments, it gets difficult to understand which value will come to which format specifier, for example:
🌐
Programiz
programiz.com › java-programming › library › string › format
Java String format()
args - 0 or more arguments · returns a formatted string · class Main { public static void main(String[] args) { String language = "Java"; int number = 30; String result; // format object as a string · result = String.format("Language: %s", language); System.out.println(result); // Language: Java // format number as a hexadecimal number ·
🌐
Dot Net Perls
dotnetperls.com › format-java
Java - String.format Examples - Dot Net Perls
June 1, 2023 - This example shows three different syntax forms. We can specify just "%s," use the percentage sign "%," or use a "$" in between. Note It helps to use an index to reference the argument (like "%1") if we reuse the argument, or the references are different in order.