In jdk1.5, the string class adds a very useful static function Format (string format, objece... argues), which can format various types of data into strings and output them. The format parameter specifies the output format, which is the most complex and difficult to grasp, while argues is a series of objects waiting for formatting. This function imitates the usage of the printf function in C language, so it is much easier to learn about it based on C language. Next we will focus on the format and meaning of the format parameter.
The format parameter can contain strings that do not need to be converted. What are the strings you write and what is output in the end. It also contains some special formats to specify which object to convert and what form to convert. This special format starts with % index $. The index value starts from 1, indicating that the index parameter is taken in for formatting. This is better than the C language. The C language can only be formatted in order of parameters, while Java can select the nth parameter for formatting. Since this function can format any object, different objects have different parameters, so we will discuss them in the following categories.
1. format the integer: % [index $] [ID] [minimum width] Conversion Method
We can see that the formatted string consists of four parts. The meaning of % [index $] is described above, and the meaning of [minimum width] is also well understood, the minimum number of digits in the string to be converted. Let's take a look at the meanings of the remaining two parts:
Identifier:
'-' Is left aligned within the minimum width. It cannot be used together with "fill with 0 ".
'#' Is only applicable to octal and hexadecimal. In octal, a hexadecimal value is added before the result and 0x is added before the result.
The '+' result always contains a symbol (generally, it is only applicable to hexadecimal notation. if the object is BigInteger, it can be used in hexadecimal notation and hexadecimal notation)
''Plus space before a positive value and negative value plus a negative number (generally only applicable to the 10-digit system. If the object is BigInteger, it can be used in the 8-and 16-digit systems)
The '0' result is filled with zero.
',' Is only applicable to the 10-digit system. Each three digits are separated by commas (,).
'(If the parameter is a negative number, enclose The number in parentheses instead of adding a negative number in the result (the same as' +)
Conversion Method:
The description above the d-decimal o-octal x or X-hexadecimal is too boring. Let's look at some specific examples. Note that most of the logo characters can be used at the same time. System. out. println (String. format ("% 1 $, 09d",-3123 ));
System. out. println (String. format ("% 1 $9d",-31 ));
System. out. println (String. format ("% 1 $-9d",-31 ));
System. out. println (String. format ("% 1 $(9d",-31 ));
System. Out. println (string. Format ("% 1 $ # 9x", 5689 ));
// The result is:
//-0003,123
//-31
//-31
// (31)
// 0x1639
2. format the floating point: % [index $] [ID] [minimum width] [. Precision] Conversion Method
We can see that the floating point conversion has an "precision" option, which can control the number of digits after the decimal point. Identifier:
'-' Is left aligned within the minimum width. It cannot be used together with "fill with 0 ".
The '+' result always contains a symbol.
''Plus spaces before positive values and minus signs before negative values
The '0' result is filled with zero.
',' Each three digits are separated by commas (,) (only applicable to fgg conversions)
'(If the parameter is a negative number, enclose The number in parentheses instead of adding a negative number in the result (only applicable to the conversion of eefgg)
Conversion Method:
'E', 'E' -- the result is formatted as a decimal number in the computer science notation.
'F' -- the result is formatted as a normal decimal representation.
'G', 'G' -- automatically selects the normal expression or scientific notation method based on the actual situation.
'A', 'A' -- the result is formatted as a hexadecimal floating point with valid digits and indexes.
3. format the characters:
It is very easy to format characters. C indicates the characters, and '-' indicates the left alignment in the logo. Nothing else.
4. format the percent sign:
After reading the preceding instructions, you will find that the percent sign "%" is a prefix in a special format. What should we do if we enter a percent sign? It must be escaped, but note that the escape character here is not "/", but "% ". In other words, the following statement can output "12% ":
System. Out. println (string. Format ("% 1 $ d %", 12 ));
5. Obtain the Independent Line Separator of the Platform:
System. getproperty ("line. separator") can be used to obtain independent row delimiters on the platform, but it is too cumbersome to use them in the format. Therefore, the format function comes with a platform-independent line separator, Which is string. Format ("% N ").
6. format the date type:
The following Date and Time conversion suffixes are defined for 'T' and 'T' conversion. These types are similar to but not exactly equivalent to those defined by GNU date and POSIX strftime (3C. Other conversion types are provided to access Java-specific functions (for example, using 'l' as the millisecond in seconds ).
The following Conversion characters are used to format the time:
The hour in the 'H' 24-hour format is formatted as two digits with leading zeros (00-23) if necessary.
The hour in the 'I' 12-hour format is formatted as the two digits with the leading zero when necessary, that is, 01-12.
The hour in the 'K' 24-hour format, that is, 0-23.
Hours in the 'l' 12-hour format, that is, 1-12.
Minutes in the 'M' hour are formatted as two digits with leading zeros (00-59) if necessary.
Seconds in 's' minutes are formatted as two digits with leading zeros when necessary, that is, 00-60 ("60" is a special value required to support leap seconds ).
The millisecond in 'l' seconds is formatted as a three-digit number with leading zero when necessary, that is, 000-999.
The millisecond In 'n' seconds is formatted as the nine-digit number with the leading zero when necessary, that is, 000000000-999999999.
'P' indicates the language environment in lowercase in the morning or afternoon, for example, "am" or "PM ". You can use the conversion prefix 'T' to forcibly convert the output to an upper-case format.
The offset of 'Z' to the digital time zone in RFC 822 format of GMT, for example,-0800.
'Z' indicates a string in the abbreviated form of time zone. The Formatter language environment replaces the parameter language environment (if any ).
The number of seconds since the Coordinated Universal Time (UTC) on January 1, 1970 00:00:00 to the present, that is, the difference between Long. MIN_VALUE/1000 and Long. MAX_VALUE/1000.
'Q' indicates the number of milliseconds that have elapsed since 00:00:00 on January 1, January 1, 1970 in Coordinated Universal Time (UTC), that is, the difference between Long. MIN_VALUE and Long. MAX_VALUE.
The following Conversion characters are used to format the date:
'B' is the full name of the month specific to the language environment, such as "January" and "February ".
'B' indicates the abbreviation of the month specific to the language environment, such as "Jan" and "Feb ".
'H' is the same as 'B.
'A is the full name of the day of the week specific to the language environment, such as "Sunday" and "Monday"
'A refers to the day of the week (such as "Sun" and "Mon") specific to the language environment"
'C' is divided by four digits of 100 to indicate the year. It is formatted as two digits with leading zeros (00-99) if necessary.
'Y' year, formatted as a four-digit (at least) with a leading zero when necessary, for example, 0092 equals 92 CE of the Gregorian calendar.
The last two digits of the 'y' year are formatted as the two digits with the leading zero when necessary, that is, 00-99.
The number of days in a year of 'J' is formatted as a three-digit number with a leading zero when necessary. For example, for a Gregorian calendar, the value is 001-366.
The 'M' month is formatted as a two-digit with a leading zero when necessary, that is, 01-13.
'D' the number of days in a month, which is formatted as the first zero or two digits (01-31) if necessary.
The number of days in a month is formatted as two digits, that is, 1-31.
The following Conversion characters are used to format common date/time combinations.
'R' 24-hour time, formatted as "% TH: % TM"
The time in the 'T' 24-hour format is formatted as "% TH: % TM: % ts ".
'R' 12-hour time, formatted as "% Ti: % TM: % TS % TP ". The location marked in the morning or afternoon ('% TP') may be related to the language environment.
'D' date, formatted as "% TM/% TD/% ty ".
'F' complete date in ISO 8601 format, formatted as "% ty-% TM-% TD ".
'C' date and time, formatted as "% ta % tb % td % tT % tZ % tY", for example, "Sun Jul 20 16:17:00 EDT 1969 ".
This article from the CSDN blog, reproduced please indicate the source: http://blog.csdn.net/andycpp/archive/2007/08/18/1749700.aspx