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 › lang › String.html
String (Java SE 17 & JDK 17)
April 21, 2026 - 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 ...
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) });
Discussions

JDK 17 : Extend FormatString bug pattern to detect IllegalFormatArgumentIndexException
As pointed out by Dr. Heinz M. Kabutz: [1] String.format() method parsing machinery was optimized by factor 3 in Java 17 release and apparently the check is implemented in stricter way. More on github.com
🌐 github.com
2
January 2, 2022
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
When will Java get type aliasing?
It probably won't. I don't see a JEP for it. More on reddit.com
🌐 r/java
18
0
March 13, 2019
It looks like JDK20 is getting a String Templates preview!
While I’m not that happy about the \{} syntax, it’s really cool that Java will finally have this. More on reddit.com
🌐 r/java
82
147
July 6, 2022
🌐
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.
🌐
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.
🌐
Javaspecialists
javaspecialists.eu › archive › Issue294-String.format-3x-faster-in-Java-17.html
[JavaSpecialists 294] - String.format() 3x faster in Java 17
October 29, 2021 - 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 String addition with +.
🌐
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 - Parses text from a string to produce an object. equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait ... Sole constructor. (For invocation by subclass constructors, typically implicit.) ... Formats an object to produce a string.
🌐
OpenRewrite
docs.openrewrite.org › recipe catalog › java › modernize › java.lang apis › prefer `string.formatted(object...)`
Prefer String.formatted(Object...) | OpenRewrite Docs
GitHub: StringFormatted.java, Issue Tracker, Maven Central · This recipe is available under the Moderne Source Available License. This recipe is used as part of the following composite recipes: ... @@ -3,2 +3,2 @@ package com.example.app; class A { - String str = String.format("foo" - + "%s", "a"); + String str = ("foo" + + "%s").formatted("a"); }
Published   May 18, 2026
Find elsewhere
🌐
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....
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › install › version-string-format.html
Version-String Format
July 15, 2025 - Java SE platform has adopted time-based release model with the JDK being released every six months. As of JDK 10 and later, the format of the version string, which reflects the Java SE platform's time-based release model, is $FEATURE.$INTERIM.$UPDATE.$PATCH.
🌐
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:
🌐
Coderanch
coderanch.com › t › 746747 › java › String-format-lot-faster-Java
String.format() is a lot faster in Java 17 (Performance forum at Coderanch)
I just saw this post on Heinz Kabutz's blog -- perfomance issues are one of the things he often writes about. https://www.javaspecialists.eu/archive/Issue294-String.format-3x-faster-in-Java-17.html Most people won't want to upgrade to Java 17 to take advantage of this improvement, but it's interesting to see what the Java crew is doing in the background to improve the language.
🌐
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 - Note: MessageFormat differs from the other Format classes in that you create a MessageFormat object with one of its constructors (not with a getInstance style factory method). The factory methods aren't necessary because MessageFormat itself doesn't implement locale specific behavior. Any locale specific behavior is defined by the pattern that you provide as well as the subformats used for inserted arguments. MessageFormat uses patterns of the following form: MessageFormatPattern: String MessageFormatPattern FormatElement String FormatElement: { ArgumentIndex } { ArgumentIndex , FormatType } { ArgumentIndex , FormatType , FormatStyle } FormatType: one of number date time choice FormatStyle: short medium long full integer currency percent SubformatPattern
🌐
DZone
dzone.com › data engineering › data › comprehensive guide to java string formatting
Comprehensive Guide to Java String Format in 2021
July 15, 2021 - In this article, we will outline the basic techniques for formatting Strings in Java using the venerable C-style and walk through easy-to-digest tables for all of the conversions and format specifiers available to us (that can be used as reference tables when we inevitably forget which format specifiers to use).
🌐
GitHub
github.com › google › error-prone › issues › 2791
JDK 17 : Extend FormatString bug pattern to detect IllegalFormatArgumentIndexException · Issue #2791 · google/error-prone
January 2, 2022 - This code that is shared between Open Source GerritCodeReview project and apparently internal Google code: java/com/google/gerrit/server/git/MergeUtil.java String oursNameFormatted = String.format( "%0$-" + nameLength + "s (%s %s)", ours...
Author   google
🌐
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> """; ... This JEP introduces a new Java 2D internal rendering pipeline for macOS, replacing the deprecated OpenGL API (which was phased out in macOS 10.14) previously used in Swing GUI applications.
🌐
ZetCode
zetcode.com › java › string-format
Java String format - formatting strings in Java
The tY conversion characters give ... 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...
🌐
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.
🌐
Mkyong
mkyong.com › home › java › java string format examples
Java String Format Examples - Mkyong.com
March 10, 2020 - Further Reading- Formatter JavaDoc. ... 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 = %d", 1, 1, 1 + 1); // 1 + 1 = 2 System.out.println(result2); String result3 = String.format("%s = %f", "PI", Math.PI); // PI = 3.141593 String result4 = String.format("%s = %.3f", "PI", Math.PI); // PI = 3.142 System.out.println(result3); System.out.println(result4); } }
🌐
DEV Community
dev.to › msmittalas › new-way-to-use-strings-in-java-17-2i1l
New way to Use Strings in Java 17 - DEV Community
June 10, 2022 - Before Java 15, Writing SQL, HTML, JSON or any Polyglot scripts in String were difficult. It was ugly and difficult to read. For example : Following HTML in Java : ... With "JEP 378: Text Blocks" introduced in Java 15 ( Java 17 LTS), We can use two dimensional block of text.