Quick Start introduction to the powerful Java String. format (), javastring. format

Source: Internet
Author: User
Tags month name

Quick Start introduction to the powerful Java String. format (), javastring. format


Since Java 5.0, the String class has added a powerful String formatting method format (). There are still not many people using this method till now, which is a waste. This article takes you through the function of this method quickly. When you want to format text in the future, you may not need to borrow a third-party class library or implement it yourself.

First, let's look at a simple example:

String formatted = String. format ("% s % d years old. "," Xiao Li ", 30); //" Xiao Li is 30 years old. "

You can also see that:

  • The first parameter in this method is the format string, followed by the parameters of the format string, used to replace the placeholder in the format string.
  • Placeholders are represented in the form of "% x". Different parameter types must use different letters. It will be detailed later.
  • String. format () the return value type is String, that is, the format result.

I. placeholder type

The letter after the Placeholder "%" determines the type of the actual parameter it accepts. There are several types of placeholders:

Letter Applicable parameter type Description
% Floating Point Number Returns a floating point number in hexadecimal notation.
% B/% B Any value If the parameter is null, false is output; otherwise, true is output.
% C/% C Character or integer Output Unicode characters
% D Integer Format and output Integers
% E/% E Floating Point Number Output floating point number in scientific notation
% F Floating Point Number Formatting and outputting floating point numbers
% G/% G Floating Point Number Use conditions to determine whether to output floating point numbers in scientific notation
% H/% H Any value Return Value of hashCode () of the output parameter in hexadecimal format
% O Integer Returns an integer in octal format.
% S/% S String Format and output strings
% T Date and Time Format and output the date and time
% X/% X Integer Returns an integer in hexadecimal notation.
% N None Line Break
% None Percent itself

Uppercase letters indicate that all output letters are uppercase letters.

We usually use % s, % d, and % f most often, and occasionally use % t. This article is limited in length and only introduces these four types. For the rest, please read the API documentation on your own.

Ii. String and integer formatting

Examples are provided here to illustrate:

// Fill in space and align right: String. format ("% 10 s, world", "Hello"); // output "Hello, world" String. format ("% 8d", 123); // output "123" // fill in space and align left: String. format ("%-10 s, world", "Hello"); // output "Hello, world" String. format ("%-8d", 123); // output "123" // fill in 0 and align (only valid for numbers) String. format ("% 08d", 123); // The output is "123" String. format ("%-08d", 123); // error! 0 cannot be added to the right. // a maximum of N strings can be output. format ("%. 5 s "," Hello, world "); // output" Hello "String. format ("%. 5s... "," Hello, world "); // output" Hello... "String. format ("% 10.5s.."," Hello, world "); // output" Hello... "// output comma-separated numbers String. format ("%, d", 1234567); // output "1,234,567"

3. Date formatting

This is a little complicated, but if you want to mix text numbers and dates in a string, it is easier to tune only one method than to combine DateFormat and NumberFormat.

First, you can add a placeholder parameter that can specify a location in the format of % n $. For example, % 2 $ d indicates the second integer parameter. Note that n starts from 1 instead of 0.

When formatting a date, multiple placeholders need to point to the same parameter (to avoid repeating the same parameter several times), because "t" indicates the date time, therefore, the complete format is % n $ tX. X indicates the part of the time. The optional values of X are as follows:

Y = year; m = month; d = day; H = hour; M = minute; S = Second; L = millisecond; A = day of the week (name ); B = month name;

There are other letters. For more information, see the API documentation. The following is an example:

// The output format is "Now is 15:04:52, Sunday" // note that 10 in "% 1 $ 10tH" also indicates that the space is filled with 10 digits and the right-aligned String. format ("Now is % 1 $ 10tH: % 1 $ tM: % 1 $ tS, % 1 $ tA", new Date ())


The above is all the content of this article. I hope the content of this article has some reference and learning value for everyone's learning or work. If you have any questions, please leave a message to us, thank you for your support.

Related Article

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.