Re-explore the use of Java Foundation--string.format (String format, Object ... args)

Source: Internet
Author: User
Tags iso 8601 iso 8601 format posix string format

Recently seen some code like this: String.Format ("parameter%s cannot be empty", "Birthday"); I didn't use it before. This function does not know impurity meaning, after study a bit, detailed explain below.

public static string format(string format, Object ... args) is very powerful and very flexible to use. The main meaning is to return the specified formatted string. The format parameter is formatted as String syntax as follows:

%[argument_index$][flags][width][.precision]conversion

The parameters in "[]" are indicated as optional.

Optional argument_index : The subscript is a decimal integer that indicates the position of the parameter in the argument list.

The optional flags flag is a character set that modifies the output format. The collection of valid flags depends on the conversion type.

Optional width minimum width is a non-negative decimal integer that indicates the minimum number of characters to write to the output.

Optional Precision is a non-negative decimal integer that is typically used to limit the number of characters. The specific behavior depends on the conversion type.

The desired conversion Conversion Method is a character that indicates how the parameter should be formatted. The set of valid conversions for a given parameter depends on the data type of the parameter.

The above explanation is more boring, the code example is as follows;

EG1: Description Argument_index and width

SYSTEM.OUT.PRINTLN (msg);

String s = String.Format ("This is a%2$s%1 $ s%s test", "Java", "C + +");

This is a C + + Java Java C + + Test

Eg2. Description Flags

System.out.println (String.Format ("%1$,09d",-3872));

System.out.println (String.Format ("%1$,010d",-3872));

System.out.println (String.Format ("%1$,09d", 3872));

System.out.println (String.Format ("%1$9d",-14));

System.out.println (String.Format ("%1$-9d",-14));

System.out.println (String.Format ("%1$ (9d",-14));

System.out.println (String.Format ("%1$ #9x", 31));

Results:

-0003,872

-00003,872

00003,872

-14

-14

(14)

0x1f

The following table summarizes the supported flags . y indicates that the flag is supported by the indicated parameter type.

Sign

Conventional

Character

Integer

Floating point

Date/Time

Description

‘-‘

Y

Y

Y

Y

Y

The result will be left-aligned.

‘#‘

Y1

-

Y3

Y

-

The result should use a substitution form that depends on the conversion type

+

-

-

Y4

Y

-

The result always includes a symbol

‘ ‘

-

-

Y4

Y

-

For positive values, a leading space will be included in the result

' 0 '

-

-

Y

Y

-

The result will be populated with zero

‘,‘

-

-

Y2

Y5

-

The result will include locale-specific group separators

‘(‘

-

-

Y4

Y5

-

The result will be a negative number enclosed in parentheses

EG3: Description Conversion

System.out.println (String.Format ("This is:%s%s", "String", "format"));

System.out.println (String.Format ("%b,%b,%b,%b,%b,%b", "true", true, false, NULL, 0>1, 1, ""));

System.out.println (String.Format ("%o", 31));

System.out.println (String.Format ("%x", 31));

System.out.println (String.Format ("%e", 12345.6987));

System.out.println (String.Format ("%a", 12345.6987));

System.out.println (String.Format ("%c,%c,%c,%c", ' A ', ' B ', 48, 98));

Results:

This is:string format

True, True, False, False, False, True

37

1f

1.234570e+04

0x1.81cd96f0068dcp13

A, B, 0, B

Description of the flag:

Transformation

Parameter categories

Description

' B ', ' B '

Conventional

If the parameter arg is null, the result is "false". If Arg is a Boolean value or Boolean, the result is a string returned by String.valueof (). Otherwise, the result is "true".

' H ', ' H '

Conventional

If the parameter arg is null, the result is "null". Otherwise, the result is the result of calling Integer.tohexstring (Arg.hashcode ()).

' s ', ' s '

Conventional

If the parameter arg is null, the result is "null". If Arg implements Formattable, Arg.formatto is called. Otherwise, the result is the result of calling Arg.tostring ().

' C ', ' C '

Character

The result is a Unicode character

' d '

Integer

The result is formatted as a decimal integer

' O '

Integer

The result is formatted as an eight-binary integer

' x ', ' x '

Integer

The result is formatted as a hexadecimal integer

' E ', ' e '

Floating point

The result is formatted as a decimal number in computer science notation

' F '

Floating point

The result is formatted as a decimal number

' G ', ' G '

Floating point

The results are formatted using computer science notation or decimal format, based on the values of precision and rounding operations.

' A ', ' a '

Floating point

The result is formatted as a hexadecimal floating-point number with significant digits and exponent

' t ', ' t '

Date/Time

The prefix of the date and time conversion character. See Date/Time conversion.

‘%‘

Percentage

Result is literal '% ' (' \u0025 ')

N

Row delimiter

The result is platform-specific row separators

String msg = String.Format ("parameter%s cannot be empty", "Birthday");

SYSTEM.OUT.PRINTLN (msg);

EG4: Formatted date

Calendar C = calendar.getinstance ();

String data_str = String.Format ("Today is:%1$ty/%1$tm/%1$te, now-time is%1$th:%1$tm:%1$ts", c);

Results:

Today Is:2013/10/31, now time is 17:36:49

Description of the Date class flag:

The following date and time conversion character suffixes are defined for the ' t ' and ' t ' conversions. These types are similar to, but not identical to, those types defined by the GNU date and POSIX strftime (3c). Additional conversion types are provided to access Java-specific features (for example, ' L ' for milliseconds in seconds).

The following conversion characters are used to format the time:

H

' \u0048 '

The 24-hour hour is formatted as necessary with a leading zero double-digit, or 00-23. 00 corresponds to midnight.

' I '

' \u0049 '

The 12-hour hour is formatted as necessary with a leading zero double-digit, or 01-12. 01 corresponds to 1 o'clock (morning or afternoon).

K

' \u006b '

24-hour hour, or 0-23. 0 corresponds to midnight.

L

' \U006C '

12-hour hour, or 1-12. 1 corresponds to one o'clock in the morning or afternoon.

M

' \u004d '

The minute in the hour, formatted as necessary with a leading zero of two digits, i.e. 00-59.

S

' \u0053 '

Seconds in minutes, formatted as necessary with leading zeros in two digits, i.e. 00-60 ("60" is a special value required to support leap seconds).

L

' \U004C '

The milliseconds in seconds, formatted as necessary with a leading zero three-digit number, or 000-999.

N

' \u004e '

The nanosecond in seconds, formatted as necessary with a leading zero nine-digit number, or 000000000-999999999. The precision of this value is limited by the underlying operating system or hardware analysis.

' P '

' \u0070 '

Locale-specific morning or afternoon marks are indicated in lowercase, such as "AM" or "PM". Use the conversion prefix ' T ' to forcibly convert this output to uppercase. (Note that the output produced by ' p ' is lowercase.) The output from the GNU date and POSIX strftime (3c) is capitalized. )

