String.format method - Analysis (rpm)

Transfer from https://blog.csdn.net/u010137760/article/details/82869637 

1. The use of a simple code
2. The method calls source
3. Class -Formatter
3.1 optional parameter index
3.2 selectable marker
3.3 optional width
3.4 optional precision
3.5 casts
3.1 Non date / time conversion type
string conversion 3.1.1
3.1.2 character conversion
3.1.3 integer conversion
3.1.4 converting floating point
3.1.5 Boolean value
3.1.6hash value converting
3.1.7 no parameters conversion
3.2 date / time converter
1. the use of a simple code
String.Format ( "%. 2F.", 2.0f);
. 1
. 1
2. the method calls source code
* returns a localized formatted string, using the supplied format and arguments,
the format * return localized string format, and use the parameters
* at the a using the user's default locale.
* using the user's default locale
* <p> If you're formatting a string other than for human
* consumption, you should use the {@code format(Locale, String, Object...)}
* 格式化字符串如果使用其他语言环境,你应该使用format(Locale, String, Object...)
* overload and supply {@code Locale.US}. See
* "<a href="../util/Locale.html#default_locale">Be wary of the default locale</a>".
*
* @param format the format string (see {@link java.util.Formatter#format})
* @param args
* the list of arguments passed to the formatter. If there are
* more arguments than required by {@code format},
* additional arguments are ignored.
* @return the formatted string.
* @throws NullPointerException if {@code format == null}
* @throws java.util.IllegalFormatException
* if the format is invalid.
* @since 1.5
*/
public static String format(String format, Object... args) {
return format(Locale.getDefault(), format, args);
}

public static String format(Locale locale, String format, Object... args) {
if (format == null) {
throw new NullPointerException("format == null");
}
int bufferSize = format.length() + (args == null ? 0 : args.length * 10);
Formatter f = new Formatter(new StringBuilder(bufferSize), locale);
return f.format(format, args).toString();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 is
32
33 is
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
. 19
20 is
21 is
22 is
23 is
24
25
26 is
27
28
29
30
31 is
32
33 is
3. Class -Formatter
on the interpretation of the Formatter class the following information:

Format strings consist of plain text interspersed with format specifiers, such as {@code “name: %s weight: %03dkg\n”}. Being a Java string, the usual Java string literal backslash escapes are of course available.

A plain text format string, where the interspersed format specifier, such as "name:% s weight:% 03dkg \ n" in% s,% 03d. As java string, the backslash character transferred is supported

Format specifiers (such as {@code “%s”} or {@code “%03d”} in the example) start with a {@code %} and describe how to format their corresponding argument. It includes an optional argument index, optional flags, an optional width, an optional precision, and a mandatory conversion type.

Format specifier begins%, describe how to format the appropriate parameters. Format specifier includes an optional parameter index, selectable markers, an optional width, optional precision, cast

3.1 optional parameter index
markers action example of the results
% 2 $ s 2 second argument specifies a display format ( "% 2s, s, % 1s, s", " Bob", "Li") Li, Xiaoming
<multiplexing same parameter format ( "% o% <d % <x", 64); 100 64 40
alternatively 3.2 indicia
marking action example of the results
, split large data format ( "%, d", 1024); 1,234
+ always show a positive, negative sign format ( "% + d,% + 4d", -5, 5); -5, +5
non-negative has a leading space String.format ( "x% d% 5d ", . 4, -4); -4. 4 X
(the parentheses in the negative format ( "% (d,% (d,% (6d", 12, -12, -12); 12, (12), (12 )
- alignment
required width;
- default right alignment, - No. left alignment; the format ( "% - 6DX",. 5);
the format ( "% -. 3C,%. 3C", 'D', 0x65);. 5 X
D, E
0 number with leading zero padding the format ( "% 07D, 03D%",. 4, 5555); 0000004, 5555
# deformed
only octal, hexadecimal; format ( "% o% # o% d", 010, 010,010) ;
format("%x %#x %d", 0x12, 0x12,0x12); 10 010 8
12 0x12 18
3.3可选的宽度
The width is a decimal integer specifying the minimum number of characters to be used to represent the argument.

Width is a decimal integer, the minimum number of characters for display.
Note: Do not use the width of the field to truncate

Results marked effect Examples
5 two spaces in front of the format ( "% 3D", 5); 5
3.4 optional precision
The precision is a followed by a decimal integer {@code.}

Accuracy Is behind with plastic figures

Examples marked effect results
later with several decimal format ( "% 1F.", 123.456f);. 123.5
3.5 of casts
3.1 Non date / time conversion type
3.1.1 string conversion
flag action example of the results
s string format ( " S% S% "," Hello "," the Hello "); Hello the Hello
S uppercase character string the format ("% S% S "," Hello "," the Hello "); HELLO HELLO
3.1.2 character conversion
marked effect results examples
character c the format ( "% c% c", 'D', 'E'); D E
C uppercase the format ( "% C% C", 'D', 'E'); DE
3.1.3 integer conversion
tag examples of action results
d decimal the format ( "% d", 26 is); 26 is
O octal the format ( "% O", 032); 32
X hexadecimal format ( "% x% X" , 0x1a, 0x1a); 1a 1A
3.1.4 floating-point numbers
marked action example of the results
f decimal the format ( "% f", 123.456f);
the format ( "% 1F.", 123.456f);
the format ( "% 1.5F",123.456f);
format("%10f", 123.456f);
the format ( "% 6.0f", 123.456f); 123.456001
123.5
123.45600
123.456001
   123
E, E index float the format ( "% E", 123.456f);
the format ( "% 1E.", 123.456f);
the format ( "% 1.5E ", 123.456f);
the format ("% 10E ", 123.456f);
the format ("% 6.0E ", 123.456f); 1.234560e + 02
1.2E + 02
1.23456E + 02
1.234560E + 02
 1E + 02
g, G or decimal number format depends on the size of the index ( "% G% G", 0.123, .0000123); .123000 1.23000e-05
a, a hex decimal format ( "% a", 123.456f ); 0x1 .edd2f2p6
3.1.5 Boolean value to
support the Boolean value. null is considered to be false, it is true of other types of instances

Tag action example of the results
b, B Boolean the format ( "% B% B", to true, to false);
the format ( "% B% B", to true, to false);
the format ( "% B", null);
the format ( " B% "," Hello "); to false to true
TRUE FALSE
to false
to true
3.1.6hash value conversion
call parameters hashCode method, all types of support

Examples marked effect results
h, H hex the format hashCode ( "% H", the this);
the format ( "% H", the this);
the format ( "% H", null); 190d11
190D11
null
3.1.7 nullary conversion
tag action example of the results
% outputs% characters the format ( "% D %%", 50); 50%
n-wrap the format ( "First% nSecond"); First \ nSecond
.. 3 2 date / time conversion
calendar, a date, milliseconds can be as the date / time parameters. If you type an error, then return 1970-01-01 00:00:00 UTC

Examples of results marked effect
ta day of week (abbreviated) format ( "% ta", CAL, CAL); Tue
tA day of week (full) format ( "% tA", CAL, CAL); catalog on Tuesday
TB month The first few months (abbreviation)) format ( "% TB", CAL); Apr
tB month in the first few months (full) format ( "% tB", CAL); April
tc time, do not use format ( "% tc", CAL); Tue Apr 01 16:19:17 CEST 2008
tC century, two-digit format ( "% tC", CAL); 20
td day of the month, two-digit format ( "% td", cal ); 01
tD US date format, do not use the format ( "% tD", CAL); 04/01/08
TE one day of the month (1-31) format ( "% te ", cal); 1
at tF ISO 8601 standard full date format (the MM-YYYY-DD) the format ( "% tF", CAL); 2008-04-01
TH% synonyms TB
represents the median tH 2 hours, 24-hour (00-23) format ( "% tH ", CAL); 16
tI 2 digits for hours, 12-hour clock (01-12) format ("% tI ", cal);04
TJ three digits indicate the day (001-366) in the year format ( "% TJ", CAL); 092
tk is the hour, 24-hour clock (0-23) format ( "% tk ", cal) ; 16
tl denotes hour, 12-hour clock (1-12) the format ( "% tl", CAL);. 4
tL milliseconds format ( "% tL", cal ); 359
of the months of the year digits tm 2 ( 01-12) format ( "% tm", CAL); 04
tM 2 Wei minutes format ( "% tM", CAL); 08
tN nanoseconds format ( "% tN", CAL); 359 million
TP morning or afternoon format ( "% TP% Tp", CAL, CAL); PM PM
tQ since the times of millisecond value format ( "% tQ", CAL); 1,207,059,412,656
TR complete 12-hour% tI:% tM:% tS % Tp format ( "% TR", CAL); 04:15:32 PM
tR short 24-hour clock format ( "% tR", CAL); 16:15
TS format since the second value from the era ( "% ts", cal) ; 1,207,059,412
tS second two digits (00-60) the format ( "% tS", CAL);. 17
tT complete 24-hour% tH:% tM:% tS format ( "% tT", cal); 16:15:32
ty two-digit years (00-99) format ( "% ty", CAL); 08
tY four-digit years format ( "% tY", cal );2008
TZ time zone GMT offset format ( "% TZ", CAL); +0100
tZ local time zone abbreviated format ( "% tZ", cal ); CEST

---------------------
Author: Los desert O_o
Source: CSDN
Original: https: //blog.csdn.net/u010137760/article/details/82869637
copyright notice : This article is a blogger original article, reproduced, please attach Bowen link!

Guess you like

Origin www.cnblogs.com/xd502djj/p/10943634.html