%1 $ s indicates that the first replacement location is placed as a string type such as System.out.println (String.Format ("%1 $ s Hello%2$s", "XXX", "!")); The output is "XXX Hello!" String.Format after the first argument is an expression, can be placed in the following n to put the value of the detailed usage reference below: Import java.math.bigdecimal;/** * 1.%[argument_index$][flags][width ][.precision]conversion * * String.Format ("%1 $ S", 1234,5678); Point to the first argument to the string * String.Format ("%1$s%2$s", 1234,5678), format the two parameters as a string, and concatenate together * String.Format ("%s", 1234,5678); Point to the first argument converted to a string * String.Format ("%s%06d", 1234,5678); Format the first as "1234" the second format 005678 W */public class StringFormat {/** * handles floating-point data application range float, float, double, double, and BigDecimal *%[ Argument_index$][flags][width][.precision]conversion *%[index$][logo [min width] [reserved precision] Conversion * Logo: * '-' to the left of the minimum width, not with the "fill with 0" using * ' + ' results always include a symbol * ' positive value before adding a space, negative before negative Number * ' 0 ' results will be filled with zero * ', ' between 3 digits with ', ' delimited (only for FGG conversions) * ' (' If the argument is negative, the result does not add a minus sign but enclose the number in parentheses (only for EEFGG conversions) * Minimum width: The length of the string to which the integer is converted contains at least the number of digits * Reserved precision: Keep the number of decimal digits after * Conversion Mode: * ' e ', ' e '--result is lattice Decimal number as represented by computer science notation * ' F '--the result is formatted as a decimal normal representation * ' g ', ' g '--depending on the situation, Automatic selection in normal notation or scientific notation * ' a ', ' a '--the result is formatted as a hexadecimal floating-point number with significant digits and exponent * */public STA tic void Formatfloat () {System.out.println (String.Format ("%1$e", 1234567890.123456789));// Conversion to scientific notation represents SYSTEM.OUT.PRINTLN (String.Format ("%1$020e", 1234567890.123456789));//converted to scientific notation, length 20, With 0 filler System.out.println (String.Format ("%1$g", 12345.123456789)), or//based on the result of the brake identification using the converter E or fSystem.out.println ( String.Format ("%1$a", 12345.123456789));//Convert to 16-binary floating-point System.out.println (String.Format ("%1$,f", 1234567890.123456789));//The conversion result retains the default decimal digits, 3 digits are separated by, and converted to decimal notation System.out.println ("%1$,f", 1234567890.123456789));//The conversion result retains the default decimal place, 3 digits, separated System.out.println (String.Format ("%1$.10f", 1234567890.123456789)) ;//The conversion result is to preserve 10-bit precision. Convert to decimal notation System.out.println (STring.format ("%1$,.100f", New BigDecimal ("12345678909.1234567890123456789")));//The conversion result is to retain 100 bits of precision, no loss of precision, the integer digits 3 is used, Separates System.out.println (String.Format ("%1$,.5f", 1234567890.123456789));//Conversion results reserved 5 decimal places, 3 digits, separated}/** * Processing integer data application range byte, Byte, short, short, int, Integer, long, long, and BigInteger * *%[argument_index$][flags ][width]conversion *%[index$][logo [minimum width] conversion * Identification: * '-' to the left of the minimum width, not with the "fill with 0" use * ' # ' only applies to 8 binary and 16 binary, 8 when adding a 0,16 in front of the result add 0x * ' + ' result always includes a symbol (usually only applicable For 10 binary, if the object is BigInteger can be used for 8 and 16 binary) * "positive value before adding a space, negative value before the minus sign (generally only applies to 10, if the object is BigInteger can be used for 8 and 16 binary * ' 0 ' results will be filled with zero * ', ' only applicable to 10 binary, each 3 digits between the "," Delimited * ' (' If the argument is negative, the result does not add a minus sign but enclose the number in parentheses (same as ' + ') * Minimum width: Finally, the integer converted string contains at least how many digits * Conversion mode: D-Decimal O-octal x or X -16 binary */public static void FormatNumber () {System.out.println (String.Format ("%1$d",-31)); Format numeric representation using decimal, result " -31" System.out.println (String.Format ("%1$o",-31)); Formatted as a numeric representation using octal, the result "37777777741" System.out.println (String.Format ("%1$19d",-31));//format numeric representation using decimal, total length display 19-bit result " -31 "System.out.println (String.Format ("%1$-19d ",-31));//format numeric representation using decimal, total length display 19 bits, left snap result" -31 "System.out.print ln (String.Format ("%1$09d",-31));//formatted as a numeric representation, using decimal, result " -00000031" System.out.println (String.Format ("%1$,9d",-3123) )////3 digits with, separated by, total length 9 bits, result " -3,123" System.out.println (String.Format ("%1$,09d",-3123));//3 digits with, spaced, 0-filled total length 9 bits, result "- 0003,123 "System.out.println (String.Format ("%1$ (9d ",-3123));//3 digits with 0 padding for total length 9 bits, result" (3123) "System.out.println ( String.Format ("%1$ 9d",-31));} /** * Processing character data * Formatting characters is very simple, C C is a character, the identity '-' means left-aligned, and nothing else */public static void Formatchar () {System.out.println (S Tring.format ("%1$c", 97));//Convert to character System.out.println (String.Format ("%1$10c", "Zou"));//Convert to character, 10-bit System.out.println ( String.Format ("%1$-10c", ' hung '));//Convert to WordCharacters, 10 bits, left}/** * format percentage.% special characters. The escape format is percent rather than */public static void Formatbaifenbi () {System.out.println (String.Format ("%1$f%%", 12.123456)); System.out.println (String.Format ("%1$.4f%%", 12.123456));//Leave 4 decimal places, 4 5 into BigDecimal a = new BigDecimal ("12.12"), B = new BigDecimal ("13.13"); BigDecimal C = a.divide (B,28,BIGDECIMAL.ROUND_HALF_UP);//reserved 28 decimal places SYSTEM.OUT.PRINTLN (C + ""); System.out.println (String.Format ("%1$.28f", c));//format reserved for 28 decimal places}/** * Get stand-alone platform row delimiter */public static void Getseparator () { System.out.println (String.Format ("%n")); System.out.println (System.getproperty ("Line.separator"));} /** * Formatted date (usable range long,long,calendar,java.util.date) *%[index$][identification [min Width] Conversion Mode * ID: * Date and time conversion characters before Prefix t or T * conversion: * Format Date Convert characters * ' B ' full month full name specific to the locale, such as "January" and "February". * ' B ' is a locale-specific month abbreviation, such as "Jan" and "Feb". * ' h ' is the same as ' B '. * ' A ' the full name of the day of the week that is specific to the locale, such as "Sunday" and "Monday" * ' A ' a language-specific day of the week abbreviation, such as "Sun" and "Mon" * ' C ' divided by 100 of the four-digit year, is formatted as necessary with leading zeros of two digits, that is, 00 -* ' Y ' year, formatted as necessary with a leading zero four-digit number (at least), for example, 0092 is equal to the Gregorian calendar of the sum of the CE. * The last two digits of the ' Y ' year, formatted as necessary with leading zeros of two digits, i.e. 00-99. * ' J ' number of days in the year, formatted as necessary with a leading zero three digits, for example, for the Gregorian calendar is 001-366. * ' m ' month, is formatted as necessary with a leading zero two-digit number, i.e. 01-13. * The number of days in the ' d ' one month, formatted as necessary with a leading 2-digit number, that is, the number of days in 01-31 * ' e ' one months, is formatted as a two-digit number, or 1-31. * * Format Time character * ' H ' 24 hour, which is formatted as necessary with leading zeros of two digits, i.e. 00-23. * ' I ' 12 Hour hour, is formatted as necessary with a leading zero of two digits, that is, 01-12. * ' K ' 24 hour hour, i.e. 0-23. * ' l ' 12 hour hour, i.e. 1-12. * Minutes in ' M ' hours, formatted as necessary with a leading zero double-digit, i.e. 00-59. * Seconds in minutes, formatted as necessary with leading zeros of two digits, i.e. 00-60 ("60" is a special value required to support leap seconds). * milliseconds in ' L ' seconds, formattedA three-digit number with a leading zero, or 000-999, if necessary. * The nanosecond in ' N ' seconds, formatted as necessary with a leading zero nine-digit number, or 000000000-999999999. * ' P ' the locale-specific morning or afternoon markers are indicated in lowercase, such as "AM" or "PM". Use the conversion prefix ' T ' to forcibly convert this output to uppercase. * ' Z ' of the digital time zone offset in RFC 822 format relative to GMT, for example-0800. * ' Z ' represents a string in the form of a time zone abbreviation. The locale of the Formatter will supersede the locale of the parameter, if any. * ' s ' from Coordinated Universal Time (UTC) January 1, 1970 00:00:00 to now the number of seconds elapsed, namely long.min_value/1000 and long.max_value/1000 Difference between the two values. * ' Q ' from Coordinated Universal Time (UTC) January 1, 1970 00:00:00 to now the number of milliseconds elapsed, i.e. the difference between long.min_value and Long.max_value * Format time combination character * ' R ' 24 hour time, be formatted as "%TH:%TM" * ' T ' 24 hour system time, be lattice "%th:%tm:%ts". * ' R ' 12-hour time, is formatted as "%ti:%tm:%ts%TP". The location of the morning or afternoon mark ('%TP ') may be related to the locale. * ' D ' date, is formatted as "%tm/%td/%ty". * The full date of the ' F ' ISO 8601 format is formatted as "%TY-%TM-%TD". * ' C ' date and time,is formatted as "%ta%TB%td%tt%tz%ty", such as "Sun Jul 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 ' will generate ' true ' or ' false ' and the result is ' false ' if the argument is null. If the argument is a Boolean or Boolean, then the result is a string returned by String.valueof (). Otherwise, the result is "true". * ' h ' or h ' \u0068 ' generates a string representing the hash code value of the object. If the parameter arg is NULL, the result is "null". Otherwise, the result is the result of calling Integer.tohexstring (Arg.hashcode ()). * ' s or S ' \u0073 ' generates a string. If the argument is null, the result is "null". If the parameter implements Formattable, its Formatto method is called. Otherwise, the result is the result of the toString () method that invokes the parameter. * * */public static void Formatany () {System.out.println (String.Format ("%b%b%b%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 ();}}
Notes on similar issues with token=%1$s