' Z '

' \u007a '

The digital time zone offset in RFC 822 format relative to GMT, for example-0800.

Z

' \u005a '

A string that represents the abbreviated form of the time zone.

' s '

' \u0073 '

The number of seconds since Coordinated Universal Time (UTC) January 1, 1970 00:00:00 to now, which is the difference between long.min_value/1000 and long.max_value/1000.

' Q '

' \u004f '

The number of milliseconds since Coordinated Universal Time (UTC) January 1, 1970 00:00:00 to now, which is the difference between Long.min_value and Long.max_value. The precision of this value is limited by the underlying operating system or hardware analysis.

The following conversion characters are used to format dates:

B

' \u0042 '

Full month names that are locale-specific, such as "January" and "February".

' B '

' \u0062 '

A locale-specific month abbreviation, such as "Jan" and "Feb".

' H '

' \u0068 '

Same as ' B '.

A

' \u0041 '

Full name of the day of the week that is specific to the locale, such as "Sunday" and "Monday"

A

' \u0061 '

Abbreviation for the day of the week that is specific to the locale, such as "Sun" and "Mon"

C

' \u0043 '

The year, divided by four digits of 100, is formatted as necessary with a two-digit number with leading zeros, which means 00-99

Y

' \u0059 '

Year, which is formatted as necessary with a leading zero four-digit number (at least), such as 0092 is equal to the Gregorian calendar of the sum of the CE.

' Y '

' \u0079 '

The last two digits of the year, which are formatted as necessary with leading zeros of two digits, that is, 00-99.

' J '

' \u006a '

The number of days in a year, formatted as necessary with a leading zero of three digits, for example, for the Gregorian calendar is 001-366. 001 corresponds to the first day of the year.

' m '

' \U006D '

The month, which is formatted as necessary with leading zeros of two digits, i.e. 01-13, where "01" is the first month of the year, ("13" is a special value required to support the lunar calendar).

' d '

' \u0064 '

The number of days in one months, formatted as necessary with leading zeros of two digits, i.e. 01-31, where "01" is the first day of one months.

E

' \u0065 '

The number of days in one months is formatted as two digits, or 1-31, where "1" is the first day of the one month.

The following conversion characters are used to format common date/time combinations.

R

' \u0052 '

24-hour time, formatted as "%TH:%TM"

T

' \u0054 '

The 24-hour time is formatted as "%th:%tm:%ts".

' R '

' \u0072 '

The 12-hour time is formatted as "%ti:%tm:%ts%TP". The location of the morning or afternoon mark ('%TP ') may be related to the area.

D

' \u0044 '

Date, which is formatted as "%tm/%td/%ty".

F

' \u0046 '

The full date of the ISO 8601 format is formatted as "%TY-%TM-%TD".

C

' \u0063 '

The date and time are formatted as "%ta%TB%td%tt%tz%ty", such as "Sun Jul 16:17:00 EDT 1969".

Read more about the documentation from the specific one!

Re-explore the use of Java Foundation--string.format (String format, Object ... args)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.