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.
Discussions

java - Can Stringformatter reuse arguments? - Stack Overflow
I'm using String.format to create a formatted string with arguments. Is it somehow possible to tell the formatter to reuse an argument multiple times? More on stackoverflow.com
🌐 stackoverflow.com
String formatting with variable number of arguments in java - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
🌐 stackoverflow.com
How to format multiple string parameters in Java - Stack Overflow
The problem I'm having is specifying the length of the strings divName, heading1, and heading2. "%-30s" formats one of them, on either end, but for the life of me, I can't get the formatting right to set a length for each one. I tried three sets of specifiers separated by commas but it just ... More on stackoverflow.com
🌐 stackoverflow.com
January 25, 2018
String.format() is 3x faster in Java 17
Glad to see some of the small enhancements we did in 17 get recognition. Not sure if this one matters, but String::format shows up in profiles every now and then so it felt reasonable to me to give it some TLC in between larger projects. More on reddit.com
🌐 r/java
41
298
October 30, 2021
🌐
W3Schools
w3schools.com › java › ref_string_format.asp
Java String format() Method
Data from the additional arguments is formatted and written into placeholders in the format string, which are marked by a % symbol.
🌐
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).
🌐
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 - One way to format Java string output is with the format method of the String class, which works like a “Java sprintf” method. Here are some examples: // output a string log.debug( String.format("The rename status is (%d)", RENAME_SUCCEEDED) ); // format some text and assign it to a string String status = String.format("The rename status is (%d)", RENAME_SUCCEEDED); // format a string with multiple variables (interpolation) log.debug( String.format("%s is %d years old, er, young", "Al", 45) );
🌐
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
🌐
DZone
dzone.com › data engineering › data › a clarified string formatting cheatsheet
A Clarified String Formatting Cheatsheet - DZone
September 22, 2017 - This is not an exhaustive list, for you must consult the Java documentation. Strings can be formatted in very much the same way as for numbers and will use many of the same flags. Let’s start by looking at a String formatted with several arguments. The formatted string can contain multiple arguments of different types.
🌐
Baeldung
baeldung.com › home › java › java string › java string.format()
Java String.format() | Baeldung
March 23, 2026 - Specifically, the first argument maps to the first % specifier, the second argument to the second % specifier, and so on. However, the Java Formatter syntax, which String.format() utilizes, also supports positional arguments.
🌐
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.
🌐
Dot Net Perls
dotnetperls.com › format-java
Java - String.format Examples - Dot Net Perls
This example uses both a string and an integer in one format string. It also inserts the second argument first—it uses the "%2" syntax for this.
🌐
W3Resource
w3resource.com › java-tutorial › string › string_format.php
Java String: format Method - w3resource
August 19, 2022 - args - Arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. The number of arguments is variable and may be zero.
🌐
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");
🌐
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:
🌐
Baeldung
baeldung.com › home › java › java string › named placeholders in string formatting
Named Placeholders in String Formatting | Baeldung
August 27, 2025 - 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:
🌐
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
🌐
Programiz
programiz.com › java-programming › library › string › format
Java String format()
Online Python Online JavaScript Online SQL Online Java Online HTML Online C Online C++ Online C# Online PHP Online Swift Online Kotlin Online TypeScript Online Go Online Rust Online Scala Online Dart Online R Online Ruby ... The format() method returns a formatted string based on the argument passed.
🌐
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.