(Reprinted from: http://www.cnblogs.com/happyday56/p/3996498.html)
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 '); }
Java placeholder App