Java placeholder application and java placeholder Application
(Reproduced from: http://www.cnblogs.com/happyday56/p/3996498.html)
The format () method of the String class is used to create formatted strings and connect multiple String objects. If you are familiar with C, you should remember the sprintf () method of C. The two have similarities. The format () method has two reloads.
Format (String format, Object... args) The new String uses the local language environment to create the String format and parameters to generate the formatted new String.
Format (Locale locale, String format, Object... args) uses the specified language environment to create String formats and parameters to generate formatted strings.
Display different Conversion characters to convert different data types to strings ,.
|
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 |
|
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 "," Wang Li "," Wang Zhang "); System. out. println (str); 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 ("50 yuan book 8.5 discount: % f yuan % 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 ');}