How to format a Java string with printf example

How to print a Java string

To use the Java String printf method to format the output text, follow these rules:

  1. Use %s as printf string specifier in text string being formatted
  2. Use %S as printf string specifier to display uppercase text
  3. Precede the letter s with a number to specify the width of the field
  4. Put a negative sign after the % to left justify the text
  5. Add %n whenever you want to include a line break

Java String Printing Example

Here is a simple example of formatting a Java string with printf, as well as

public class JavaPrintfStringExample {
  /* Java String printf example code. */
  public static void main(String[] args) {
    String name = "Cameron";
    String site = "Tss";
    System.out.printf("I like the articles %s writes on %S. %n", name, site);
    /* Output: I like the articles Cameron writes on TSS. */
  }
}

The example above displays:

I like the articles Cameron writes on TSS.

Although this is a simple example of formatting a Java string with printf, it still demonstrates two interesting features of the syntax:

  1. The case of the name ‘Cameron’ was preserved, because %s was used (lowercase)
  2. The case of Tss was capitalized because %S was used.

The %n is used to force a line break, which does not appear in the output of this example, but is an important element to include when formatting text paragraphs.

Advantages of Java String printf method

The Java String printf method can be confusing at first, but it greatly simplifies the formatting of complex strings, especially when complex database, NoSQL, or Hibernate queries need to be created.

Compare the Java String printf example above with how we should format a text string without printf. The following code snippet illustrates the difference between a string formatted with printf and a string created via string appending:


String name = "Cameron";
String site = "Tss";

/* The following line uses the Java String printf syntax. */
System.out.println( "I like the articles " + name + "  writes on " + site + "." );

/* The following line uses the Java String printf syntax. */ 
System.out.printf( "I like the articles %s writes on %S.%n", name, site );

Even with this simple example, you can see how the Java printf statement formats strings in a much more digestible way. As strings get more and more complicated, Java’s printf method makes them much easier to construct.

Syntax Java String printf

The format of the Java printf function is as follows:

% [flag] [width] specifier-character (s or S)

The most commonly used flag with a Java String printf statement is the negative sign, which will cause the text to be left-aligned.

The width specifier is an integer that specifies how many spaces should be dedicated to displaying the string. If the string is shorter than the specified number, a space is displayed next to the string

Examples of Java String printf format
print flag Objective
%s Display text in uppercase and lowercase preserved
%S Show text in all capitals
%-s Turn text on the left, with String case preserved
%20s Allow 20 spaces to display right-aligned text
%-10S Allow 20 spaces to display left-aligned text and use capital letters

Table Java String printf

A common way to demonstrate advanced Java printf String usage is to format a data table.

In the following advanced Java String printf example, we will generate the following data array:

The data table generated from the sample Java method printf.

Java String printf table example

Here is the complete code for the advanced Java String printf table example:

System.out.printf("--------------------------------%n");
System.out.printf(" Java's Primitive Types         %n");
System.out.printf(" (printf table example)         %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", "long",    "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", "short",   "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

Sample Java Printf String

How to format a string with Java printf method calls.

When the code runs, the output is a simple array that displays information about the 8 Java primitive types.

It also provides a great example of using the Java printf method to format string-based output.

Comments are closed.