In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces how to use String.format () in Java. It is very detailed and has a certain reference value. Friends who are interested must finish it!
Preface
Since Java 5. 0, a powerful string formatting method format () has been added to the String class. Up to now, not many people have used this method, which is really a waste. This article takes you through the functions of this method quickly, and when you want to use formatted 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 this year% d years old." Xiao Li, 30) / / Xiao Li is 30 years old this year. "
There is no need for me to explain, you can also see:
The first parameter of this method is the format string, and the following parameters are the parameters of the format string, which are used to replace the placeholders in the format string.
Placeholders are represented in the form of "% x", and different parameter types use different letters. It will be described in detail later.
String.format () returns a string of type, which is the result of formatting.
I. placeholder type
The letter after the placeholder "%" determines the type of actual parameter it accepts. There are several types of placeholders:
Letter applicable parameter type description% a floating point number output floating point number% b /% B any value in hexadecimal output false if the parameter is null Otherwise output true%c /% C character or integer output corresponding Unicode character% d integer format integer output% e /% E floating-point number output floating-point number% f floating-point number format floating-point number output% g /% G floating-point number to determine whether to output floating-point% h /% H arbitrary value in scientific notation with hexadecimal output parameter HashCode () return value% o integer in octal output integer% s /% s string formatted string% t date time formatted date time output% x /% X integer output integer% n no newline%% no percent sign itself
Capital letters indicate that the output letters are all uppercase.
The most common ones we use are% s,% d and% f, and occasionally use% t. This article is limited. I will only introduce these four. Please read the API documentation for the rest.
II. String and integer formatting
Here is a direct example to illustrate:
/ / complete the blanks and align them to the right: String.format ("s, world", "Hello"); / / output "Hello, world" String.format ("% 8d", 123); / / output "123s" / / complete the blanks and align them to the left: String.format ("%-10s, world", "Hello"); / / output "Hello, world" String.format ("%-8d", 123) / / output "123d" / / make up 0 and align (valid for numbers only) String.format ("d", 123); / / output "123" String.format ("%-08d ", 123); / / error! It is not allowed to complete 0amp on the right / output up to N characters String.format ("% .5s", "Hello, world"); / / output "Hello" String.format ("% .5s...", "Hello, world"); / / output "Hello..." String.format (".5s...", "Hello, world") / / output "Hello..." / / output comma-separated digits String.format ("%, d", 1234567); / / output "1234567"
III. Date formatting
This is a little more complicated, but if you want to mix text numbers and dates in a string, only one method should be more convenient than combining DateFormat and NumberFormat.
First of all, add a piece of knowledge, that is, the placeholder can specify the parameters of a location in the format% n $. For example,% 2$ d represents the second shaping parameter. Notice that n starts with 1 instead of 0.
When formatting a date, multiple placeholders are required to point to the same parameter (to avoid repeating the same parameter several times), and because "t" represents date and time, the complete format is% n$tX, where X indicates which part of the time is taken. Optional values for 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, please refer to the API documentation for details. Here is an example:
/ / the output format is "Now is 15:04:52, Sunday" / / Note that the 10 in "1 $10tH" also means that 10 spaces are filled in and right-aligned String.format ("Now is% 1 $10tH:%1$ tM:%1$ tS,% 1$ tA", new Date ()) is all the content of the article "how to use String.format () in Java". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.