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

This guide tries to make the use of string format in Java more clear and convenient. You may also want to see what's new in Java 8.

String format

The most common way to format a string in java is to use String.format(). If there is a "java sprintf", it is 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 StringBuilder. The output formatted using the format() method will be appended to 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

Here is a quick reference to all supported conversion specifiers:
image.png

Date and time format

Note: Using formatting characters with "%T" instead of "%t" in the following table will make the output uppercase.
image.png

Parameter index

The parameter index is specified as a number ending with "%" after "$", and the specified parameter is selected in the parameter list.

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

Format integer

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

Default format:
String.format("%d", 93); // prints 93
Specify width:

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

Align left within the specified width:

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

Fill with zeros:

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

Use "+" to print positive numbers:

(Negative numbers always contain "-"):

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

Use locale-specific thousands separators:

For the US locale, it is ",":

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

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 for octal and hexadecimal output:

Print the octal number starting with "0" and the hexadecimal number 0x starting with "".

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 align text

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

Specify the 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|

Generalize

This guide explains the string format in Java. We introduced the supported format specifiers. Both the number and string formats support various flags for alternative formats. If you want to know more about Java strings, please check the notes on Java strings, add group: 3907814, get more free information


Java攻城师
451 声望392 粉丝

本人太过于丰富,无法简介