String.format其实是调用的Formatter.format:
public static String format(String format, Object... args) { return new Formatter().format(format, args).toString(); }
第一个参数是格式化字符串,第二个参数是可变的被格式化的参数。
我们主要看的是格式化参数的说明:
- The format specifiers for general, character, and numeric types have the following syntax:
%[argument_index$][flags][width][.precision]conversion
-
The following table summarizes the supported flags. y means the flag is supported for the indicated argument types.
Flag General Character Integral Floating Point Date/Time Description '-' y y y y y The result will be left-justified. '#' y1 - y3 y - The result should use a conversion-dependent alternate form '+' - - y4 y - The result will always include a sign ' ' - - y4 y - The result will include a leading space for positive values '0' - - y y - The result will be zero-padded ',' - - y2 y5 - The result will include locale-specific grouping separators '(' - - y4 y5 - The result will enclose negative numbers in parentheses 1 Depends on the definition of
Formattable
.2 For
'd'
conversion only.3 For
'o'
,'x'
, and'X'
conversions only.4 For
'd'
,'o'
,'x'
, and'X'
conversions applied toBigInteger
or'd'
applied tobyte
,Byte
,short
,Short
,int
andInteger
,long
, andLong
.5 For
'e'
,'E'
,'f'
,'g'
, and'G'
conversions only.Any characters not explicitly defined as flags are illegal and are reserved for future extensions.
-
Width
The width is the minimum number of characters to be written to the output. For the line separator conversion, width is not applicable; if it is provided, an exception will be thrown.
Precision
For general argument types, the precision is the maximum number of characters to be written to the output.
For the floating-point conversions
'a'
,'A'
,'e'
,'E'
, and'f'
the precision is the number of digits after the radix point. If the conversion is'g'
or'G'
, then the precision is the total number of digits in the resulting magnitude after rounding.For character, integral, and date/time argument types and the percent and line separator conversions, the precision is not applicable; if a precision is provided, an exception will be thrown.
Argument Index
The argument index is a decimal integer indicating the position of the argument in the argument list. The first argument is referenced by "
1$
", the second by "2$
", etc.Another way to reference arguments by position is to use the
'<'
('\u003c') flag, which causes the argument for the previous format specifier to be re-used. For example, the following two statements would produce identical strings:Calendar c = ...; String s1 = String.format("Duke's Birthday: %1$tm %1$te,%1$tY", c); String s2 = String.format("Duke's Birthday: %1$tm %<te,%<tY", c);
Conversion | Argument Category | Description |
---|---|---|
'b' , 'B' |
general | If the argument arg is null , then the result is "false ". Ifarg is a boolean or Boolean , then the result is the string returned by String.valueOf(arg) . Otherwise, the result is "true". |
'h' , 'H' |
general | If the argument arg is null , then the result is "null ". Otherwise, the result is obtained by invokingInteger.toHexString(arg.hashCode()) . |
's' , 'S' |
general | If the argument arg is null , then the result is "null ". Ifarg implements Formattable , then arg.formatTo is invoked. Otherwise, the result is obtained by invokingarg.toString() . |
'c' , 'C' |
character | The result is a Unicode character |
'd' |
integral | The result is formatted as a decimal integer |
'o' |
integral | The result is formatted as an octal integer |
'x' , 'X' |
integral | The result is formatted as a hexadecimal integer |
'e' , 'E' |
floating point | The result is formatted as a decimal number in computerized scientific notation |
'f' |
floating point | The result is formatted as a decimal number |
'g' , 'G' |
floating point | The result is formatted using computerized scientific notation or decimal format, depending on the precision and the value after rounding. |
'a' , 'A' |
floating point | The result is formatted as a hexadecimal floating-point number with a significand and an exponent. This conversion isnot supported for the BigDecimal type despite the latter's being in thefloating point argument category. |
't' , 'T' |
date/time | Prefix for date and time conversion characters. See Date/Time Conversions. |
'%' |
percent | The result is a literal '%' ('\u0025') |
'n' |
line separator | The result is the platform-specific line separator |
示例代码:
package main; public class StringFormat { public static void main(String[] args) { // TODO Auto-generated method stub String format = "Haha.%1$(g%2$s%3$s"; Object obj = 100f, object1 = "fdsafds", object2 = new StringFormat(); String format2 = String.format(format, obj, object1, object2); System.out.println(format2); // String Format總結 // %[argument_index$][flags][width][.precision]conversion 标准表达式 } }
运行结果:
Haha.100.000fdsafdsmain.StringFormat@6d6f6e28