🌐
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:
🌐
W3Schools
w3schools.com › java › ref_string_format.asp
Java String format() Method
Strings Concatenation Numbers and Strings Special Characters Code Challenge Java Math Java Booleans
🌐
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 IllegalFormatConversionException.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Formatter.html
Formatter (Java Platform SE 8 )
April 21, 2026 - Java™ Platform Standard Ed. 8 ... 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 ...
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › String.html
String (Java Platform SE 8 )
April 21, 2026 - For additional information on string ... 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 ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-string-format-method-with-examples
Java String format() Method - GeeksforGeeks
June 2, 2026 - Supports formatting of numbers, strings, dates, and other data types. Uses format specifiers such as %s, %d, %f, and %c. Example: Java program to demonstrate working of format() method
🌐
DZone
dzone.com › data engineering › data › java string format examples
Java String Format Examples | Novixys Software Dev Blog
July 11, 2021 - The most common way of formatting a string in java is using String.format().
🌐
Programiz
programiz.com › java-programming › library › string › format
Java String format()
Java String format() Java String getBytes() Java Varargs · Java Method Overloading · Java Object toString() Java String toLowerCase() Java String valueOf() The format() method returns a formatted string based on the argument passed. class Main { public static void main(String[] args) { String ...
🌐
Vultr Docs
docs.vultr.com › java › standard-library › java › lang › String › format
Java String format() - Format String Text | Vultr Docs
December 20, 2024 - By mastering String.format(), you can control text alignment, incorporate multiple variables into strings, and customize numeric and date/time formatting effortlessly. Implement these techniques in your Java applications to produce more readable, professional outputs that effectively communicate data states and values to users.
Find elsewhere
🌐
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);
🌐
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.
🌐
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 - Java String formatting combines conversions, flags, widths, and precisions. We cover the basics and provide detailed tables for future reference.
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) });
🌐
Baeldung
baeldung.com › home › java › java string › guide to java.util.formatter
Guide to java.util.Formatter | Baeldung
January 8, 2024 - Introduction to formatting Strings in Java using the java.util.Formatter.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › text › Format.html
Format (Java Platform SE 8 )
April 21, 2026 - Java™ Platform Standard Ed. 8 ... Format is an abstract base class for formatting locale-sensitive information such as dates, messages, and numbers. Format defines the programming interface for formatting locale-sensitive objects into Strings (the format method) and for parsing Strings back ...
🌐
Oracle
docs.oracle.com › javase › 9 › docs › api › java › lang › String.html
String (Java SE 9 & JDK 9 )
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 ...
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › util › Formatter.html
Formatter (Java Platform SE 7 )
Java™ Platform Standard Ed. 7 ... 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 ...
🌐
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 - This means that the formatting will be the same, regardless of whether you're using Java, Python, or some other language that supports MessageFormat. MessageFormat extends the abstract Format class, just how DateFormat and NumberFormat do. The Format class is meant to format locale-sensitive objects into Strings. Let's see a nice example, courtesy of MessageFormat's documentation.
🌐
GNU
gnu.org › software › gettext › manual › html_node › java_002dformat.html
java-format (GNU gettext utilities)
Java format strings are described in the JDK documentation for class java.text.MessageFormat, https://docs.oracle.com/javase/7/docs/api/java/text/MessageFormat.html.
🌐
Oracle
docs.oracle.com › en › java › javase › 15 › docs › api › java.base › java › lang › String.html
String (Java SE 15 & JDK 15)
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 ...