Token % 1 $ s for similar issues
% 1 $ s indicates that the first replacement position is placed as a string in the System. out. println (String. format ("% 1 $ s Hello % 2 $ s", "XX ","! "). The output is" Hello !" String. the first parameter after format is an expression. You can add N values to be added later. For more information, see import java. math. bigDecimal;/*** 1.% [argument_index $] [flags] [width] [. precision] conversion ** String. format ("% 1 $ s", 1234,5678); point to the first parameter to convert to String * String. format ("% 1 $ s % 2 $ s", 1234,5678); format the two parameters as strings and connect them together * String. format ("% s", 1234,5678); point to the first parameter to convert to String * String. format ("% s % 06d", 1234,5678); format the first file as "1234" and format the second file as "005678 w */public class StringFormat {/*** to process float The application scope of point data is float, Float, double, Double, and BigDecimal * % [argument_index $] [flags] [width] [. precision] conversion * % [index $] [identifier] [minimum width] [Reserved accuracy] conversion Method * identifier: * '-' is left aligned within the minimum width, * '+' cannot be used together with "fill with 0". The result always includes a symbol * ''with a space before the positive value, the result of adding a negative number * '0' before a negative value will be filled with zero * '. 'Each three digits are separated by commas (,) (only applicable to fgG conversion) * '(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 eEfgG conversion) * minimum width: the minimum number of digits * Reserved precision: number after the decimal point * Conversion Method: * 'E ', 'E' -- the result is formatted as a decimal number expressed in the computer science notation * 'F' -- the result is formatted as 'g' or 'G' in decimal notation. -- based on the actual situation, the system automatically selects the common representation or scientific notation * 'A ', 'A' -- the result is formatted as A hexadecimal floating point number with valid digits and indexes **/public static void formatFloat () {System. out. println (String. format ("% 1 $ e", 1234567890.123456789); // converts to scientific notation to indicate System. out. println (String. format ("% 1 $ 020e", 1234567890.123456789); // converts it to scientific notation, with a length of 20 and fills the System with 0. out. println (String. format ("% 1 $ g", 12345.123456789); // use the converter e or fSystem to identify the brakes based on the results. out. pri Ntln (String. format ("% 1 $ a", 12345.123456789); // converts it to a hexadecimal floating point number System. out. println (String. format ("% 1 $, f", 1234567890.123456789); // The default decimal places are retained in the conversion result. The three digits are separated by commas (,) and are converted to decimal digits to indicate System. out. println (String. format ("% 1 $, f", 1234567890.123456789); // The default decimal places are retained for the conversion result. The three digits are used to separate System. out. println (String. format ("% 1 $. 10f ", 1234567890.123456789); // The conversion result is to retain the 10-bit precision. convert to the decimal format: System. out. println (String. format ("% 1 $ ,. 100f ", new BigDecimal (" 12345" 678909.1234567890123456789 "); // The conversion result is to retain the accuracy of 100 bits, no loss of precision, the entire number of three is used, separated by the System. out. println (String. format ("% 1 $ ,. 5f ", 1234567890.123456789); // The Conversion Result is retained with 5 decimal places. The value is used for three digits, separated}/*** Processing Integer Data Application Range: byte, Byte, short, Short, int, Integer, long, Long, And BigInteger *** % [argument_index $] [flags] [width] conversion * % [index $] [identifier] [minimum width] conversion Method * identifier: * '-' is left aligned within the minimum width. It cannot be used with "fill with 0" at the same time. * '#' is only applicable to octal and hexadecimal formats, add a hexadecimal value before the result in octal format and add 0x * '+' before the result. A symbol (generally only applicable to the decimal system, if the object is BigInteger, it can be used for both the octal and hexadecimal Systems) * ''is preceded by a space, add a negative number before a negative value (generally only applicable to the decimal system. If the object is BigInteger, it can be used in the octal and hexadecimal formats) * The '0' result will be filled with zero * '. 'Only applies to the 10-digit system. Each three digits are separated by commas (,) *' (if the parameter is a negative number, instead of adding a negative sign to the result, enclose the number with parentheses (the same as '+') * minimum width: the minimum number of digits in the string to be converted by this integer * Conversion Method: d-decimal o-octal x or X-hexadecimal */public static void formatNumber () {System. out. println (String. format ("% 1 $ d",-31); // format it into a numeric value. The result is "-31" System. out. println (String. format ("% 1 $ o",-31); // format the value to indicate that octal is used. The result is "37777777741" System. out. println (String. format ("% 1 $ 19d",-31); // format it into a numeric value to indicate that it is in decimal format. The total length is 19 digits and the result is "-31" System. out. println (String. format ("% 1 $-19d",-31); // format it into a numeric value to indicate that it is in decimal format. The total length is 19 digits, and the result is rounded to the left. "-31" System. out. println (String. format ("% 1 $ 09d",-31); // format it into a numeric value, in decimal format. The result is "-00000031" System. out. println (String. format ("% 1 $, 9d",-3123); // each 3 digits are separated by commas (,). The total length is 9 digits. The result is "-3,123" System. out. println (String. f Ormat ("% 1 $, 09d",-3123); // use commas (,) to separate three digits. Use 0 to fill 9 digits in length. The result is "-0003,123" System. out. println (String. format ("% 1 $ (9d",-3123); // this parameter is used for every three digits. The total length is 9 digits with 0. The result is "(3123)" System. out. println (String. format ("% 1 $ 9d",-31);}/*** process character data * it is very easy to format characters, c C Represents characters, '-' in the identifier indicates the left alignment, and nothing else */public static void formatChar () {System. out. println (String. format ("% 1 $ c", 97); // convert to the character System. out. println (String. format ("% 1 $ 10c", 'hour'); // converts the string to a character, with ten Sys Tem. out. println (String. format ("% 1 $-10c", 'hong'); // convert to character, ten digits, left}/*** format percentage. % special characters. The escape format is % instead of \ */public static void formatBaiFenBi () {System. out. println (String. format ("% 1 $ f %", 12.123456); System. out. println (String. format ("% 1 $. 4f % ", 12.123456); // returns four decimal places, 4 homes 5 into BigDecimal a = new BigDecimal (" 12.12 "), B = new BigDecimal ("13.13"); BigDecimal c =. divide (B, 28, BigDecimal. ROUND_HALF_UP); // retain the 28-digit decimal number System. out. println (c + ""); System. out. println (String. format ("% 1 $. 28f ", c); // format: keep 28 decimal places }/*** Obtain the independent platform line separator */public static void getSeparator () {System. out. println (String. format ("% n"); System. out. println (System. getProperty ("line. separator ");}/*** format the date (available range: long, Long, Calendar, java. util. date) * % [index $] [identifier] [minimum width] Conversion Mode * identifier: * The prefix t or T * of the Date and time Conversion characters. Conversion Method: * format the date conversion character * 'B' is the full name of the month specific to the language environment, for example, "January" and "February ". * 'B' refers to the abbreviated 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, for example, "Sunday" and "Monday" * 'a is the abbreviation of the day of the week specific to the language environment, for example, "Sun" and "Mon" * 'C' are divided by the four-digit year of 100, which is formatted as two digits with leading zeros if necessary, that is, 00-99 * 'y' year, which is 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 two digits with leading zeros (00-99) if necessary. * The number of days in a year in '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' indicates the number of days in a month, which is formatted as the number of days in a month with the leading zero and double digits (01-31 * 'E') if necessary. The number of days in a month is formatted as two digits, that is, 1-31. ** Format the time character * 'H' in the 24-hour format. If necessary, it is formatted with two digits leading to zero, that is, 00-23. * The hour in the 'I' 12-hour format is formatted as a two-digit number with the leading zero when necessary, that is, 01-12. * The hour in the 'K' 24-hour format, that is, 0-23. * The hour 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 value in 'l' seconds is formatted as the number of three digits with the leading zero when necessary, that is, 000-999. * In 'n' seconds, it is formatted as a nine-digit number with a 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 abbreviated to the time zone. The Formatter language environment replaces the parameter language environment (if any ). * The number of seconds between the Coordinated Universal Time (UTC) January 1, 1970 00:00:00 and now, 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, January 1, January 1, 1970, Coordinated Universal Time (UTC), that is, Long. MIN_VALUE and Long. difference between MAX_VALUE * format the time combination character * 'R' in the 24-hour format, formatted as "% tH: % tM" * 't'24-hour time, formatted as "% tH: % tM: % tS ". * The 'r' 12-hour time is 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 ". * Complete date in 'f' ISO 8601 format, formatted as "% tY-% tm-% td ". * 'C' date and time are formatted as "% ta % tb % td % tT % tZ % tY", for example, "Sun Jul 20 16:17:00 EDT 1969 ". **/Public static void formatDate () {long c = System. currentTimeMillis (); System. out. println (String. format ("% 1 $ tY-% 1 $ tm-% 1 $ td % 1 $ tH: % 1 $ tM: % 1 $ tS", c); System. out. println (String. format ("% 1 $ ty-% 1 $ tm-% 1 $ td % 1 $ tI: % 1 $ tM: % 1 $ tS % 1 $ tp % 1 $ tb % 1 $ ta ", c); System. out. println (String. format ("% 1 $ tF % 1 $ tT", 1244943575031l ));} /* the conversion can be applied to any parameter type * 'B or B' \ u0062 '. "true" or "false" is generated. If the parameter is null, the result is "false ". If the parameter is a boolean value or Boolean, the result is a String returned by String. valueOf. Otherwise, the result is "true ". * 'H or h'' \ u0068 'generates a string that represents the hash code value of the object. If the arg parameter is null, the result is "null ". Otherwise, the result is obtained by calling Integer. toHexString (arg. hashCode. *'S or s' \ u0073 'generates a string. If the parameter is null, the result is "null ". If Formattable is implemented, the formatTo method is called. Otherwise, the result is the result obtained by calling the toString () method of the parameter. * **/Public static void formatAny () {System. out. println (String. format ("% B", null, "", "true", true )); string pattern = "% 1 $ s in % 4 $ tF % 4 $ tT said \" % 1 $ s love % 2 $ s % 3 $ d Year \""; system. out. println (String. format (pattern, "mingming", "shuilian", 10000, System. currentTimeMillis ();} public static void main (String [] args) {formatAny ();}}