Home >Java >JavaBase >What is the usage of String.format?

What is the usage of String.format?

coldplay.xixi
coldplay.xixiOriginal
2020-10-27 11:30:229084browse

string.format Usage: 1. Use the local language environment for the new string, formulate the string format and parameters to generate a formatted new string; 2. Use the specified language environment, formulate the string format and parameter generation Formatted string.

What is the usage of String.format?

string.format usage:

Two overloading methods of String.format() string regular type formatting

  • format(String format, Object… args) The new string uses the local language environment, specifies the string format and parameters to generate a formatted new string.
  • format(Locale locale, String format, Object… args) Use the specified locale, specify the string format and parameters to generate a formatted string.

The last chestnut used the formatting of character types and integer types. Now I will give examples of commonly used types

##%sString type"Like Please bookmark"##%c%b##%dInteger type (decimal)88%xInteger type (hexadecimal) FFInteger type (octal )##%fFloating point type8.888% aHexadecimal floating point typeFF.35AE9.38 e 5No examples (basically not used)No example (basically not used)% (% special characters %% can display %)##%nLine breakNo examples (basically not used)%txDate and time types (x represents different date and time conversion characters)No examples (basically not used)
String str=null;  
    str=String.format("Hi,%s", "小超");  
    System.out.println(str);  
    str=String.format("Hi,%s %s %s", "小超","是个","大帅哥");            
    System.out.println(str);                           
    System.out.printf("字母c的大写是:%c %n", 'C');  
    System.out.printf("布尔结果是:%b %n", "小超".equal("帅哥"));  
    System.out.printf("100的一半是:%d %n", 100/2);  
    System.out.printf("100的16进制数是:%x %n", 100);  
    System.out.printf("100的8进制数是:%o %n", 100);  
    System.out.printf("50元的书打8.5折扣是:%f 元%n", 50*0.85);  
    System.out.printf("上面价格的16进制数是:%a %n", 50*0.85);  
    System.out.printf("上面价格的指数表示:%e %n", 50*0.85);  
    System.out.printf("上面价格的指数和浮点数结果的长度较短的是:%g %n", 50*0.85);  
    System.out.printf("上面的折扣是%d%% %n", 85);  
    System.out.printf("字母A的散列码是:%h %n", 'A');
Hi,小超 
Hi,小超 是个 大帅哥  
字母c的大写是:C   
布尔的结果是:false   100的一半是:50   100的16进制数是:64   100的8进制数是:144   50元的书打8.5折扣是:42.500000 元  
上面价格的16进制数是:0x1.54p5   
上面价格的指数表示:4.250000e+01   上面价格的指数和浮点数结果的长度较短的是:42.5000   上面的折扣是85%   字母A的散列码是:41
Match conversion characters and implement advanced functions. In the first example, the $
Conversion symbols Detailed description Example
Character type 'm'
Boolean type true
##%o
77
##%e Exponential type
%g General floating point type (the shorter of f and e types)
%h Hash code
%% Percent type
For the convenience of understanding, let’s give an example Output results

flag

ExplanationExampleAdd a sign to a positive or negative number(“% d”,15)(“d”, 99)("% 4d", 99)##,Use "," to group numbers (commonly used to display the amount)("%,f", 9999.99)9,999.990000(Use brackets to include negative numbers(“%(f”, -99.99) (99.990000)#If it is a floating point number, include the decimal point, if it is hexadecimal or octal, add 0x or 0 (“%#x”, 99)(“%#o”, 99)0x63 014399.450000 and 99.45First one In the example, it is mentioned that %tx c
Result
15 0 Add 0 in front of the number (commonly used for encryption)
0099 Spaces Adds the specified number of spaces before the integer
99
##< Format the previous one The parameters described by the conversion operator ("%f and %<3.2f", 99.45)
d,% 2$s”, 99,”abc”) 99,abc

Includes full date and time information

Saturday October 27 14:21:20 CST 2007F"Year-Month-Day" format2007-10-27D"Month/Day/Year" format10/27/07r"HH:MM:SS PM" format ( 12-hour format)02:25:51 PMT"HH:MM:SS" format (24-hour format)14:28:16R"HH:MM" format (24-hour format)14:28Let’s give an example to facilitate understandingOutput resultsRelated learning recommendations:
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
java basic tutorial

The above is the detailed content of What is the usage of String.format?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn