Always forget the Java string format specifiers? Or maybe you never took the time to learn. This is a reference to the various flags you can use. Have you tried to read and understand Java’s string format documentation? I’ve found it almost impenetrable. While it does contain all the information, the organization still has some shortcomings.

This guide attempts to make the use of string formats in Java cleaner and easier. You may also want to look at the new features in Java 8.

String format

The most common way to format strings in Java is to use string.format (). If there is a “Java Sprintf,” that is it.

String output = String.format("%s = %d", "joe", 35);

For formatted console output, you can use the format() methods of printf() or System.out and System.Err printStreams.

System.out.printf("My name is: %s%n", "joe");

Create a Formatter and link it to a StringBuilder. The output formatted using the format() method is appended to the StringBuilder.

StringBuilder sbuf = new StringBuilder();
Formatter fmt = new Formatter(sbuf);
fmt.format("PI = %f%n", Math.PI);
System.out.print(sbuf.toString());
// you can continue to append data to sbuf here.

Format specifier

The following is a quick reference to all supported conversion specifiers:

Date and time format

Note: Using a formatting character with ‘%T’ instead of ‘%T’ in the following table causes the output to be capitalized.

Parameter index

The parameter index specifies the number ending in ‘%’ after ‘$’ and selects the specified parameter in the parameter list.

String.format("%2$s", 32, "Hello"); // prints: "Hello"

Formatted integer

Using the %d format specifier, you can use arguments of all integer types, including byte, short, int, long, and BigInteger.

Default format: string.format (“%d”, 93); // Prints can be used to specify the widths:

String.format("|%20d|", 93); // prints: |                  93| 

Left-align within the specified width:

String.format("|%-20d|", 93); // prints: |93                  |

Fill with zeros:

String.format("|%020d|", 93); // prints: |00000000000000000093|

Print positive numbers with “+” :

(Negative numbers always contain a “-“) :

String.format("|%+20d|', 93); // prints: |                 +93|

Using locale specific thousand separators:

For the American locale, it is “, “:

String.format("|%,d|", 10000000); / / prints: 10000000 | |

Enclose negative numbers in parentheses (” () “) and skip the “-” :

String.format("|%(d|", -36); // prints: |(36)|

Octal output:

String.format("|%o|"), 93); // prints: 135

Hexadecimal output:

String.format("|%x|", 93); // prints: 5d

Alternative representations of octal and hexadecimal output:

Print octal digits starting with “0” and hexadecimal digits starting with “” 0x.

String.format("|%#o|", 93);
// prints: 0135

String.format("|%#x|", 93);
// prints: 0x5d

String.format("|%#X|", 93);
// prints: 0X5D

String and character conversion

Default format:

Print the entire string.

String.format("|%s|", "Hello World"); // prints: "Hello World"

Specify field length

String.format("|%30s|", "Hello World"); // prints: | Hello World|

Left-aligned text

String.format("|%-30s|", "Hello World"); // prints: |Hello World |

Specifies the maximum number of characters

String.format("|%.5s|", "Hello World"); // prints: |Hello|

Field width and maximum number of characters

The String. Format (" | | % 30.5 s, "" Hello World"); | Hello|

summary

This guide explains string formatting in Java. We introduced the supported format specifiers. Both numeric and string formats support a variety of flags for alternative formats. If you want to learn more about Java strings, please check out the Java Strings Dos and don ‘ts section, 3907814, for more free information