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.
Displays different conversion characters that 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 |
|
Test Cases
public static void Main (string[] args) { String str=null; Str=string.format ("hi,%s", "Wang Li"); System.out.println (str); Str=string.format ("hi,%s:%s.%s", "Wang Nan", "Li Li", "Wang Zhang"); System.out.println (str); System.out.printf ("Uppercase of the letter A is:%c%n", ' a '); System.out.printf ("3>7 results are:%b%n", 3>7); System.out.printf ("Half of 100 is:%d%n", 100/2); System.out.printf ("100 of 16 binary numbers are:%x%n", +); System.out.printf ("100 of 8 binary numbers are:%o%n", +); System.out.printf ("50 yuan book 8.5 discount is:%f Yuan%n", 50*0.85); System.out.printf ("16 of the above price is:%a%n", 50*0.85); System.out.printf ("The index of the above price indicates:%e%n", 50*0.85); System.out.printf ("The index of the above price and the length of the floating point result is shorter:%g%n", 50*0.85); System.out.printf ("The above discount is%d%%%n",); System.out.printf ("The hash code for the letter A is:%h%n", ' A ');}
Output results
With the symbol of the conversion character.
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 |
Test Cases
public static void Main (string[] args) { String str=null; $ using str=string.format ("format parameter $ use:%1$d,%2$s", "the", "the" ABC "); System.out.println (str); + Use System.out.printf ("Show positive negative sign:%+d and%d%n", 99,-99); Fill o use System.out.printf ("The most bull's number is:%03d%n", 7); Spaces using System.out.printf ("tab effect is:% 8d%n", 7); . Use System.out.printf ("Integer Grouping effect is:%,d%n", 9989997); Spaces and the number of digits after the decimal System.out.printf ("The price of a book is:% 50.5f%n", 49.8);}
Output results
Format parameter $ used: 99,ABC displays positive negative numbers for symbols: +99 and 99 the most bull's number is: the effect of the 007Tab key is: 7 integer Grouping effect is: 9,989,997 The price of a book is: 49.80000 yuan
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 |
Test Cases
public static void Main (string[] args) { 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); The 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);}
Output results
All date and time information: Monday borlnd 10:43:36 CST 2012-month-day format: 2012-09-10 month/day/year format: 09/10/12HH:MM:SS pm Format (12 o'clock): 10:43:36 morning hh:mm: SS Format (24 o'clock System): 10:43:36hh:mm format (24 o'clock): 10:43
A conversion character that defines a date format enables a date to generate a new string from the specified conversion character. These date conversions.
public static void Main (string[] args) {date date=new date (); The use of B, the month is referred to as String Str=string.format (locale.us, "English month Abbreviation:%TB", date); System.out.println (str); System.out.printf ("Local month abbreviation:%tb%n", date); The use of B, full month full Str=string.format (locale.us, "English month Full name:%TB", date); System.out.println (str); System.out.printf ("Local Month full name:%tb%n", date); The use of a, week abbreviation Str=string.format (locale.us, "English week Abbreviation:%ta", date); System.out.println (str); The use of a, week full name System.out.printf ("Local week abbreviation:%ta%n", date); The use of C, the year before the two-bit System.out.printf ("the first two digits of the year (less than two bits in front of 0):%tc%n", date); The use of Y, the year after the two-bit System.out.printf ("the following two digits of the year (less than two bits in front of 0):%ty%n", date); The use of J, the number of days of the Year System.out.printf ("The number of days in a year" (i.e. the day of the year):%tj%n ", date); The use of M, month System.out.printf ("Two digits of the month (less than two bits before 0):%tm%n", date); The use of D, Day (two bit, not enough 0) System.out.printf ("Two digits of the day (less than two bits before 0):%td%n", date); Use of E, day (one does not fill 0) System.out.priNTF ("Day of the month (not before 0):%te", date);}
Output results
English month Abbreviation: Sep local month abbreviation: September full name: September Local Month full name: September English week abbreviation: Mon local week abbreviation: The first two digits of Monday (less than two bits before 0): 20 two digits (less than two digits before 0): The number of days in 121 years (the day of the year): 2,542-digit month (less than two bits preceded by 0): 92-digit day (less than two bits in front of 0): October of the day (not before 0): 10
The conversion of the time format is more and more accurate than the date format conversion character. It can format time into units of hours, minutes, seconds, or even milliseconds. The conversion character that formats the time string.
Conversion character |
Description |
Example |
H |
2-digit 24 o'clock hour (less than 2 bits in front of 0) |
15 |
I |
2-digit 12 o'clock hour (less than 2 bits in front of 0) |
03 |
K |
2-digit 24 o'clock hour (not 0 in front) |
15 |
L |
2-digit 12 o'clock hour (not 0 in front) |
3 |
M |
2-digit minute (less than 2 bits in front of 0) |
03 |
S |
2-digit seconds (less than 2 bits in front of 0) |
09 |
L |
3-digit millisecond (less than 3 bits preceded by 0) |
015 |
N |
9 digits in milliseconds (less than 9 bits in front of 0) |
562000000 |
P |
Small letter of the morning or afternoon mark |
Medium: PM English: PM |
Z |
Offset of the RFC822 time zone relative to GMT |
+0800 |
Z |
Time zone abbreviation string |
Cst |
S |
1970-1-1 00:00:00 to now the number of seconds elapsed |
1193468128 |
Q |
1970-1-1 00:00:00 to present the number of milliseconds passed |
1193468128984 |
Test code
public static void Main (string[] args) {Date date = new Date ();//h using System.out.printf ("2-digit 24 o'clock hour (less than 2 bits in front of 0):%th%n", date);//i use System.out.printf ("2-digit 12 o'clock hour (less than 2 bits in front of 0):%ti%n", date),//k use System.out.printf ("2-digit 24 o'clock hours (not preceding 0): %tk%n ", date),//l use System.out.printf (" 2-digit 12 o'clock hour (front not 0):%tl%n ", date),//m use System.out.printf (" 2-digit minutes (less than 2 bits in front of 0):%tm%n ", date),//s using System.out.printf (" 2-digit seconds (less than 2 bits before 0):%ts%n ", date),//l use System.out.printf ( "3-digit milliseconds (less than 3 bits in front of 0):%tl%n", date),//n using System.out.printf ("9 digits in milliseconds (less than 9 bits before 0):%tn%n", date),//p using string str = String.Format (locale.us, "Small Letter of the morning or afternoon mark (English):%TP", date); System.out.println (str); System.out.printf ("Small Letter morning or afternoon mark (middle):%tp%n", date),//z using System.out.printf ("offset from RFC822 time zone relative to GMT:%tz%n", date);// Use of the System.out.printf ("time zone abbreviation string:%tz%n", date),//s using System.out.printf ("1970-1-1 00:00:00 to present the number of seconds elapsed:%ts%n", date);// Q Using System.out.printf ("1970-1-1 00:00:00 to present the number of milliseconds elapsed:%tq%n", date);}
Output results
2-digit 24 o'clock hours (less than 2 bits in front of 0): 112-digit 12 o'clock hour (less than 2 digits before 0): 112-digit 24 o'clock-hour (without 0): 112-digit 12 o'clock hour (front not 0): 112-digit minute (less than 2 digits before 0): 0 32-digit seconds (less than 2 bits in front of 0): 523-digit milliseconds (less than 3 bits before 0): 7,739-digit milliseconds (less than 9 digits before 0): 773000000 Small letter morning or afternoon mark (English) : AM or PM mark in am lowercase (medium): Morning offset from RFC822 time zone relative to GMT: + 0800 time zone abbreviation string: Cst1970-1-1 00:00:00 to the present number of seconds: 13472462321970-1-1 00:00:00 The number of milliseconds that have elapsed to now: 1347246232773
Java string Formatting-string.format ()