Home > Java > javaTutorial > body text

Use Java's String.format() function to format a string according to the specified format

王林
Release: 2023-07-25 17:12:20
Original
1276 people have browsed it

Use Java's String.format() function to format the string according to the specified format

String.format() is a very useful function in Java. It can format the string according to the specified format. The style we want. This function is very flexible and can be applied to various scenarios, such as date formatting, number formatting, etc. In this article, we will introduce the usage of String.format() and give some sample code.

The basic syntax of the String.format() function is:
String.format(String format, Object... args)

Among them, format is a template in string format, args is an object array of variable parameters. Format can contain placeholders, which are used to specify the position of the elements in the args array displayed in the string. The following are some commonly used placeholders and their corresponding parameter types:

  • %s: String type
  • %d: Integer type
  • %f: Floating point type
  • %c: Character type
  • %b: Boolean type
  • %n: Newline character

Here is a simple Sample code shows how to use the String.format() function to format a string into a specified format:

String name = "Alice";
int age = 25;

String formattedString = String.format("My name is %s and I am %d years old.", name, age);
System.out.println(formattedString);
Copy after login

The output result is:

My name is Alice and I am 25 years old.
Copy after login

In addition to the order of parameters, we can also specify The index of the parameter to change the order of the parameters in the string. The following sample code shows how to use indexes to change parameter positions:

String name = "Bob";
int age = 30;

String formattedString = String.format("I am %2$d years old and my name is %1$s.", name, age);
System.out.println(formattedString);
Copy after login

The output result is:

I am 30 years old and my name is Bob.
Copy after login

In practical applications, in addition to specifying the parameter position and type, we can also use a A series of formatting flags to customize the output format. These flags can be used to control the precision, width, alignment, etc. of the output. The following are some commonly used formatting flags and their usage:

  • %d: Formatting flag of integer type, output integer.
  • %f: Formatting flag of floating point number type, output floating point number.
  • %s: formatting flag of string type, output string.
  • %t: Formatting flag of time and date type, output time and date.

The following sample code shows how to use formatting flags to customize the date output format:

import java.util.Date;

Date now = new Date();
String formattedDate = String.format("Today is %tF", now);
System.out.println(formattedDate);
Copy after login

The output result is:

Today is 2021-01-01
Copy after login

To summarize, String.format The () function is a very convenient function that can format a string into the style we want according to the specified format. Whether it is date formatting, number formatting or other scenarios, we can achieve it through the String.format() function. When using it, we need to understand the usage of placeholders, parameter indexes and formatting flags in order to grasp the powerful function of this function.

I hope that through the introduction of this article, you will have a deeper understanding of the String.format() function and be able to flexibly apply this function in actual development. Happy coding!

The above is the detailed content of Use Java's String.format() function to format a string according to the specified format. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!