Formatting of a Java string string

Source: Internet
Author: User

JDK1.5, the string class has added a useful static method String.Format ():
Format (Locale l, string format, Object ... args) returns a formatted string using the specified locale, format string, and parameters.

Format (string format, Object ... args) returns a formatted string using the specified format string and parameters. The string format parameter in the format () method has many kinds of conversion options, such as date, integer, floating-point number, and so on.

I. Regular formatting


Example:

<span style= "Font-family:microsoft yahei;font-size:14px;" >public static void Main (string[] args) {    String str=null;    Str=string.format ("hi,%s", "dragon");          Formatted string    System.out.println (str);                             Output string variable str content    System.out.printf ("The uppercase of the letter A is:%c%n", ' a ')    ; System.out.printf ("3>7 results are:%b%n", 3>7);    System.out.printf ("Half of 100 is:%d%n", 100/2);    System.out.printf ("100 of 16 binary numbers are:%x%n", +);    System.out.printf ("100 of 8 binary numbers are:%o%n", +);    System.out.printf ("50 yuan book 8.5 discount is:%f Yuan%n", 50*0.85);    System.out.printf ("16 of the above price is:%a%n", 50*0.85);    System.out.printf ("The index of the above price indicates:%e%n", 50*0.85);    System.out.printf ("The index of the above price and the length of the floating point result is shorter:%g%n", 50*0.85);    System.out.printf ("The above discount is%d%%%n",);    System.out.printf ("The hash code for the letter A is:%h%n", ' A ');} </span>
Operation Result:

<span style= "Font-family:microsoft yahei;font-size:14px;" >hi, the capital of the Dragon letter A is: A3>7 The result is: false100 half is: 50100 of 16 binary is: 64100 of 8 binary is: 14450 Yuan book 8.5 discount is: 42.500000 The 16 binary of the price above is: 0X1.54P5 The above price index means: 4.250000e+01 above the price index and floating point result of the shorter length is: 42.5000 above the discount is 85% letter a hash code is:41</span>
these string format parameters not only have the flexibility to convert other data types into strings, but can be combined with various flags to produce strings of various formats


Example:

