Learn how to printf a Java String
To make use of the Java String printf
technique to format output textual content, observe these guidelines:
- Use
%s
because theprintf
String specifier within the textual content string being formatted. - Use
%S
because theprintf
String specifier to output upper-case textual content. - Precede the letter
s
with a quantity to specify area width. - Put a adverse signal after the
%
to left-justify the textual content. - Add
%n
any time you want to embrace a line break.
Java String printf instance
Right here is an easy instance of easy methods to format a Java String with printf
:
public class JavaPrintfStringExample /* Java String printf instance code. */ public static void important(String[] args) String identify = "Cameron"; String web site = "Tss"; System.out.printf("I just like the articles %s writes on %S. %n", identify, web site); /* Output: I just like the articles Cameron writes on TSS. */
The instance above outputs the next textual content:
I just like the articles Cameron writes on TSS.
This can be a easy instance of easy methods to format a Java String with printf
, however it additionally demonstrates two fascinating options of the syntax:
- Using
%s
(lowercase s) preserves the casing of the identify ‘Cameron’ - Using
%S
(uppercase S) makes the casing of Tss uppercase.
Using %n
forces a line break, which isn’t seen within the output of this instance, however it is a vital component to incorporate when formatting paragraphs of textual content.
Advantages of the Java String printf technique
The Java String printf
technique could be complicated at first, however it enormously simplifies the way you format advanced Strings. That is very true when creating sophisticated database, NoSQL or Hibernate queries.
Examine the Java String printf
instance above with how we must format a textual content String with out printf
. The next code snippet demonstrates the distinction between a String formatted with printf
, and one created by means of String addition:
String identify = "Cameron";
String web site = "Tss";
/* The next line makes use of the Java String printf syntax. */
System.out.println( "I just like the articles " + identify + " writes on " + web site + "." );
/* The next line makes use of the Java String printf syntax. */
System.out.printf( "I just like the articles %s writes on %S.%n", identify, web site );
Even with this straightforward instance, you may see how the Java printf
assertion codecs Strings which might be a lot simpler to digest. As Strings turn into more and more sophisticated, Java’s printf
technique makes these Strings considerably simpler to construct.
Java String printf syntax
The format for the Java printf
perform is as follows:
% [flag] [width] specifier-character (s or S)
The flag most sometimes used with a Java String printf
assertion is the adverse signal, which left-aligns the textual content.
The width specifier is an integer that specifies what number of areas must be devoted to displaying the String. If the String is shorter than the quantity specified, whitespace is displayed adjoining to the String.
printf flag | Function |
%s | Show the textual content with higher and lowercasing preserved |
%S | Show the textual content in all uppercase letters |
%-s | Alight the textual content to the left, with String casing preserved |
%20s | Allocate 20 areas to show the right-aligned textual content |
%-10S | Allocate 20 areas to show the left-aligned textual content and use uppercase lettering |
Java String printf desk
A standard approach to reveal superior Java printf
String utilization is to format a table of information.
The superior Java String printf
instance under will generate the next desk of information:
The information desk generated from the Java printf technique instance.
Java String printf desk instance
Right here is the total code for the superior Java String printf
desk instance:
System.out.printf("--------------------------------%n"); System.out.printf(" Java's Primitive Varieties %n"); System.out.printf(" (String printf instance) %n"); System.out.printf("--------------------------------%n"); System.out.printf("| %-10s | %-8s | %4s |%n", "CATEGORY", "NAME", "BITS"); System.out.printf("--------------------------------%n"); System.out.printf("| %-10s | %-8s | %4s |%n", "Floating", "double", "0064"); System.out.printf("| %-10s | %-8s | %4s |%n", "Floating", "float", "0032"); System.out.printf("| %-10s | %-8s | %4s |%n", "Integral", "lengthy", "0064"); System.out.printf("| %-10s | %-8s | %4s |%n", "Integral", "int", "0032"); System.out.printf("| %-10s | %-8s | %4s |%n", "Integral", "char", "0016"); System.out.printf("| %-10s | %-8s | %4s |%n", "Integral", "quick", "0016"); System.out.printf("| %-10s | %-8s | %4s |%n", "Integral", "byte", "0008"); System.out.printf("| %-10s | %-8s | %4s |%n", "Boolean", "boolean", "0001"); System.out.printf("--------------------------------%n");
Output with Java String printf

Learn how to format a String with Java printf technique calls.
When the code runs, the output is an easy desk that shows details about the 8 Java primitive types.
It additionally offers a fantastic instance of easy methods to use the Java printf
technique to format String-based output.