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.

Answer from Martin Törnwall on Stack Overflow
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › docs › api › java.base › java › util › Formatter.html
Formatter (Java SE 17 & JDK 17)
April 21, 2026 - An interpreter for printf-style format strings. This class provides support for layout justification and alignment, common formats for numeric, string, and date/time data, and locale-specific output. Common Java types such as byte, BigDecimal, and Calendar are supported.
Top answer
1 of 12
323

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) });
🌐
Javaspecialists
javaspecialists.eu › archive › Issue294-String.format-3x-faster-in-Java-17.html
[JavaSpecialists 294] - String.format() 3x faster in Java 17
One of the most convenient ways of constructing complex Strings is with String.format(). It used to be excessively slow, but in Java 17 is about 3x faster. In this newsletter we discover what the difference is and where it will help you. Also when you should use format() instead of the plain ...
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › docs › api › java.base › java › lang › String.html
String (Java SE 17 & JDK 17)
April 21, 2026 - For additional information on string concatenation and conversion, see The Java Language Specification. Unless otherwise noted, passing a null argument to a constructor or method in this class will cause a NullPointerException to be thrown. A String represents a string in the UTF-16 format in which supplementary characters are represented by surrogate pairs (see the section Unicode Character Representations in the Character class for more information).
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › docs › api › java.base › java › text › Format.html
Format (Java SE 17 & JDK 17)
April 21, 2026 - parseObject(String source, ParsePosition pos) These general methods allow polymorphic parsing and formatting of objects and are used, for example, by MessageFormat. Subclasses often also provide additional format methods for specific input types as well as parse methods for specific result types. Any parse method that does not take a ParsePosition argument should throw ParseException when no text in the required format is at the beginning of the input text.
🌐
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.
🌐
Reddit
reddit.com › r/java › string.format() is 3x faster in java 17
r/java on Reddit: String.format() is 3x faster in Java 17
October 30, 2021 - Yes, String::formatted is just a non-static variant of String::format. So this optimization applies equally to both. ... That's pretty cool. Highest version Java I've ever seen in the wild in Fortune 500 software is 11. Next project for me is in 8. Hope we get to 17 someday.
Find elsewhere
🌐
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:
🌐
OpenRewrite
docs.openrewrite.org › recipe catalog › java › modernize › java.lang apis › prefer `string.formatted(object...)`
Prefer `String.formatted(Object...)` | OpenRewrite Docs
This recipe is used as part of the following composite recipes: Java best practices · java · Diff · package com.example.app; class A { String str = String.format("foo" + "%s", "a"); } package com.example.app; class A { String str = ("foo" ...
Published: 1 month ago
🌐
Medium
medium.com › kwal-it › how-to-format-a-string-in-java-950d07ea8be6
How To Format A String in Java. Formatted is the new kid in town | by Dieter Jordens | Kwal-IT | Medium
May 2, 2024 - You probably think Java String ... to format a String. A lot has happened since java 8. You will see the latest and the fastest way of formatting a string for Java 17....
🌐
Mkyong
mkyong.com › home › java › java string format examples
Java String Format Examples - Mkyong.com
March 10, 2020 - ... package com.mkyong; public class JavaStringFormat1 { public static void main(String[] args) { String result = String.format("%s is %d", "mkyong", 38); // mkyong is 38 System.out.println(result); String result2 = String.format("%d + %d = ...
🌐
DZone
dzone.com › data engineering › data › comprehensive guide to java string formatting
Comprehensive Guide to Java String Format in 2021 - DZone
July 15, 2021 - For example, a format specifier of %.1f with an argument of 1.2345 results in 1.2. Floating point (g, G, a and A): Number of digits in the magnitude after rounding. Character, Integer, Date, Percent Symbol & Line Separator: Must not be specified.
🌐
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:
🌐
Oracle
docs.oracle.com › en › java › javase › 18 › docs › api › java.base › java › util › Formatter.html
Formatter (Java SE 18 & JDK 18)
August 18, 2022 - An interpreter for printf-style format strings. This class provides support for layout justification and alignment, common formats for numeric, string, and date/time data, and locale-specific output. Common Java types such as byte, BigDecimal, and Calendar are supported.
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › docs › api › java.base › java › text › MessageFormat.html
MessageFormat (Java SE 17 & JDK 17)
April 21, 2026 - MessageFormatPattern: String ... can be used to quote any arbitrary characters except single quotes. For example, pattern string "'{0}'" represents string "{0}", not a FormatElement....
🌐
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] ...
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › install › version-string-format.html
Version-String Format
July 15, 2025 - The version string doesn't have trailing zero elements. For example, if the value of $FEATURE is 17, the value of $INTERIM is 0, the value of $UPDATE is 1, and the value of $PATCH is 0, then the full version string is 17.0.1.
🌐
ZetCode
zetcode.com › java › string-format
Java String format - formatting strings in Java
The tY conversion characters give ... digits with leading zeros as necessary. $ java Main.java There are 12 apples, 32 oranges and 43 pears There are 32 apples, 43 oranges and 12 pears Year: 2022, Month: 10, Day: 17...
🌐
Ideas2IT
ideas2it.com › blogs › java-17-heres-a-juicy-update-on-everything-thats-new
Java 17 New Features: Here's a Juicy Update in JDK17
The text block automatically formats the strings in a predictable way and gives the developer control over the format needed. String html = """ <html> <body> <p>Test</p> </body> </html> """; Pattern matching allows the conditional extraction of components from objects to be expressed more concisely and safely. Let's consider the below example, here we are first testing whether obj is a string, then the declaration of string variables, and then type casting obj to a string into variables.