Java Magic Hall: String.Format detailed

Source: Internet
Author: User
Tags locale

Java Magic Hall: String.Format detailed

Directory

First, preface

Second, heavy-duty method

Third, placeholder

Iv. formatting characters and strings

Five, the integer is formatted

Vi. formatting floating-point numbers

Vii. formatting dates and times

Viii. Other Conversion characters

Ix. Summary

Reference

First, preface

as a text Processing tool, String.Format provides us with a powerful and rich string formatting function, in order not to stop at simply calling String.Format ("Hello%s", "John ");

, keep your notes organized and documented below.

Second, heavy-duty method

formats the string using the current local region object (Locale.getdefault ()) String.Format (String fmt, Object ... args);  custom Local Area object formatting strings string String.Format (locale locale, string fmt, Object ... args);   

Third, placeholder

The full format of the placeholder is: %[index$][identifies the]*[minimum width [. precision] Conversion character.

For formatting of different data types, the placeholder format will be cropped.

%, the real character of the placeholder, to use% within the placeholder, you need to write a percent .

[index$], the position index is calculated starting from 1, which specifies that the corresponding argument of the index is formatted and replaced by the placeholder.

[Identity], which is used to enhance formatting capabilities and can use multiple [identities] at the same time, but some identities cannot be used at the same time.

[minimum width], used to set the minimum length of the formatted string, if using [minimum width] without setting [identity], then when the length of the string is less than the minimum width, the left fill the space to the minimum width.

[. Precision], for floating-point type formatting use, set how many digits are retained after the decimal point.

A conversion character that specifies the formatted style, and restricts the data type that corresponds to the entry parameter.

Iv. formatting characters and strings

Placeholder format: %[index$][identifier [minimum width] translator

Example-format "Hello" as "Hello"

"Hello"; String str = String.Format ("%1$7s", raw);  simplified //String str = String.Format ("%7s", raw);          

Example-format "Hello" as "Hello"

"Hello"; String str = String.Format ("%1$-7s", raw);  simplified //String str = String.Format ("%-7s", raw);          

Available Identities:

--left-aligned in the minimum width, with blanks on the right.

Available Converters:

s, String type.

C, the character type, the argument must be char or int, short, etc. can be converted to a char type data type, otherwise throw illegalformatconversionexception exception.

B, Boolean type, as long as the argument is a Boolean type other than False, all are formatted as String true, otherwise the string is false.

N, platform-independent newline character ( as with System.getproperty ("line.separator")

Five, the integer is formatted

Placeholder Format: %[index$][identity]*[min Width] Translator

Example--Display 1 as 0001

1; String str = String.Format ("%04d", num)   

Example--Display 1000 as (1,000)

int num =-+; String str = String.Format ("% (, D", num)    

Available Identities:

-left-aligned within the minimum width and cannot be used with the 0 ID. 0, if the content length is less than the minimum width, the left is filled with zeros. #, add 0x to 8 binary and 16 binary, 8 before adding a 0,16 before the binary. +, the result always contains a + or- number. Spaces, plus spaces before positive numbers, plus-numbers before a negative number. , separated by only the decimal, with each 3 digits. (If the result is negative, enclose it in parentheses and do not display the symbol.) 

Available Converters:

B, Boolean type, as long as the argument is a Boolean type other than False, all are formatted as String true, otherwise the string is false. D, Integer type (decimal). X, integer type (hexadecimal). O, Integer type (octal) n, platform-independent line break, or System.getproperty ("line.separator") to get  

Vi. formatting floating-point numbers

Placeholder Format: %[index$][identity]*[min width [. precision] Conversion

Example:

double num = 123.4567899 out.print (String.Format ( " %f%n// 123.456790 system. Out.print (String.Format ( "%a%n // 0X1.EDD3C0BB46929P6 system. Out.print (String.Format ( "%g%n // 123.457        

Available Identities:

-left-aligned within the minimum width and cannot be used with the 0 ID. 0, if the content length is less than the minimum width, the left is filled with zeros. #, add 0x to 8 binary and 16 binary, 8 before adding a 0,16 before the binary. +, the result always contains a + or- number. Spaces, plus spaces before positive numbers, plus-numbers before a negative number. , separated by only the decimal, with each 3 digits. (If the result is negative, enclose it in parentheses and do not display the symbol.) 

Available Converters:

B, Boolean type, as long as the argument is a Boolean type other than False, all are formatted as String true, otherwise the string is false. N, platform-independent newline characters can also be obtained through System.getproperty ("line.separator"). F, floating-point type (decimal). A 9-digit number is displayed and rounded. such as 99. . A, floating-point type (hex). E, index type. such as 9.38e+5. G, floating point type (shorter than%f,%a length, showing 6 digits, and rounding)       

Vii. formatting dates and times

Placeholder format: %[index$]t conversion character

Example:

New Date (); String str = String.Format ("%tf// 2014-10-12    

Available conversion characters

1. Conversion of dates

C, Saturday bodyguards2714:21st:20 CST 2007f,2007-10-27d,10/27/07r,02:25:51 afternoon T,14:28:16r,14:28b, month abbreviation B, month full name A, week abbreviation A, week full name C, year before two (less than two bit 0) y, Year two (less than two bit 0) J, the Day of the Year m, month (less than two bit 0) d, date (less than two bit 0) E, date (less than two digits not 0)        

2. Conversion of Time

H, 24-hour hour (less than two-bit 0) K, 24-hour hour (less than two not 0) I, 12-hour hour (less than two-bit 0) I, 12-hour hour (less than two digits not 0) M, minutes (less than two bits 0) s, seconds (less than two bits 0) L, milliseconds (less than three bit 0) N , milliseconds (less than 9 bit 0) p, small letter morning or afternoon marks, such as Chinese as "Afternoon", English pmz, time zone offset relative to GMT, such as +0800Z, time zone abbreviation, such as CSTs, since 1970-1-xx:00 : 00 seconds elapsed Q, since 1970-1-xx:from 00:00 minutes after the Hao second        

Viii. Other Conversion characters

<, which is used to format the parameters described by the previous translator.

Example:

; String str = String.Format ("%d%<,d", num);  results "      

Ix. Summary

Write here for the time being, and use the other additions later!

Respect the original, reprint please indicate from: http://www.cnblogs.com/fsjohnhuang/p/4094777.html ^_^ Fat Boy John

Java Magic Hall: String.Format detailed

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.