It works same as printf() of C.

%s for String 
%d for int 
%f for float

ahead

String.format("%02d", 8)

OUTPUT: 08

String.format("%02d", 10)

OUTPUT: 10

String.format("%04d", 10)

OUTPUT: 0010

so basically, it will pad number of 0's ahead of the expression, variable or primitive type given as the second argument, the 0's will be padded in such a way that all digits satisfies the first argument of format method of String API.

Answer from Hiren on Stack Overflow
🌐
W3Schools
w3schools.com › java › ref_string_format.asp
Java String format() Method
Note: You will find more "Try it Yourself" examples at the bottom of this page. The format() method returns a formatted string using a locale, format and additional arguments.
🌐
Baeldung
baeldung.com › home › java › java string › java string.format()
Java String.format() | Baeldung
March 23, 2026 - For format specifiers that don’t correspond to arguments, the conversion is a character indicating content to be inserted in the output. ... The behavior of a null argument depends on the conversion. For example, characters s and S evaluate to null if the argument arg is null. Let’s demonstrate string formatting with an example JUnit test method:
Discussions

How to use String.format() in Java? - Stack Overflow
Maybe another example would be this: String s = String.format("Hello %s answered your question", "placeofm"); When you print the string to show it in the console it would look like this ... The placeholder d is for a decimal number with two digits. %s is for a String like my name in the sample above. If you want to learn Java ... More on stackoverflow.com
🌐 stackoverflow.com
How to format strings in Java - Stack Overflow
There are plenty of ways to format Strings using external libraries. They add little to no benefit if the libraries are imported solely for the purpose of String formatting. Few examples: Apache Commons: StringSubstitutor, examples in its JavaDoc. More on stackoverflow.com
🌐 stackoverflow.com
Help with String.format
Why are you chaining the Strings with concatenation? String.format works totally different from what you do with it. With String.format you don't concatenate, you write your text with placeholders and add the values separated by commas after the text: String info = String.format("The movie %15s was directed by %15s and cost %.3f to make.", this.getTitle(), this.getDirector(), this.getProductionCost()); See: String.format and Formatter as well as the Formatting Numeric Print Output Tutorial More on reddit.com
🌐 r/learnjava
10
3
October 18, 2016
s-Strings in Java?
This is called "string interpolation" and despite multiple proposals it still is not supported in Java. "String.format()" is the closest you can get. Some Java versions have a ".formatted()" method on Strings as well. More on reddit.com
🌐 r/java
23
27
March 3, 2021
🌐
Jmu
wiki.cs.jmu.edu › reference › java › formatting
Formatting Strings and Output in Java - JMU CS Wiki
January 26, 2026 - The static format() method in the String class is passed one parameter called a format string and then any number of other parameters. In the above example, the format string is "CS=" and the one other parameter is course. A format string consists of String literals and format specifiers.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-string-format-method-with-examples
Java String format() Method - GeeksforGeeks
June 2, 2026 - IllegalFormatException:If the format specified is illegal or there are insufficient arguments. Example: Using String.format() method to show concatinate the two floating values of given variable using /.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Formatter.html
Formatter (Java Platform SE 8 )
April 21, 2026 - The locale used is the default locale for formatting for this instance of the Java virtual machine. ... Constructs a new formatter with the specified locale. The destination of the formatted output is a StringBuilder which may be retrieved by invoking out() and whose current content may be converted into a string by invoking toString().
🌐
CodeSignal
codesignal.com › learn › courses › java-string-manipulation-for-beginners › lessons › string-formatting-in-java-enhancing-readability-of-your-data
String Formatting in Java: Enhancing Readability of Your ...
In this example, we fill the additional spaces with 0: int number = 10; // `d` specifies the number will be returned with 5 characters in it, extra digits will be filled with 0 String formatted = String.format("Number [d]", number); System.out.println(formatted); // Prints: Number [00010] This ...
🌐
Programiz
programiz.com › java-programming › library › string › format
Java String format()
System.out.println(String.format("n1 = %.2f", n1)); // -452.53 System.out.println(String.format("n2 = %.2f", n2)); // -345.77 } } ... Note: When we format -452.534 using %f, we are getting -452.533997. It is not because of the format() method. Java doesn't return the exact representation of ...
Find elsewhere
🌐
Kansas State University
textbooks.cs.ksu.edu › cc210 › 09-strings › 06-java › 04-formatting
String Formatting :: CC 210 Textbook
June 27, 2024 - Java also includes a special string method, the format() method, which allows us to use placeholders in our output string, and then replace those placeholders with the values stored in variables. Here’s an example of how to use that method in Java:
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › How-to-format-a-Java-String-with-printf-example
How to format a Java String with printf example
July 24, 2025 - Use %S as the printf String specifier to output upper-case text. Precede the letter s with a number to specify field width. Put a negative sign after the % to left-justify the text.
🌐
DZone
dzone.com › coding › java › formatting strings in java: string.format() method
Formatting Strings in Java: String.format() Method - DZone
November 13, 2024 - Core Java Specialization | Enroll in Free Online Course Today* ... There are three primary ways to format a string in Java. You can use the String.format() method, the printf() method, or the MessageFormat class for formatting strings.
🌐
CodeGym
codegym.cc › java blog › strings in java › java string format()
Java String format()
The Java string format() method is used to format strings, integers, decimal values, and so on, by using different format specifiers. This method returns the formatted string using the given locale, specified formatter, and arguments. If no locale is provided then it uses the default locale for formatting the strings...
Published   December 24, 2024
🌐
Novixys Software
novixys.com › blog › java-string-format-examples
Java String Format Examples | Novixys Software Dev Blog
February 4, 2018 - If there were a “java sprintf”, this would be it. String output = String.format("%s = %d", "joe", 35);
🌐
DZone
dzone.com › data engineering › data › java string format examples
Java String Format Examples | Novixys Software Dev Blog
July 11, 2021 - If there were a “java sprintf” then this would be it. String output = String.format("%s = %d", "joe", 35);
🌐
TechVidvan
techvidvan.com › tutorials › java-string-format-method
Java String format() Method with Examples - TechVidvan
February 22, 2024 - Java is simple to understand and implement code in a variety of platforms. We are going to learn about String format(). String format() is used to create formatted String in format pattern.
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › util › Formatter.html
Formatter (Java Platform SE 7 )
The locale used is the default locale for this instance of the Java virtual machine. ... Constructs a new formatter with the specified locale. The destination of the formatted output is a StringBuilder which may be retrieved by invoking out() and whose current content may be converted into a string by invoking toString().
Top answer
1 of 12
324

