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
🌐
DZone
dzone.com › data engineering › data › a clarified string formatting cheatsheet
A Clarified String Formatting Cheatsheet - DZone
September 22, 2017 - The only required elements of a format string are the percentage character % and a conversion character. To round-up the conversion characters I have talked about I have constructed a summary table.
🌐
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
Discussions

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
Does anyone have a concise cheat sheet for f string formatting with numbers
Here's what I have as a reference. Basically a summary of the Format Specification Mini-Language . This would all go after a : in the f-string curly braces. So if you want a float with 2 decimal place precision, with * as fill and right aligned in a field width of 10 characters you'd do f"{val:*>10.2f}" [[fill]align][sign][#][0][minimumwidth][.precision][type] Fill: Add a character to fill with. Must be followed by Align flag. Align: '<' - Forces the field to be left-aligned within the available space (This is the default.) '>' - Forces the field to be right-aligned within the available space. '=' - Forces the padding to be placed after the sign (if any) but before the digits. This is used for printing fields in the form '+000000120'. This alignment option is only valid for numeric types. '^' - Forces the field to be centered within the available space. Sign: '+' - indicates that a sign should be used for both positive as well as negative numbers '-' - indicates that a sign should be used only for negative numbers (this is the default behavior) ' ' - indicates that a leading space should be used on positive numbers #: Flags alternate numbering formats; binary, octal, and hexadecimal output will be prefixed with '0b', '0o', and '0x', respectively. 0: Zero-padding. Equivalent to fill '=' and character of '0' Minimumwidth: Min width of the field. Precision: Decimal places for floats or max field size for non-numeric types. Type: Integers: 'b' - Binary. Outputs the number in base 2. 'c' - Character. Converts the integer to the corresponding Unicode character before printing. 'd' - Decimal Integer. Outputs the number in base 10. 'o' - Octal format. Outputs the number in base 8. 'x' - Hex format. Outputs the number in base 16, using lower- case letters for the digits above 9. 'X' - Hex format. Outputs the number in base 16, using upper- case letters for the digits above 9. 'n' - Number. This is the same as 'd', except that it uses the current locale setting to insert the appropriate number separator characters. '' (None) - the same as 'd' Floats: 'e' - Exponent notation. Prints the number in scientific notation using the letter 'e' to indicate the exponent. 'E' - Exponent notation. Same as 'e' except it converts the number to uppercase. 'f' - Fixed point. Displays the number as a fixed-point number. 'F' - Fixed point. Same as 'f' except it converts the number to uppercase. 'g' - General format. This prints the number as a fixed-point number, unless the number is too large, in which case it switches to 'e' exponent notation. 'G' - General format. Same as 'g' except switches to 'E' if the number gets to large. 'n' - Number. This is the same as 'g', except that it uses the current locale setting to insert the appropriate number separator characters. '%' - Percentage. Multiplies the number by 100 and displays in fixed ('f') format, followed by a percent sign. '' (None) - similar to 'g', except that it prints at least one digit after the decimal point. More on reddit.com
🌐 r/learnpython
5
3
February 2, 2022
Python F-Strings Number Formatting Cheat Sheet
🌐 r/Python
2
82
August 12, 2022
A ‘printf’ formatting output cheat sheet
shit. I wish I had this 3 months ago with I was doing some changes to old ass C code and some bash scripts. More on reddit.com
🌐 r/learnprogramming
1
0
October 10, 2017
🌐
Scribd
scribd.com › document › 882906039 › Java-String-Methods-CheatSheet
Java String Methods CheatSheet | PDF | String (Computer Science) | Computer Programming
Java String Methods CheatSheet - Free download as PDF File (.pdf), Text File (.txt) or read online for free. The document provides an overview of various Java String methods, including length and character access, comparison, searching, substrings, case conversion, trimming, splitting, conversion, interning, and formatting.
🌐
Eriklievaart
eriklievaart.com › cheat › java › lang › format.html
format cheat sheet - Erik's Java Rants
String.format("%d", [number]); plain number String.format("%,d", [number]); separate thousands with ',' String.format("�", [number]); leftpad with spaces String.format(" d", [number]); leftpad with zeroes String.format("%-9d", [number]); rightpad with spaces
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) });
🌐
Edureka
edureka.co › blog › cheatsheets › java-string-cheat-sheet
Java Strings Cheat Sheet | A Complete Reference to Java Strings | Edureka
June 17, 2021 - A handy Java String Cheat Sheet is useful for the aspiring Java developer which will give you all the important methods and concepts of Java String at a glance.
🌐
Alvin Alexander
alvinalexander.com › programming › printf-format-cheat-sheet
A `printf` format reference page (cheat sheet) (C, Java, Scala, etc.) | alvinalexander.com
January 31, 2026 - In this cheat sheet I’ll show all the examples using Perl, but at first it might help to see one example using both Perl and Java. Therefore, here’s a simple Perl printf example to get things started: printf("the %s jumped over the %s, %d times", "cow", "moon", 2); And here are three different Java printf examples, using different string formatting methods that are available to you in the Java programming language:
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:
🌐
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.
🌐
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 - We've started and ended a String "", after which we've opened another one " but haven't closed it. This makes printing reserved characters like this impossible, using this approach. The way to bypass this is by escaping. To print special characters (such as ") directly we need to escape its effects first, and in Java that means prefixing it with a backslash (\).
🌐
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
🌐
Salapura
salapura.com › java › A printf format reference page.pdf pdf
A printf format reference page (cheat sheet)
I originally created this cheat sheet · for my own purposes, and then thought I would share it here. A cool thing about the printf formatting syntax is that the specifiers you can use are very · similar, if not identical, between several different languages, including C, C++, Java, Perl,
🌐
JavaKing
javaking.com › home › latest › java string methods cheat sheet
Java String Methods Cheat Sheet: Quick Reference
January 2, 2026 - Complete Java String methods reference with examples. Covers length, substring, split, replace, format, StringBuilder, and common patterns.
🌐
The Java Cheat Sheet
thejavacheatsheet.wordpress.com › 2015 › 04 › 11 › printf
Printf | The Java Cheat Sheet - WordPress.com
April 12, 2015 - Java's printf() is a method that is used for formatting data output and is a combination of String.format() and out.print(). The types of formatting include: flags width precision conversions General Equation: %[flag][width][.precision][conversion] Flags: plus(+) or minus(-) sign, zero-padding, comma delimiter, and left justify.
🌐
Java Concept Of The Day
javaconceptoftheday.com › home › java strings cheat sheet
Java Strings Cheat Sheet
June 20, 2023 - Java strings quick reference guide for interview, Java strings cheat sheet, Java strings study materials, Java strings quick notes...
🌐
Skillademia
skillademia.com › home › cheat sheets › java
Java Cheat Sheet (2026) — 90+ Syntax & Collections
April 3, 2026 - Skip to content · Pricing · Log In · Metriz-social-facebook Instagram X-twitter Metriz-social-linkedin Ion-logo-youtube · Quick links · Support · © 2025 Skillademia. All rights reserved
🌐
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 - The %d symbol is a placeholder that indicates that a decimal value (something like an int or long in Java) should be printed in place of this symbol. The value of that decimal comes from the variable that follows my string, in this case the RENAME_SUCCEEDED variable. If it helps to see the format method used without the additional log.debug method, here’s an example where I use the String.format method to assign a similarly formatted String to another String:
🌐
Codecademy
codecademy.com › learn › learn-java › modules › learn-java-string-methods › cheatsheet
Learn Java: String Methods Cheatsheet | Codecademy
toLowerCase() returns the string value converted to lowercase. ... Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
🌐
Baeldung
baeldung.com › home › java › java string › guide to java.util.formatter
Guide to java.util.Formatter | Baeldung
January 8, 2024 - A template is a String that contains some static text and one or more format specifiers, which indicate which argument is to be placed at the particular position. In this case, there’s a single format specifier %s, which gets replaced by the corresponding argument.