Regular formatting
The format () method of the string class is used to create formatted strings and connect multiple string objects. Readers familiar with the C language should remember the sprintf () method of the C language. The two have similarities. The format () method has two reloads.
L format (string format, object... ARGs)
This method uses the specified string format and parameters to generate a formatted new string. The new string always uses the local language environment. For example, the format of the current date information in the Chinese language environment is "2007-10-27", but there are different forms in other countries.
Syntax:
String. Format (format, argS ...)
Format: string format.
ARGs...: parameters referenced by format specifiers in string format. If there are parameters other than format specifiers, ignore these additional parameters. The number of parameters is variable and can be 0.
L format (locale, string format, object... ARGs)
This method uses the specified language environment, string format, and parameters to generate a formatted new string. The new string always uses the specified language environment.
Syntax:
String. Format (locale, format, argS ...)
Locale: Specifies the language environment.
Format: string format.
ARGs...: parameters referenced by format specifiers in string format. If there are parameters other than format specifiers, ignore these additional parameters. The number of parameters is variable and can be 0.
The string format parameters in the format () method have many conversion character options, such as date, integer, and floating point number. See table 7.1.
Table 7.1 conversion Operator
Conversion character
Description
Example
% S
String type
"Mingrisoft"
% C
Character Type
'M'
% B
Boolean Type
True
% D
INTEGER (decimal)
99
% X
Integer type (hexadecimal)
FF
% O
Integer type (octal)
77
% F
Floating Point Type
99.99
%
Hexadecimal floating point type
Ff.35ae
% E
Exponential type
9.38e + 5
% G
General floating point type (shorter of the F and E types)
% H
Hash code
%
Percentage type
%
% N
Line Break
% TX
Date and Time type (X represents different date and time Conversion characters
Ch0705Instance location:Mr \ 07 \ sl \ 05
The following example uses various Conversion characters in Table 7.1 to convert different data types to strings, and outputs the converted strings to the console using the system. Out. printf () method. The implementation steps are as follows.
(1) create a strconversion class and copy the following code to the class definition.
Routine 06 Code Location: CD \ Mr \ 07 \ sl \ 05 \ SRC \ com \ LZW \ strconversion. Java
Public static void main (string [] ARGs ){
String STR = NULL;
STR = string.Format("Hi, % s", ""); // format the string
System.Out. Println (STR); // output the str content of the string variable.
System.Out. Printf ("uppercase letter A: % C % N", 'A ');
System.Out. Printf ("3> 7: % B % N", 3> 7 );
System.Out. Printf ("half of 100 is: % d % N", 100/2 );
System.Out. Printf ("The hexadecimal number of 100 is % x % N", 100 );
System.Out. Printf ("the octal number of 100 is % o % N", 100 );
System.Out. Printf ("8.5 discount for 50 RMB books: % f RMB % N", 50*0.85 );
System.Out. Printf ("The hexadecimal number of the above price is: % A % N", 50*0.85 );
System.Out. Printf ("the above price index: % E % N", 50*0.85 );
System.Out. Printf ("the result length of the above price index and floating point number is shorter: % G % N", 50*0.85 );
System.Out. Printf ("the discount above is % d % N", 85 );
System.Out. Printf ("the hash code for letter A is % H % N", 'A ');
}
(2) run the strconversion class. The output result in the console is as follows:
Hi, feilong
Uppercase letter A is:
3> 7: false
Half of 100 is: 50
The hexadecimal number of 100 is: 64
The octal number of 100 is: 144
8.5 discount for 50 RMB books: 42.500000 RMB
The hexadecimal number of the above price is: 0x1. 54p5
The above price index indicates: 4.2520.e + 01
The result of the above price index and floating point number is shorter: 42.5000
The above discount is 85%
The hash code for letter A is: 41
These string format parameters not only flexibly convert other data types into strings, but also can be combined with various flags to generate strings of various formats, as shown in Table 7.2.
Table 7.2 is used with a conversion character
Logo
Description
Example
Result
+
Add a sign to a positive or negative number
("% + D", 15)
+ 15
−
Left aligned
("%-5d", 15)
| 15 |
0
Add 0 before the number
("% 04d", 99)
0099
Space
Add a specified number of spaces before the integer
("% 4D", 99)
| 99 |
,
Group numbers ","
("%, F", 9999.99)
9,999.990000
(
Use parentheses to include negative numbers
("% (F",-1, 99.99)
(99.990000)
#
If it is a floating point, it contains the decimal point. If it is a hexadecimal or octal, add 0x or 0.
("% # X", 99)
("% # O", 99)
0x63
0143
<
Format the parameters described by the previous conversion Operator
("% F and % <3.2f", 99.45)
99.450000 and 99.45
$
Formatted parameter index
("% 1 $ D, % 2 $ s", 99, "ABC ")
99, ABC
Ch0706Instance location:Mr \ 07 \ sl \ 06
The following examples use several common conversion character combinations to format strings and output them to the console using the system. Out. printf () method. The implementation steps are as follows.
(1) create a strdatetime class and copy the following code to the class definition.
Location of the Code for example 07: CD \ Mr \ 07 \ sl \ 06 \ SRC \ com \ LZW \ strformat. Java
Public static void main (string [] ARGs ){
String STR = NULL;
STR = string.Format("Use of format parameter $: % 1 $ D, % 2 $ s", 99, "ABC"); // format the string
System.Out. Println (STR); // output string variable
System.Out. Printf ("symbols showing positive and negative numbers: % + D and % d % N", 99,-99 );
System.Out. Printf ("the best number is % 03d % N", 7 );
System.Out. Printf ("the effect of the tab key is % 8d % N", 7 );
System.Out. Printf ("the integer grouping effect is: %, d % N", 9989997 );
System.Out. Printf ("the price of a book is % 2.2f RMB % N", 49.8 );
}
(2) run the strformat class and output the formatted result of the string on the console.
Format parameter $ usage: 99, ABC
Show positive and negative symbols: + 99 and-99
The best number is 007.
The effect of the tab key is: 7
Integer grouping: 9,989,997
The price of a book is: 49.80 yuan
7.4.2 format the date and time strings
The display time and date are often required on the program interface, but the display format is often unsatisfactory. You need to write a lot of code to get the ideal date and time format through various algorithms. The % TX conversion character is not detailed in the string format. It is used to format the date and time. X in the % TX conversion operator represents another conversion operator that processes the date and time formats. Their combination can format the date and time into multiple formats.
1. Common Date and Time formatting
Conversion operators for formatting dates and times define various formatting methods for date strings. The most common combination formats of dates and times are shown in Table 7.3.
Table 7.3 common date and time combination formats
Conversion character
Description
Example
C
Including all date and time information
Week 60 month 27 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 (in 12-hour format)
02:25:51 pm
T
"HH: mm: SS" format (in 24-hour format)
14:28:16
R
"HH: mm" format (in 24-hour format)
14: 28
Ch0707Instance location:Mr \ 07 \ sl \ 07
The following example uses the conversion character in Table 7.3 to format the current date and time, and outputs the data to the console using the system. Out. printf () method. The implementation steps are as follows.
(1) create a strdatetime class and copy the following code to the class definition.
Task 08 Code Location: CD \ Mr \ 07 \ sl \ 07 \ SRC \ com \ LZW \ strdatetime. Java
Public static void main (string [] ARGs ){
Date = new date (); // create date object
System.Out. Printf ("all date and time information: % TC % N", date); // format the output date or time
System.Out. Printf ("year-month-day format: % Tf % N", date );
System.Out. Printf ("month/day/year format: % TD % N", date );
System.Out. Printf ("HH: mm: ss pm format (12-hour format): % tr % N", date );
System.Out. Printf ("HH: mm: SS format (in 24-hour format): % TT % N", date );
System.Out. Printf ("HH: Mm format (in 24-hour format): % TR", date );
}
(2) run the instance and output the current date and time in the local time format on the console. The running result is as follows:
All date and time information: Sunday October 28 13:53:24 CST 2007
Year-month-day format: 2007-10-28
Month/day/year format: 10/28/07
Hh: mm: ss pm format (in 12-hour format): 01: 53: 24 PM
Hh: mm: SS format (in 24-hour format): 13: 53: 24
Hh: Mm format (in 24-hour format): 13: 53
2. format the date string
The conversion operator that defines the date format can generate a new string using the specified conversion token. See Table 7.4.
Table 7.4 date format conversion Operator
Conversion character
Description
Example
B or H
Abbreviated month name
Medium: May October
English: Oct
B
Full name of month
Medium: May October
English: October
A
Abbreviation of a week
Medium: Saturday
English: Sat
A
Full name of the week
Medium: Saturday
English: Saturday
C
The first two digits of the Year (less than two digits are preceded by 0)
20
Y
The last two digits of the Year (less than two digits are preceded by 0)
07
Y
Year with four digits (0 before four digits)
2007
J
The day of the year (that is, the day of the year)
300
M
The month with two digits (less than two digits are preceded by 0)
10
D
Day of two digits (less than two digits are preceded by 0)
27
E
The day of the month (no preceding 0)
5
Ch0708Instance location:Mr \ 07 \ sl \ 08
The following instances will format the date of the current system using various delimiters and output it to the console using the system. Out. printf () method. The implementation steps are as follows.
(1) create a strdate class and copy the following code to the class definition.
Sample 09 Code Location: CD \ Mr \ 07 \ sl \ 08 \ SRC \ com \ LZW \ strdate. Java
Public static void main (string [] ARGs ){
Date = new date (); // create date object
String STR = string.Format(Locale.Us, "Abbreviated month: % TB", date); // format the date string
System.Out. Println (STR); // output string content
System.Out. Printf ("Local month Abbreviation: % TB % N", date );
STR = string.Format(Locale.Us, "Full name of the English month: % TB", date );
System.Out. Println (STR );
System.Out. Printf ("Local month full name: % TB % N", date );
STR = string.Format(Locale.Us, "% Ta", date );
System.Out. Println (STR );
System.Out. Printf ("Local week Abbreviation: % TA % N", date );
System.Out. Printf ("first two digits of the Year (less than two digits are preceded by 0): % TC % N", date );
System.Out. Printf ("the last two digits of the Year (less than two digits are preceded by 0): % ty % N", date );
System.Out. Printf ("days in a year (that is, the day of the year): % TJ % N", date );
System.Out. Printf ("two-digit month (less than two first 0): % TM % N", date );
System.Out. Printf ("two-digit Day (less than two first 0): % TD % N", date );
System.Out. Printf ("day of the month (0 is not added before): % te", date );
}
(2) run the instance and output strings formatted by various dates on the console. The running result is as follows:
Abbreviated month: Oct
Local month Abbreviation: October
October
Full name of local Month: January 1, October
Sun
Abbreviation of local week: Sunday
The first two digits of the Year (0 for less than two digits): 20
The last two digits of the Year (less than two digits are preceded by 0): 07
Days in a year (the day of the year): 301
Month with two digits (0 before less than two digits): 10
Day with two digits (0 before less than two digits): 28
Day of the month (not 0): 28
3. format the time string
Compared with the date format conversion operator, the time format conversion operator is more accurate. It can format the time to a unit of time, minute, second, or even hour millisecond. The conversion character of the formatted time string is shown in Table 7.5.
Table 7.5 time format conversion Operator
Conversion character
Description
Example
H
The hour in the 24-digit format (0 for less than two digits)
15
I
The hour in 12-digit notation (0 for less than two digits)
03
K
The hour in the 24-digit format (the first digit is not 0)
15
L
The hour in 12-digit notation (0 is not added before)
3
M
Minutes of two digits (0 before less than two digits)
03
S
The second of two digits (0 before less than two digits)
09
L
The millisecond value of a three-digit number (0 before 3 digits)
015
N
9-digit millisecond count (0 before 9 digits)
562000000
P
Morning or afternoon Mark of lowercase letters
Moderate: afternoon
English: PM
Z
Offset of the rfc822 Time Zone relative to GMT
+ 0800
Z
Time zone abbreviation string
CST
Continue table
Conversion character
Description
Example
S
The number of seconds that have elapsed since 00:00:00
1193468128
Q
The number of milliseconds that have elapsed since 00:00:00
1193468128984
Ch0709Instance location:Mr \ 07 \ sl \ 09
The following example uses a variety of delimiters to format the time of the current system and outputs it to the console using the system. Out. printf () method. The implementation steps are as follows.
(1) create a strtime class and copy the following code to the class definition.
Routine 10 Code Location: CD \ Mr \ 07 \ sl \ 08 \ SRC \ com \ LZW \ strtime. Java
Public static void main (string [] ARGs ){
Date = new date (); // create date object
System.Out. Printf ("hour in the 24-digit format (0 before the second digit): % th % N", date );
System.Out. Printf ("hour in 12-digit format (0 before 2 digits): % Ti % N", date );
System.Out. Printf ("hour in the 24-digit format (0 is not added before): % TK % N", date );
System.Out. Printf ("2-digit 12-hour (not supplemented with 0): % TL % N", date );
System.Out. Printf ("2-digit minute (0 before 2 digits): % TM % N", date );
System.Out. Printf ("seconds with two digits (0 before less than two digits): % TS % N", date );
System.Out. Printf ("3-digit Millisecond (0 before 3): % TL % N", date );
System.Out. Printf ("9-digit millisecond count (0 before 9 digits): % TN % N", date );
String STR = string.Format(Locale.Us, "Morning or afternoon signs of lowercase letters (English): % TP", date );
System.Out. Println (STR); // output the str content of the string variable.
System.Out. Printf ("morning or afternoon Mark of lowercase letters (medium): % TP % N", date );
System.Out. Printf ("offset of rfc822 Time Zone relative to GMT: % TZ % N", date );
System.Out. Printf ("Time Zone abbreviation string: % TZ % N", date );
System.Out. Printf ("the number of seconds since 00:00:00: % TS % N", date );
System.Out. Printf ("the number of milliseconds that have elapsed since 00:00:00: % TQ % N", date );
}
(2) run the instance and output the following information on the console:
Hour in 24-digit format (0 before 2 digits): 15
Hour in 12-digit format (0 before 2 digits): 03
Hour in the 24-digit format (0 is not added before): 15
Hours in 12-digit notation (0 is not added before): 3
Minute with two digits (0 before less than two digits): 24
Second of two digits (0 before less than two digits): 56
3-digit Millisecond (0 before 3 digits): 828
9-digit millisecond count (0 before 9 digits): 828000000
Morning or afternoon signs of lowercase letters (English): PM
Morning or afternoon signs (middle) of lowercase letters: afternoon
Offset of rfc822 Time Zone relative to GMT: + 0800
Time zone abbreviation string: CST
00:00:00 the number of seconds that have elapsed since now: 1193556296
00:00:00 the number of milliseconds elapsed since now: 1193556296828