Take a look at String.format. Note, however, that it takes format specifiers similar to those of C's printf family of functions -- for example:

String.format("Hello %s, %d", "world", 42);

…would return "Hello world, 42". The "format string" link points to the complete official spec, but for simple cases, this much shorter documentation may be helpful for an introduction to format specifiers even though it's outdated and about Lava. The most commonly used ones are:

  • %s - insert a string
  • %d - insert a signed integer (decimal)
  • %f - insert a real number, standard notation

This is radically different from C#, which uses positional references with an optional format specifier. That means that you can't do things like:

String.format("The {0} is repeated again: {0}", "word");

... without actually repeating the parameter passed to printf/format. (see The Scrum Meister's comment below)


If you just want to print the result directly, you may find System.out.printf (PrintStream.printf) to your liking.

2 of 12
182

In addition to String.format, also take a look java.text.MessageFormat. The format less terse and a bit closer to the C# example you've provided and you can use it for parsing as well.

For example:

int someNumber = 42;
String someString = "foobar";
Object[] args = {new Long(someNumber), someString};
MessageFormat fmt = new MessageFormat("String is \"{1}\", number is {0}.");
System.out.println(fmt.format(args));

A nicer example takes advantage of the varargs and autoboxing improvements in Java 1.5 and turns the above into a one-liner:

MessageFormat.format("String is \"{1}\", number is {0}.", 42, "foobar");

MessageFormat is a little bit nicer for doing i18nized plurals with the choice modifier. To specify a message that correctly uses the singular form when a variable is 1 and plural otherwise, you can do something like this:

String formatString = "there were {0} {0,choice,0#objects|1#object|1<objects}";
MessageFormat fmt = new MessageFormat(formatString);
fmt.format(new Object[] { new Long(numberOfObjects) });
🌐
Tutorialspoint
tutorialspoint.com › java › lang › string_format.htm
Java - String format() Method
In this example, we are creating an instantiating the string class with the value "Hello". Then, using the format() method, we are trying to get the formatted string of the current string in the specified format null. package com.tutorialspoint; ...
🌐
Baeldung
baeldung.com › home › java › java string › guide to java.util.formatter
Guide to java.util.Formatter | Baeldung
January 8, 2024 - The set of valid conversions for ... argument indices. Both these being the first and second argument respectively: String greetings = String.format( "Hello %2$s, welcome to %1$s !", "Baeldung", "Folks");...
🌐
Codecademy
codecademy.com › docs › java › strings › .format()
Java | Strings | .format() | Codecademy
May 29, 2025 - String.format(Locale.FRANCE, "%.2f", 1234.56); // Uses comma as decimal separator ... If the arguments don’t match the format specifiers in .format(), Java throws a java.util.IllegalFormatException, such as MissingFormatArgumentException or ...