Preface
Starting in Java 5.0, the String class has added a powerful string format() method. This method is not used so far, and it is really a waste. This article will take you through the functions of this method quickly. When you want to use formatted text in the future, you may no longer need to borrow third-party library or implement it yourself.
Let’s first look at a simple example:
String formatted = String.format("%s %d this year.", "Xiao Li", 30); // "Xiao Li is 30 years old this year."Without my explanation, you can also see:
1. Placeholder type
The letters following the placeholder "%" determine the type of actual parameters it accepts. There are several placeholder types:
| letter | Applicable parameter types | illustrate |
|---|---|---|
| %a | Floating point number | Output floating point numbers in hexadecimal |
| %b / %B | Any value | If the parameter is null, output false, otherwise output true |
| %c / %C | Character or integer | Output the corresponding Unicode characters |
| %d | Integer | Format the output of integers |
| %e / %E | Floating point number | Output floating point numbers using scientific notation |
| %f | Floating point number | Format the output of floating point numbers |
| %g / %G | Floating point number | Decide whether to output floating point numbers in scientific notation method |
| %h / %H | Any value | Returns the hashCode() value in hexadecimal output parameter |
| %o | Integer | Output integers in octal |
| %s / %S | String | Format the output of the string |
| %t | Date and time | Format the output of date and time |
| %x / %X | Integer | Output integers in hexadecimal |
| %n | none | Line breaks |
| %% | none | The percent sign itself |
Capital letters indicate that the letters output are all capitalized.
The most common ones we use are %s, %d and %f, and occasionally %t. This article is limited in space, so only these four types are introduced. Please read the API documentation for the rest yourself.
2. Format string and integer
Here is an example to illustrate:
// Fill out the spaces and align right: String.format("%10s, world", "Hello"); // Output " Hello, world"String.format("%8d", 123); // Output " 123"// Fill out the spaces and align left: String.format("%-10s, world", "Hello"); // Output "Hello, world"String.format("%-8d", 123); // Output "123 "// Complease 0 and align (valid only for numbers)String.format("%08d", 123); // Output "123 "String.format("%-08d", 123); // Error! 0 is not allowed to be filled on the right side// Output up to N characters String.format("%.5s", "Hello, world"); // Output "Hello"String.format("%.5s...", "Hello, world"); // Output "Hello..."String.format("%10.5s...", "Hello, world"); // Output "Hello..."// Output comma separated numbers String.format("%,d", 1234567); // Output "1,234,567" 3. Date formatting
This is a little more complicated, but if you want to mix text numbers and dates in a string, it should be more convenient to use only one method than combining DateFormat and NumberFormat together.
First, let’s add one more knowledge, that is, placeholders can specify parameters at a certain position, in the format of %n$. For example, %2$d represents the second plastic shaping parameter. Note that n here starts with 1 instead of 0.
When formatting a date, multiple placeholders are required to point to the same parameter (to avoid repeating the same parameter several times). At the same time, because "t" represents date and time, the complete format is %n$tX, where X represents which part of the time is taken. Optional values for X are for example:
Y=year; m=month; d=day; H=hour; M=minute; S=second; L=ms; A=day of the week (name); B=month name;
There are other letters, please refer to the API documentation for details. Here is an example:
// The output format is "Now is 15:04:52, Sunday" // Note that 10 in "%1$10tH" also means that the space is filled with 10 bits and right-aligned String.format("Now is %1$10tH:%1$tM:%1$tS, %1$tA", new Date())Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.