String.format() Detailed explanation of graphics and text, very well written!

Author: Pan Jiaqi link: https://segmentfault.com/a/1190000019350486

introduction

The format() method of the String class is used to create a formatted string and connect multiple string objects. Familiar with C language should remember the sprintf() method of C language, the two have similarities.

The format() method has two overloaded forms.

Overload

// 使用当前本地区域对象(Locale.getDefault()),制定字符串格式和参数生成格式化的字符串
String String.format(String fmt, Object... args);

// 自定义本地区域对象,制定字符串格式和参数生成格式化的字符串
String String.format(Locale locale, String fmt, Object... args);

Placeholder

The format description can have up to 5 parts (not including the% symbol). The following [] symbols are optional items, so only% and type are necessary. The order of the format description is prescribed and must Specify in this order chapter.

Examples:

When more than one parameter

Add the new parameter to the back, so there will be 3 parameters to call format() instead of two, and in the first parameter, that is, in the format string, there will be two different formatting settings. That is, the combination of two characters starting with %, the second will be applied to the first %, and the third parameter will be used in the second %, that is, the parameters will be applied to the% in order".

int one = 123456789;
double two = 123456.789;
String s = String.format("第一个参数:%,d 第二个参数:%,.2f", one, two);
System.out.println(s);

Conversion

Conversion symbol

Format the string

Example-Format "hello" as "hello" (left justified)

String raw = "hello word";
String str = String.format("|%-15s|", raw);
System.out.println(str);

Format integers

Example-Display -1000 as (1,000)

int num = -1000;String str = String.format("%(,d", num);
System.out.println(str);

Format floating point numbers

double num = 123.456789;
System.out.print(String.format("浮点类型:%.2f %n", num));
System.out.print(String.format("十六进制浮点类型:%a %n", num));
System.out.print(String.format("通用浮点类型:%g ", num));

Format the date and time

  • Date conversion

  • Time conversion

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

Recommended recent hot articles:

1. 6 ways to get IntelliJ IDEA activation code for free!

2. I used Java 8 to write a piece of logic, and my colleagues couldn't understand it directly. You can try it. .

3. Sling Tomcat, Undertow performance is very explosive! !

4. The Chinese open sourced a super easy-to-use Redis client, which is so fragrant! !

5. The latest release of "Java Development Manual (Songshan Edition)", download it quickly!

Feel good, don't forget to like + forward!

Feel good, don’t forget to like + forward!

Finally, pay attention to the WeChat official account of the stack leader: Java technology stack, reply: Welfare, you can get a free copy of the latest Java interview questions I have compiled for 2020. It is really complete (including answers), without any routine.

Guess you like

Origin blog.csdn.net/youanyyou/article/details/108532851