Java Magic Hall: String.Format detailed
Directory
First, preface
Second, heavy-duty method
Third, placeholder
Iv. formatting characters and strings
Five, the integer is formatted
Vi. formatting floating-point numbers
Vii. formatting dates and times
Viii. Other Conversion characters
Ix. Summary
Reference
First, preface
String.Format as a text processing tool provides us with a powerful and rich string formatting function, in order not to stop at simply calling String.Format ("Hello%s", "John");
, keep your notes organized and documented below.
Second, heavy-duty method
Formats a string using the current local zone object (Locale.getdefault ()) String.Format (String Fmt, Object ... args);//Custom Local area object format string String.Format (locale locale, String fmt, Object ... args);
Third, placeholder
The full format of the placeholder is:%[index$][identifies the]*[minimum width [. precision] Conversion character.
For formatting of different data types, the placeholder format will be cropped.
%, the real character of the placeholder, to use% within the placeholder, you need to write a percent.
[index$], the position index is calculated starting from 1, which specifies that the corresponding argument of the index is formatted and replaced by the placeholder.
[Identity], which is used to enhance formatting capabilities and can use multiple [identities] at the same time, but some identities cannot be used at the same time.
[Minimum width], used to set the minimum length of the formatted string, if using [minimum width] without setting [identity], then when the length of the string is less than the minimum width, the left fill the space to the minimum width.
[. Precision], for floating-point type formatting use, set how many digits are retained after the decimal point.
A conversion character that specifies the formatted style, and restricts the data type that corresponds to the entry parameter.
Iv. formatting characters and strings
PLACEHOLDER format:%[index$][identifier [minimum width] Translator
Example-format "Hello" as "Hello"
String raw = "Hello"; String str = String.Format ("%1$7s", raw);//simplified//string str = String.Format ("%7s", raw);
Example-format "Hello" as "Hello"
String raw = "Hello"; String str = String.Format ("%1$-7s", raw);//simplified//string str = String.Format ("%-7s", raw);
Available Identities:
--left-aligned in the minimum width, with blanks on the right.
Available Converters:
S, String type.
C, the character type, the argument must be char or int, short, etc. can be converted to a char type data type, otherwise throw illegalformatconversionexception exception.
B, Boolean type, as long as the argument is a Boolean type other than False, all are formatted as String true, otherwise the string is false.
N, platform-independent newline character (as with System.getproperty ("Line.separator")
Five, the integer is formatted
Placeholder format:%[index$][identity]*[min Width] Translator
Example--Display 1 as 0001
int num = 1; String str = String.Format ("%04d", num)
Example--Display 1000 as (1,000)
int num =-1000; String str = String.Format ("% (, d", num)
Available Identities:
-left-aligned within the minimum width and cannot be used with the 0 ID. 0, if the content length is less than the minimum width, the left is filled with zeros. #, add 0x to 8 binary and 16 binary, 8 before adding a 0,16 before the binary. +, the result always contains a + or-number. Spaces, plus spaces before positive numbers, plus-numbers before a negative number. , separated by only the decimal, with each 3 digits. (If the result is negative, enclose it in parentheses and do not display the symbol.)
Available Converters:
B, Boolean type, as long as the argument is a Boolean type other than False, all are formatted as String true, otherwise the string is false. D, Integer type (decimal). X, integer type (hexadecimal). O, Integer type (octal) n, platform-independent line break, or System.getproperty ("Line.separator") to get
Vi. formatting floating-point numbers
Placeholder format:%[index$][identity]*[min width [. precision] Conversion
Example:
Double num = 123.4567899; System.out.print (String.Format ("%f%n", num)); 123.456790 System.out.print (String.Format ("%a%n", num)); 0X1.EDD3C0BB46929P6 System.out.print (String.Format ("%g%n", num)); 123.457
Available Identities:
-left-aligned within the minimum width and cannot be used with the 0 ID. 0, if the content length is less than the minimum width, the left is filled with zeros. #, add 0x to 8 binary and 16 binary, 8 before adding a 0,16 before the binary. +, the result always contains a + or-number. Spaces, plus spaces before positive numbers, plus-numbers before a negative number. , separated by only the decimal, with each 3 digits. (If the result is negative, enclose it in parentheses and do not display the symbol.)
Available Converters:
B, Boolean type, as long as the argument is a Boolean type other than False, all are formatted as String true, otherwise the string is false. N, platform-independent newline characters can also be obtained through System.getproperty ("Line.separator"). F, floating-point type (decimal). A 9-digit number is displayed and rounded. such as 99.99. A, floating-point type (hex). E, index type. such as 9.38e+5. G, floating point type (shorter than%f,%a length, showing 6 digits, and rounding)
Vii. formatting dates and times
Placeholder format:%[index$]t conversion character
Example:
Date now = new Date (); String str = String.Format ("%TF", now); 2014-10-12
Available conversion characters
1. Conversion of dates
C, week Liuxing 14:21:20 CST 2007f,2007-10-27d,10/27/07r,02:25:51 pm t,14:28:16r,14:28b, month abbreviation B, month full name A, week abbreviation A, week full name C, two years ago (less than two people Complement 0) Y, two digits after the year (less than two digits of 0) J, the Day of the Year m, month (less than two bit 0) d, date (less than two bit 0) E, date (less than two digits do not fill 0)
2. Conversion of Time
H, 24-hour hour (less than two-bit 0) K, 24-hour hour (less than two not 0) I, 12-hour hour (less than two-bit 0) I, 12-hour hour (less than two digits not 0) M, minutes (less than two bits 0) s, seconds (less than two bits 0) L, milliseconds (less than three bit 0) N , milliseconds (less than 9 bit 0) p, lowercase letter of the morning or afternoon mark, such as Chinese as "Afternoon", English pmz, time zone offset relative to GMT, such as +0800z, time zone abbreviation, such as CSTs, number of seconds since 1970-1-1 00:00:00 Q, since 1970-1-1 Minutes from 00:00:00.
Viii. Other Conversion characters
<, which is used to format the parameters described by the previous translator.
Example:
int num = 1000; String str = String.Format ("%d%<,d", num);//result "1000 1,000
Ix. Summary
Write here for the time being, and use the other additions later!
Respect the original, reprint please indicate from: http://www.cnblogs.com/fsjohnhuang/p/4094777.html ^_^ Fat Boy John
Reference
http://blog.csdn.net/lonely_fireworks/article/details/7962171
Http://www.cnblogs.com/xytop/articles/1277125.html
Java Magic Hall: String.Format detailed (reprint)