Java magic Hall: String. format

Source: Internet
Author: User
Tags date now

Java magic Hall: String. format
I. Preface String. as a text processing tool, format provides us with powerful and rich String formatting functions. format ("Hello % s", "John");. sort and record the notes below. Ii. overload method // use the current local region object (Locale. getDefault () Format String. format (String fmt, Object... args); // customize the format String of the local region object. format (Locale locale, String fmt, Object... (args); (3) Complete placeholder format: % [index $] [ID] * [minimum width] [. precision] conversion character. For formatting of different data types, the placeholder format will be cropped. %, The actual character of the placeholder. To use % inside the placeholder, you need to write it as %. [Index $]. The Position index starts from 1. It is used to specify to format the real parameters of the index and replace the placeholder. [Identifier], used to enhance the formatting capability. Multiple [identifiers] can be used at the same time, but some identifiers cannot be used at the same time. [Minimum width] is used to set the minimum length of the formatted string. If [minimum width] is used without [ID], when the string length is smaller than the minimum width, then, fill in spaces on the left to minimize the width. [. Precision]: For floating point formatting, set the number of digits after the decimal point. Conversion character, used to specify the format, and limit the Data Type of the corresponding input parameter. 4. format the placeholder characters and strings as follows: % [index $] [identifier] [minimum width] conversion character example -- format "hello" as "hello" String raw = "hello"; String str = String. format ("% 1 $7 s", raw); // simplified // String str = String. format ("% 7 s", raw); example -- format "hello" to "hello" String raw = "hello"; String str = String. format ("% 1 $-7 s", raw); // simplified // String str = String. format ("%-7 s", raw); available identifier:-, left aligned within the minimum width, with spaces on the right. Available conversion character: s, string type. C, character type. The real parameter must be a char, int, short, or other data type that can be converted to a char type. Otherwise, the IllegalFormatConversionException is thrown. B, boolean type, as long as the real parameter is a boolean type that is not false, are formatted as the string true, otherwise the string is false. N, the platform's Independent Line Break (with. getProperty ("line. the format of the placeholder for integer formatting is as follows: % [index $] [identifier] * [minimum width] conversion character example -- display 1 as 0001 int num = 1; String str = String. format ("% 04d", num) Example -- display-1000 as (1,000) int num =-1000; String str = String. format ("% (, d", num) Available identifiers: Copy code-, left aligned within the minimum width, cannot be used with 0 identifiers. 0. If the content length is less than the minimum width, fill it with 0 on the left. # Add a 0, 16, and 0x before the 8-in and 16-in. +, The result always contains a plus (+) or minus (-) sign. Space: Add a space before a positive number, and add a-sign before a negative number .,, Only in decimal format. each three-digit number is separated by commas. (If the result is a negative number, it is enclosed in parentheses and the symbol is not displayed. Available Conversion characters of the copy code: B, Boolean Type. If the real parameter is a boolean type other than false, all values are formatted as true; otherwise, the value is false. D, integer type (decimal ). X, integer type (hexadecimal ). O, integer type (octal) n, platform independent line break, can also be through the System. getProperty ("line. separator ") 6. format the placeholder for floating point number formatting: % [index $] [ID] * [minimum width] [. precision] conversion character example: double num = 123.4567899; System. out. print (String. format ("% f % n", num); // 123.456790 System. out. print (String. format ("% a % n", num); // 0x1. edd3c0bb46929p6 System. out. print (String. format ("% g % n", num); // 123.457 available identifier: copy the code-, which is left aligned within the minimum width and cannot be used with the 0 identifier. 0. If the content length is less than the minimum width, fill it with 0 on the left. # Add a 0, 16, and 0x before the 8-in and 16-in. +, The result always contains a plus (+) or minus (-) sign. Space: Add a space before a positive number, and add a-sign before a negative number .,, Only in decimal format. each three-digit number is separated by commas. (If the result is a negative number, it is enclosed in parentheses and the symbol is not displayed. Available Conversion characters of the copy code: B, Boolean Type. If the real parameter is a boolean type other than false, all values are formatted as true; otherwise, the value is false. N, a platform-independent line break, which can also be obtained through System. getProperty ("line. separator. F: Float (decimal ). A nine-digit valid number is displayed and rounded. For example, 99.99. A, floating point (hexadecimal ). E, exponential type. For example, 9.38e + 5. G, floating point type (shorter than % f, % a, with 6 Valid digits displayed and rounded) 7. format the date and time as a placeholder: % [index $] t conversion character example: Date now = new Date (); String str = String. format ("% tF", now); // 2014-10-12 convertible 1. copy the code c for the date conversion operator, 27 14:21:20 CST 2007F, 2007-10-27D, 10/27/07r, 02:25:51 T, 14: 28: 16R, 14: 28b, month B, the full name of the month is a, the short name of the week is A, the full name of the week is C, two years ago (less than two zeros) y, two years later (less than two zeros) j, the day of the year m, month (less than two zeros) d, date (less than two zeros) e, date (less than two do not zero) Copy Code 2. copy the Code H with the time token. The hour in the 24-hour format (less than two zeros) k, 2 4-hour (less than two do not make zero) I, 12-hour (less than two do zero) I, 12-hour (less than two do not make zero) M, minute (less than two zeros) S, second (less than two zeros) L, Millisecond (less than three zeros) N, Millisecond (less than nine zeros) p, the morning or afternoon Mark of lowercase letters. For example, if the Chinese character is "Afternoon" and the English character is pmz, the time zone offset relative to GMT, such as + 0800Z, and the time zone abbreviation, such as CSTs, q: The number of seconds that have elapsed since 00:00:00. Copy code 8 and other Conversion characters <from 00:00:00 to format the parameters described by the previous conversion character. Example: int num = 1000; String str = String. format ("% d % <, d", num); // result "1000 1,000

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.