Use of Java string Formatting-string.format ()
Formatting of regular types
The format () method of the string class is used to create a formatted string and to concatenate multiple string objects. Students familiar with C language should remember the C language of the sprintf () method, the two have similarities. There are two overloaded forms of the format () method.
Format (string format, Object ... args) a new string that uses the local locale to develop a string format and parameters to generate a new formatted string.
Format (locale locale, string format, Object ... args) uses the specified locale to develop string formatting and parameters to generate a formatted string.
The role of formatting
Not only can you convert integers, string types to different types of data, but you can also convert strings to HTML code, which is a powerful feature. Especially when doing the website is particularly important, can make complex problem simplification.
1. Display different conversion characters to implement different data types to string conversions.
Conversion character |
Description |
Example |
%s |
String type |
"Mingrisoft" |
%c |
Character type |
' m ' |
%b |
Boolean type |
True |
%d |
Integer type (decimal) |
99 |
%x |
Integer type (hex) |
Ff |
%o |
Integer type (octal) |
77 |
%f |
Floating-point types |
99.99 |
%a |
Hexadecimal floating-point type |
Ff.35ae |
%e |
Index type |
9.38e+5 |
%g |
Common floating-point types (shorter in type F and e) |
|
%h |
Hash code |
|
%% |
Percent type |
% |
%n |
Line break |
|
%tx |
Date and Time type (x represents a different date and time conversion character |
|
2. Match the symbol of the translator.
Flag |
Description |
Example |
Results |
+ |
Add a symbol to a positive or negative number |
("%+d", 15) |
+15 |
− |
Align Left |
("%-5d", 15) |
|15 | |
0 |
Number Front 0 |
("%04D", 99) |
0099 |
Space |
Add a specified number of spaces before an integer |
("% 4d", 99) |
| 792 |
, |
Group numbers with "," |
("%,f", 9999.99) |
9,999.990000 |
( |
Use parentheses to include negative numbers |
("% (f",-99.99) |
(99.990000) |
# |
If a floating-point number contains a decimal point, add 0x or 0 if the binary is 16 or 8 |
("% #x", 99) ("% #o", 99) |
0x63 0143 |
< |
Format the parameters described by the previous translator |
("%f and%<3.2f", 99.45) |
99.450000 and 99.45 |
$ |
Index of the parameter being formatted |
("%1$d,%2$s", "the", "ABC") |
99,abc |
3. Date and event string formatting
In the program interface often need to display the time and date, but the format of the display is often unsatisfactory, you need to write a lot of code through various algorithms to get the ideal date and time format. There is also a%tx converter in the string format that is not described in detail and is specifically used to format dates and times. The x in the%TX Converter represents an additional conversion that handles date and time formats, and their combination can format dates and times into multiple formats.
Format for common date and time combinations.
Conversion character |
Description |
Example |
C |
Include all date and time information |
Saturday 14:21:20 CST 2007 |
F |
"Year-month-day" format |
2007-10-27 |
D |
"Month/day/year" format |
10/27/07 |
R |
"HH:MM:SS PM" format (12 o'clock-year) |
02:25:51 pm |
T |
"HH:MM:SS" Format (24 o'clock-year) |
14:28:16 |
R |
"HH:MM" Format (24 o'clock-year) |
14:28 |
Example:
- Date date=new Date ();
- Use of//c
- System.out.printf ("All date and time information:%tc%n", date);
- Use of//f
- System.out.printf ("year-month-day format:%tf%n", date);
- Use of//d
- System.out.printf ("Month/day/year format:%td%n", date);
- Use of//r
- System.out.printf ("HH:MM:SS pm format (12 o'clock):%tr%n", date);
- Use of//t
- System.out.printf ("HH:MM:SS format (24 o'clock):%tt%n", date);
- Use of//r
- System.out.printf ("hh:mm format (24 o'clock):%tr", date);
Results:
- All date and time information: Monday borlnd 10:43:36 CST 2012
- Year-month-day format: 2012-09-10
- Month/day/year format: 09/10/12
- HH:MM:SS pm Format (12 o'clock): 10:43:36 a.m.
- HH:MM:SS format (24 o'clock): 10:43:36
- hh:mm format (24 o'clock): 10:43
Summary of String.Format ()