Java String format
上次修改时间:2024 年 7 月 4 日
在本文中,我们将展示如何在 Java 中格式化字符串。
在 Java 中,我们有字符串格式化方法。另一种动态创建字符串的方法是 字符串构建。
System.out.printf、System.out.format 和 formatted 方法可以用于在 Java 中格式化字符串。 它们的作用相同。这三个方法使用指定的格式字符串和参数将格式化的字符串写入输出流。 如果参数多于格式说明符,则会忽略多余的参数。
%[argument_index$][flags][width][.precision]conversion
通用、字符和数字类型的格式说明符具有以下语法。
%[argument_index$][flags][width]conversion
这是用于表示日期和时间的类型的语法。
格式说明符以 % 字符开头,以 1 或 2 个字符的转换符结尾,该转换符指定要生成的格式化输出的类型。 可选项目位于方括号之间。
argument_index 是一个十进制整数,指示参数在参数列表中的位置。 flags 是一组修改输出格式的字符。 有效标志的集合取决于转换。 width 是一个非负十进制整数,指示要写入输出的最小字符数。
precision 是一个非负十进制整数,通常用于限制字符数。 具体行为取决于转换。 required conversion 是一个字符,指示应如何格式化参数。
Java String format 方法
我们使用这三个方法来格式化一条简单的消息。
void main() {
String name = "John Doe";
String occupation = "gardener";
String txt = "%s is a %s";
String msg = txt.formatted(name, occupation);
System.out.println(msg);
System.out.format("%s is a %s\n", name, occupation);
System.out.printf("%s is a %s%n", name, occupation);
}
我们构建了三次相同的字符串。
String name = "John Doe"; String occupation = "gardener"; String txt = "%s is a %s"; String msg = txt.formatted(name, occupation);
formatted 方法是一个实例方法。
System.out.format("%s is a %s\n", name, occupation);
System.out.printf("%s is a %s%n", name, occupation);
format 和 printf 方法是静态方法。
$ java Main.java John Doe is a gardener John Doe is a gardener John Doe is a gardener
Java String format 说明符
接下来,我们使用两个基本的格式说明符。
void main() {
System.out.format("There are %d %s.%n", 5, "pencils");
System.out.printf("The rock weighs %f kilograms.%n", 5.345);
}
在此程序中,我们格式化两个简单的句子。
System.out.format("There are %d %s.%n", 5, "pencils");
在此代码行中,我们有三个格式说明符。 每个说明符都以 % 字符开头。 d 说明符格式化整数值。 s 说明符需要字符串值。 %n 输出特定于平台的行终止符;它不需要参数。
System.out.printf("The rock weighs %f kilograms.%n", 5.345);
f 将浮点值格式化为十进制值。 System.out.printf 的作用与 System.out.format 相同。
$ java Main.java There are 5 pencils. The rock weighs 5.345000 kilograms.
Java String format 参数索引
在下一个示例中,我们将使用参数索引。
import java.time.LocalDateTime;
void main() {
int x = 12;
int y = 32;
int z = 43;
LocalDateTime dt = LocalDateTime.now();
System.out.format("There are %d apples, %d oranges and "
+ "%d pears%n", x, y, z);
System.out.format("There are %2$d apples, %3$d oranges and "
+ "%1$d pears%n", x, y, z);
System.out.format("Year: %tY, Month: %<tm, Day: %<td%n", dt);
}
该示例使用参数索引来引用包含在参数列表中的变量。
System.out.format("There are %d apples, %d oranges and "
+ "%d pears%n", x, y, z);
如果我们不指定索引,则变量会自动匹配说明符。 d 说明符将整数值格式化为十进制值。
System.out.format("There are %2$d apples, %3$d oranges and "
+ "%1$d pears%n", x, y, z);
1$ 引用 x 变量,2$ 引用 y 变量,3$ 引用 z 变量。
System.out.format("Year: %tY, Month: %<tm, Day: %<td%n", c);
< 标志导致重复使用先前格式说明符的参数。 所有三个说明符都引用 c 变量。 tY 转换字符给出一个年份,格式化为至少四位数字,必要时带有前导零,tm 给出一个月份,格式化为两位数字,必要时带有前导零,td 给出一个月份中的日期,格式化为两位数字,必要时带有前导零。
$ java Main.java There are 12 apples, 32 oranges and 43 pears There are 32 apples, 43 oranges and 12 pears Year: 2022, Month: 10, Day: 17
Java String format 标志
flag 以特定方式修改格式。 有几个可用的标志。 例如,+ 标志要求输出包含所有正数的正号。
void main() {
System.out.format("%+d%n", 553);
System.out.format("%010d%n", 553);
System.out.format("%10d%n", 553);
System.out.format("%-10d%n", 553);
System.out.format("%d%n", -553);
System.out.format("%(d%n", -553);
}
该示例展示了字符串格式说明符的几个标志。
System.out.format("%010d%n", 553);
0 标志将导致输出用前导零填充到最小字段宽度。 我们的数字有三位数字。 最小宽度为 10。因此,我们在输出中有 7 个前导零。
System.out.format("%10d%n", 553);
如果没有 0 标志,则数字将右对齐。
System.out.format("%-10d%n", 553);
- 标志将导致数字左对齐。
System.out.format("%d%n", -553);
System.out.format("%(d%n", -553);
默认情况下,负数带有减号。 如果我们使用 ( 标志,则负值将放在圆括号内。 (这用于会计中。)
$ java Main.java
+553
0000000553
553
553
-553
(553)
Java String format 宽度
width 字段是要写入输出的最小字符数。 它不能与行分隔符一起使用。
void main() {
System.out.println(1);
System.out.println(16);
System.out.println(1655);
System.out.println(16567);
System.out.println(166701);
System.out.format("%10d%n", 1);
System.out.format("%10d%n", 16);
System.out.format("%10d%n", 1655);
System.out.format("%10d%n", 16567);
System.out.format("%10d%n", 166701);
}
首先,我们在不指定字段宽度的情况下打印五个数字。 输出的宽度等于显示的字符数。 在第二种情况下,我们的字段宽度为 10。五个输出中的每一个的最小长度为 10 个字符。 数字右对齐。
System.out.format("%10d%n", 1);
数字 10 表明字符串输出必须至少有十个字符。
$ java Main.java
1
16
1655
16567
166701
1
16
1655
16567
166701
我们可以看到,在第二种情况下,数字是右对齐的。
Java String format 精度
precision 字段对于不同的转换具有不同的含义。 对于通用参数类型,精度是要写入输出的最大字符数。
void main() {
System.out.format("%.3g%n", 0.0000006);
System.out.format("%.3f%n", 54.34263);
System.out.format("%.3s%n", "ZetCode");
}
精度说明符在三个不同的输出上得到演示。
System.out.format("%.3g%n", 0.0000006);
如果使用 g 转换,则精度是舍入后得到的数量级中的总位数。
System.out.format("%.3f%n", 54.34263);
对于浮点值,精度是小数点分隔符后的位数。
System.out.format("%.3s%n", "ZetCode");
对于字符串,它是打印字符的最大数量。 七个字符中只有三个字符被打印到控制台。
$ java Main.java 6.00e-07 54.343 Zet
Java String format 数字
下一个示例格式化数值数据。
void main() {
System.out.format("%d%n", 12263);
System.out.format("%o%n", 12263);
System.out.format("%x%n", 12263);
System.out.format("%e%n", 0.03452342263);
System.out.format("%d%%%n", 45);
}
该示例演示了数字的标准格式化说明符。
System.out.format("%d%n", 12263);
d 转换说明符会将整数值转换为十进制值。
System.out.format("%o%n", 12263);
o 转换说明符会将数字格式化为八进制基数。
System.out.format("%x%n", 12263);
使用 x 说明符,结果被格式化为十六进制整数。
System.out.format("%e%n", 0.03452342263);
使用 e 说明符,该数字以科学计数法打印。
System.out.format("%d%%%n", 45);
%% 字符用于打印百分号。
$ java Main.java 12263 27747 2fe7 3.452342e-02 45%
Java String format 日期和时间
最后,我们格式化日期和时间数据。
import java.time.LocalDateTime;
void main() {
LocalDateTime ldt = LocalDateTime.now();
System.out.format("%tF%n", ldt);
System.out.format("%tD%n", ldt);
System.out.format("%tT%n", ldt);
System.out.format("%1$tA, %1$tb %1$tY%n", ldt);
System.out.format("%1$td.%1$tm.%1$tY%n", ldt);
}
该示例演示了日期的标准格式化说明符。 日期和时间格式字符串的转换部分以 t 字符开头。
System.out.format("%tF%n", c);
此行使用 tF 转换以完整的 ISO 8601 格式打印日期。
System.out.format("%1$td.%1$tm.%1$tY%n", c);
使用这些格式说明符,我们以斯洛伐克使用的形式打印日期。 各部分由点字符分隔,日位于月之前,月位于年之前。 所有三个格式说明符都引用 c 变量。
$ java Main.java 2024-07-04 07/04/24 13:39:10 Thursday, Jul 2024 04.07.2024
Java 本地化 String format
我们可以将区域设置传递给格式化方法。
import java.time.LocalDate;
import java.util.Locale;
void main() {
double val = 12_568_120.214;
LocalDate now = LocalDate.now();
System.out.printf("%f%n", val);
System.out.printf(Locale.FRENCH, "%f%n", val);
System.out.printf("%tA%n", now);
System.out.printf(Locale.FRENCH, "%tA%n", now);
}
在该示例中,我们以英语和法语区域设置打印值。
$ java Main.java 12568120.214000 12568120,214000 Monday lundi
来源
在本文中,我们格式化了 Java 中的字符串。
作者
列出所有Java教程。