Summary of String.Format ()

Source: Internet
Author: User
Tags time and date

Use of Java string Formatting-string.format ()

  

Formatting of regular types

The format () method of the string class is used to create a formatted string and to concatenate multiple string objects. Students familiar with C language should remember the C language of the sprintf () method, the two have similarities. There are two overloaded forms of the format () method.

Format (string format, Object ... args) a new string that uses the local locale to develop a string format and parameters to generate a new formatted string.

Format (locale locale, string format, Object ... args) uses the specified locale to develop string formatting and parameters to generate a formatted string.

The role of formatting

  Not only can you convert integers, string types to different types of data, but you can also convert strings to HTML code, which is a powerful feature. Especially when doing the website is particularly important, can make complex problem simplification.

1. Display different conversion characters to implement different data types to string conversions.

Conversion character

Description

Example

%s

String type

"Mingrisoft"

%c

Character type

' m '

%b

Boolean type

True

%d

Integer type (decimal)

99

%x

Integer type (hex)

Ff

%o

Integer type (octal)

77

%f

Floating-point types

99.99

%a

Hexadecimal floating-point type

Ff.35ae

%e

Index type

9.38e+5

%g

Common floating-point types (shorter in type F and e)

%h

Hash code

%%

Percent type

%n

Line break

%tx

Date and Time type (x represents a different date and time conversion character



2. Match the symbol of the translator.

Flag

Description

Example

Results

+

Add a symbol to a positive or negative number

("%+d", 15)

+15

Align Left

("%-5d", 15)

|15 |

0

Number Front 0

("%04D", 99)

0099

Space

Add a specified number of spaces before an integer

("% 4d", 99)

| 792

,

Group numbers with ","

("%,f", 9999.99)

9,999.990000

(

Use parentheses to include negative numbers

("% (f",-99.99)

(99.990000)

#

If a floating-point number contains a decimal point, add 0x or 0 if the binary is 16 or 8

("% #x", 99)

("% #o", 99)

0x63

0143

<

Format the parameters described by the previous translator

("%f and%<3.2f", 99.45)

99.450000 and 99.45

$

Index of the parameter being formatted

("%1$d,%2$s", "the", "ABC")

99,abc

3. Date and event string formatting

In the program interface often need to display the time and date, but the format of the display is often unsatisfactory, you need to write a lot of code through various algorithms to get the ideal date and time format. There is also a%tx converter in the string format that is not described in detail and is specifically used to format dates and times. The x in the%TX Converter represents an additional conversion that handles date and time formats, and their combination can format dates and times into multiple formats.

Format for common date and time combinations.

Conversion character

Description

Example

C

Include all date and time information

Saturday 14:21:20 CST 2007

F

"Year-month-day" format

2007-10-27

D

"Month/day/year" format

10/27/07

R

"HH:MM:SS PM" format (12 o'clock-year)

02:25:51 pm

T

"HH:MM:SS" Format (24 o'clock-year)

14:28:16

R

"HH:MM" Format (24 o'clock-year)

14:28

Example:

  1. Date date=new Date ();
  2. Use of//c
  3. System.out.printf ("All date and time information:%tc%n", date);
  4. Use of//f
  5. System.out.printf ("year-month-day format:%tf%n", date);
  6. Use of//d
  7. System.out.printf ("Month/day/year format:%td%n", date);
  8. Use of//r
  9. System.out.printf ("HH:MM:SS pm format (12 o'clock):%tr%n", date);
  10. Use of//t
  11. System.out.printf ("HH:MM:SS format (24 o'clock):%tt%n", date);
  12. Use of//r
  13. System.out.printf ("hh:mm format (24 o'clock):%tr", date);

Results:

    1. All date and time information: Monday borlnd 10:43:36 CST 2012
    2. Year-month-day format: 2012-09-10
    3. Month/day/year format: 09/10/12
    4. HH:MM:SS pm Format (12 o'clock): 10:43:36 a.m.
    5. HH:MM:SS format (24 o'clock): 10:43:36
    6. hh:mm format (24 o'clock): 10:43

Summary of String.Format ()

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.