String.format () detailed usage

String.Format () routine of the character string type two overloaded formatted

  • format (String format, Object ... args) new string using the local language environment, develop new string parameter string format and generate formatted.
  • format (Locale locale, String format, Object ... args) using the specified locale, format string and string to develop parameters to generate formatted.

Last chestnuts helpful to the character type and integer type format below I list them common type

The conversion operation Detailed description Examples
%s String type "Like your Favorites"
%c Character Types ‘m’
%b Boolean true
%d Integer type (decimal) 88
%x Integer type (hexadecimal) FF
%O Integer type (octal) 77
%f Floating-point type 8.888
%a Hexadecimal floating-point type FF.35AE
%e Index type 9.38e + 5
%g General floating point type (f and e of the shorter type) Examples not (substantially less than)
%h Hash code Examples not (substantially less than)
%% Percentage Type % (% %% to display special characters%)
%n Newline Examples not (substantially less than)
%tx The type of date and time (x representing a different date and time conversion operator) Examples not (substantially less than)

To facilitate understanding is an example

String str=null;  
    str=String.format("Hi,%s", "小超");  
    System.out.println(str);  
    STR = String.format ( "the Hi,% S% S% S", "small excess", "a", "big guy" );            
    System.out.println(str);                           
    System.out.printf ( "uppercase letter c is:%% n-c", 'C' );  
    System.out.printf ( "Boolean result:% b% n", "ultra-small" .equal ( "male" ));  
    System.out.printf ( "half 100 are: n-% D%", 100/2 );  
    System.out.printf ( "100 is hexadecimal 16:%% n-X", 100 );  
    System.out.printf ( "100 is a hexadecimal number. 8:%% n-O", 100 );  
    System.out.printf ( "50-membered 8.5 discount is typed as:% f n-membered%", 50 * 0.85 );  
    System.out.printf ( "hexadecimal 16 is above the price:%% n-A", 50 * 0.85 );  
    System.out.printf ( "price index above indicates:%% n-E", 50 * 0.85 );  
    System.out.printf ( "index price and float above results shorter length is:%% n-G", 50 * 0.85 );  
    System.out.printf ( "discount is above %%% n-D%", 85 );  
    System.out.printf ( "hash code letter A is:% h% n", '   A');

 

Output

Hi, ultra-small 
Hi, ultra-small is a big guy  
C uppercase letters are: C   
Boolean result is: to false    
half 100 is: 50    
hexadecimal number is 100: 64    
octal is 100: 144    
$ 50 to play book discount is 8.5: 42.500000 membered  
Hexadecimal number above prices are: 0x1 .54p5   
The above represents the price index: 4.250000e + 01    
index and float above results price is shorter lengths: 42.5000    
above discount 85 %    
hash code letter A is: 41

 

The first example of ### with conversion operator as well as advanced features useful to $

Mark Explanation Examples result
+ Add to positive or negative sign (“%+d”,15) +15
0 Digital leading zero (common encryption) (“%04d”, 99) 0099
Blank Adds the specified number of spaces before integer (“% 4d”, 99) 99
, To "," digital packet (commonly used to display the amount) (“%,f”, 9999.99) 9,999.990000
( Use brackets including negatives (“%(f”, -99.99) (99.990000)
# If the float contains a decimal point, or if it is a hexadecimal or octal 0x is added 0 (“%#x”, 99)(“%#o”, 99) 0x63 0143
< A parameter identifier described in the pre-conversion format ( "% F and% <3.2f", 99.45) 99.450000 and 99.45
d,%2$s”, 99,”abc”) 99,abc    

In the first example there comes% tx x represents the date conversion specifier way I include the date conversion specifier

Mark Explanation Examples
c All information including the date and time Saturday Shiyue 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 (12 hour) 02:25:51 PM
T "HH: MM: SS" format (24-hour format) 14:28:16
R "HH: MM" format (24-hour format) 14:28

 

Example to facilitate comprehension

Date date=new Date();                                  
    //c的使用  
    System.out.printf("全部日期和时间信息:%tc%n",date);          
    //f的使用  
    System.out.printf("年-月-日格式:%tF%n",date);  
    //d的使用  
    System.out.printf("月/日/年格式:%tD%n",date);  
    //r的使用  
    System.out.printf("HH:MM:SS PM格式(12时制):%tr%n",date);  
    //t的使用  
    System.out.printf("HH:MM:SS格式(24时制):%tT%n",date);  
    //R的使用  
    System.out.printf("HH:MM格式(24时制):%tR",date); 

 

输出结果

全部日期和时间信息:星期三 九月 21 22:43:36 CST 2016-月-日格式:2016-09-21/日/年格式:16/10/21  
HH:MM:SS PM格式(12时制):10:43:36 下午  
HH:MM:SS格式(24时制):22:43:36  
HH:MM格式(24时制):22:43 

 

本文转自:https://blog.csdn.net/anita9999/article/details/82346552

Guess you like

Origin www.cnblogs.com/nizuimeiabc1/p/11030191.html