Always forgetting the Java String formatting specifiers? Or maybe you never took the time to learn. Here's a reference of the various flags you can use.
Have you tried to read and understand Java’s String format documentation? I have and found it nearly impenetrable. While it does include all the information, the organization leaves something to be desired.
This guide is an attempt to bring some clarity and ease the usage of string formatting in Java. You may also want to take a look at What's New in Java 8.
String Formatting
The most common way of formatting a string in java is using String.format(). If there were a “java sprintf” then this would be it.
String output = String.format("%s = %d", "joe", 35);
For formatted console output, you can use printf() or the format() method of System.out and System.err PrintStreams.
System.out.printf("My name is: %s%n", "joe");
Create a Formatter and link it to a StringBuilder. Output formatted using the format() method will be 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 Specifiers
Here is a quick reference to all the conversion specifiers supported:
https://dzone.com/articles/java-string-format-examples
Argument Index: %1$s
An argument index is specified as a number ending with a “$” after the “%” and selects the specified argument in the argument list.
String.format("%2$s", 32, "Hello"); // prints: "Hello"
Formatting an Integer
With the %d format specifier, you can use an argument of all integral types including byte, short, int, long and BigInteger.
Default formatting:
String.format("%d", 93); // prints 93
Specifying a width:
String.format("|%20d|", 93); // prints: | 93|
Left-justifying within the specified width:
String.format("|%-20d|", 93); // prints: |93 |
Pad with zeros:
String.format("|%020d|", 93); // prints: |00000000000000000093|
Print positive numbers with a “+”:
(Negative numbers always have the “-” included):
String.format("|%+20d|', 93); // prints: | +93|
A space before positive numbers.
A “-” is included for negative numbers as per normal.
String.format("|% d|", 93); // prints: | 93| String.format("|% d|", -36); // prints: |-36|
Use locale-specific thousands separator:
For the US locale, it is “,”:
String.format("|%,d|", 10000000); // prints: |10,000,000|
Enclose negative numbers within parentheses (“()”) and skip the "-":
String.format("|%(d|", -36); // prints: |(36)|
Octal output:
String.format("|%o|"), 93); // prints: 135
Hex output:
String.format("|%x|", 93); // prints: 5d
Alternate representation for octal and hex output:
Prints octal numbers with a leading “0” and hex numbers with leading “0x“.
String.format("|%#o|", 93);
// prints: 0135
String.format("|%#x|", 93);
// prints: 0x5d
String.format("|%#X|", 93);
// prints: 0X5D
String and Character Conversion
Default formatting:
Prints the whole string.
String.format("|%s|", "Hello World"); // prints: "Hello World"
Specify Field Length
String.format("|%30s|", "Hello World"); // prints: | Hello World|
Left Justify Text
String.format("|%-30s|", "Hello World"); // prints: |Hello World |
Specify Maximum Number of Characters
String.format("|%.5s|", "Hello World"); // prints: |Hello|
Field Width and Maximum Number of Characters
String.format("|%30.5s|", "Hello World"); | Hello|
Summary
This guide explained String formatting in Java. We covered the supported format specifiers. Both numeric and string formatting support a variety of flags for alternative formats. If you want more content on Java Strings, check out the Do's and Don'ts of Java Strings.