<span style= "Font-family:microsoft yahei;font-size:14px;" >public static void Main (string[] args) {    String str=null;    Str=string.format ("format parameter $ 's use:%1$d,%2$s", "the", "the" ABC ");      System.out.println (str);                                         System.out.printf ("Show positive negative sign:%+d and%d%n", 99,-99);    System.out.printf ("The most bull's number is:%03d%n", 7);    System.out.printf ("Tab key effect is:% 8d%n", 7);    System.out.printf ("Integer Grouping effect is:%,d%n", 9989997);    System.out.printf ("The price of a book is:%2.2f Yuan%n", 49.8);} </span>
Operation Result:

<span style= "Font-family:microsoft yahei;font-size:14px;" Use of the > Format parameter $: 99,ABC shows positive and negative numbers: +99 and 99 The most bullish number is: the effect of the 007Tab key is:       7 integer Grouping effect is: 9,989,997 The price of a book is: 49.80 yuan </span>
Ii. Common Date and time formatting


Example:

<span style= "Font-family:microsoft yahei;font-size:14px;" >public static void Main (string[] args) {    date date=new date ();                                                  System.out.printf ("All date and time information:%tc%n", date);                   SYSTEM.OUT.PRINTF ("year-month-day format:%tf%n", date);    SYSTEM.OUT.PRINTF ("Month/day/year format:%td%n", date);    System.out.printf ("HH:MM:SS PM format (12 o'clock):%tr%n", date);    System.out.printf ("HH:MM:SS format (24 o'clock):%tt%n", date);    System.out.printf ("hh:mm format (24 o'clock):%tr", date);} </span>
Operation Result:

<span style= "Font-family:microsoft yahei;font-size:14px;" > All date and time information: Sunday October 13:53:24 CST 2007-month-day format: 2007-10-28 month/day/year format: 10/28/07HH:MM:SS pm Format (12 o'clock): 01:53:24 pm HH:MM: SS Format (24 o'clock System): 13:53:24hh:mm format (24 o'clock):13:53</span>
third, format date string


Example:

<span style= "Font-family:microsoft yahei;font-size:14px;"                                              >public static void Main (string[] args) {date date=new date ();          String Str=string.format (locale.us, "English month Abbreviation:%TB", date);                                               System.out.println (str);    System.out.printf ("Local month abbreviation:%tb%n", date);    Str=string.format (locale.us, "English month Full name:%TB", date);    System.out.println (str);    System.out.printf ("Local Month full name:%tb%n", date);    Str=string.format (locale.us, "English week Abbreviation:%ta", date);    System.out.println (str);    System.out.printf ("Local week abbreviation:%ta%n", date);    System.out.printf ("The first two digits of the year (less than two bits before 0):%tc%n", date);    System.out.printf ("the latter two digits of the year (less than two bits before 0):%ty%n", date);    System.out.printf ("Number of days in the Year" (The Day of the year):%tj%n ", date);    System.out.printf ("Two digits of the month (less than two bits before 0):%tm%n", date);    System.out.printf ("Two digits of the day (less than two bits in front of 0):%td%n", date); System.out.printf ("Day of the month (not before 0):%te", date);} </span> 
Operation Result:

<span style= "Font-family:microsoft yahei;font-size:14px;" > English month abbreviation: Oct local month abbreviation: October full name: October Local Month full name: October English week abbreviation: Sun local week abbreviation: The first two digits of Sunday (less than two front 0): 20 of the last two digits (less than two digits before 0): 71 years of the number of days (the day of the year): 30 12-digit month (less than two bits preceded by 0): 102-digit day (less than two digits before 0): Day of the 28 month (not prior to 0):28</span>
four, format time string


Example:

<span style= "Font-family:microsoft yahei;font-size:14px;"                               >public static void Main (string[] args) {date date=new date ();    Create Date Object System.out.printf ("2-digit 24 o'clock hour (less than 2 bits before 0):%th%n", date);    System.out.printf ("2-digit 12 o'clock hour (less than 2 bits in front 0):%ti%n", date);    System.out.printf ("2-digit 24 o'clock hour (front not 0):%tk%n", date);    System.out.printf ("2-digit 12 o'clock hour (front not 0):%tl%n", date);    System.out.printf ("2 digits in minutes (less than 2 bits before 0):%tm%n", date);    System.out.printf ("2 digit seconds (less than 2 bits in front of 0):%ts%n", date);    System.out.printf ("3 digits in milliseconds (less than 3 bits before 0):%tl%n", date);    System.out.printf ("9 digits in milliseconds (less than 9 bits in front of 0):%tn%n", date);    String Str=string.format (locale.us, "small letter morning or afternoon mark (English):%TP", date);                          System.out.println (str);    Output string Variable str contents System.out.printf ("lowercase letter of the morning or afternoon mark (medium):%tp%n", date);    System.out.printf ("Offset from RFC822 time zone relative to GMT:%tz%n", date);    System.out.printf ("time zone abbreviation string:%tz%n", date);    System.out.printf (number of seconds from 1970-1-1 00:00:00 to now:%ts%n, date); System.out.printf ("1970-1-1 00:The number of milliseconds elapsed from 00:00 to now:%tq%n ", date);} </span>
Operation Result:

<span style= "Font-family:microsoft yahei;font-size:14px;" >2 Digit 24 o'clock hours (less than 2 bits in front of 0): 152-digit 12 o'clock hour (less than 2-bit front 0): 032-digit 24 o'clock-hour (0): 152-digit 12 o'clock hour (front not 0): 32-digit minute (less than 2 digits before 0) : 242-digit seconds (less than 2 bits preceded by 0): 563-digit milliseconds (less than 3 bits before 0): 8,289-digit milliseconds (less than 9 digits before 0): 828000000 lowercase morning or afternoon markers ( English): PM or PM mark (medium): Afternoon offset from GMT in RFC822 time zone: + 0800 time zone abbreviation string: Cst1970-1-1 00:00:00 to the present number of seconds: 11935562961970-1-1 The number of milliseconds elapsed from 00:00:00 to now:1193556296828</span>


Formatting of a Java string string

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.