介绍
在 Java 中,String.format() 方法通过结合格式化字符串、区域设置(locale)和参数来格式化字符串。格式化字符串指定了参数应转换成的格式,而区域设置则指定了应使用的任何设置,例如语言或地区。在本实验中,我们将学习如何使用 String.format() 方法来格式化字符串。
创建一个简单的字符串格式化示例
在这一步中,我们将创建一个使用 String.format() 方法格式化字符串的基本示例。
- 在
~/project目录下创建一个名为SimpleFormatExample.java的新 Java 文件。 - 在
SimpleFormatExample类的main()方法中,创建一个名为formattedString的字符串变量,并将其设置为调用String.format()的结果,参数如下:- 字符串
"Hello, %s!"作为格式化字符串。 - 字符串
"world"作为参数。
- 字符串
- 使用
System.out.println()打印formattedString的值。 - 保存并关闭
SimpleFormatExample.java文件。 - 在终端中,通过运行以下命令编译
SimpleFormatExample.java文件:javac SimpleFormatExample.java。 - 然后,使用命令
java SimpleFormatExample运行生成的类文件。
以下是代码:
public class SimpleFormatExample {
public static void main(String[] args) {
String formattedString = String.format("Hello, %s!", "world");
System.out.println(formattedString);
}
}
当你运行代码时,应该会看到以下输出:
Hello, world!
在格式化字符串中使用参数
在这一步中,我们将学习如何在格式化字符串中使用参数来自定义输出。
- 在编辑器中打开
SimpleFormatExample.java文件。 - 修改
formattedString变量,使用参数来指定显示的字符数。将格式化字符串中的"s"替换为".%d-s",以指定要显示的最大字符数。 - 在格式化字符串后添加一个整数参数,表示要显示的最大字符数。
- 保存并关闭
SimpleFormatExample.java文件。 - 在终端中,通过运行以下命令编译
SimpleFormatExample.java文件:javac SimpleFormatExample.java。 - 然后,使用命令
java SimpleFormatExample运行生成的类文件。
以下是修改后的代码:
public class SimpleFormatExample {
public static void main(String[] args) {
String formattedString = String.format("Hello, %d-s!", 5);
System.out.println(formattedString);
}
}
当你运行代码时,应该会看到以下输出:
Hello, 5-s!
使用标志修改格式化行为
在这一步中,我们将学习如何使用标志符来修改格式化字符串的行为。
- 在编辑器中打开
SimpleFormatExample.java文件。 - 修改格式化字符串,通过在
%符号后添加减号(-)来实现文本的左对齐。 - 在格式化字符串中添加一个参数,以指定输出的最小宽度。
- 保存并关闭
SimpleFormatExample.java文件。 - 在终端中,通过运行以下命令编译
SimpleFormatExample.java文件:javac SimpleFormatExample.java。 - 然后,使用命令
java SimpleFormatExample运行生成的类文件。
以下是修改后的代码:
public class SimpleFormatExample {
public static void main(String[] args) {
String formattedString = String.format("%-10s, world!", "Hello");
System.out.println(formattedString);
}
}
当你运行代码时,应该会看到以下输出:
Hello , world!
使用转换字符格式化不同数据类型
在这一步中,我们将学习如何使用不同的转换字符来格式化不同的数据类型。
- 在编辑器中打开
SimpleFormatExample.java文件。 - 修改格式化字符串,使用
%f转换字符来包含一个浮点数。 - 在格式化字符串后添加一个浮点数参数。
- 保存并关闭
SimpleFormatExample.java文件。 - 在终端中,通过运行以下命令编译
SimpleFormatExample.java文件:javac SimpleFormatExample.java。 - 然后,使用命令
java SimpleFormatExample运行生成的类文件。
以下是修改后的代码:
public class SimpleFormatExample {
public static void main(String[] args) {
String formattedString = String.format("The value of pi is approximately %f", 3.14159265359);
System.out.println(formattedString);
}
}
当你运行代码时,应该会看到以下输出:
The value of pi is approximately 3.141593
使用精度修饰符控制数字位数
在这一步中,我们将学习如何使用精度修饰符来控制格式化输出中显示的数字位数。
- 在编辑器中打开
SimpleFormatExample.java文件。 - 修改格式化字符串,使用
%f转换字符来包含一个浮点数。添加精度修饰符(%.2f)以仅显示小数点后两位。 - 在格式化字符串后添加一个浮点数参数。
- 保存并关闭
SimpleFormatExample.java文件。 - 在终端中,通过运行以下命令编译
SimpleFormatExample.java文件:javac SimpleFormatExample.java。 - 然后,使用命令
java SimpleFormatExample运行生成的类文件。
以下是修改后的代码:
public class SimpleFormatExample {
public static void main(String[] args) {
String formattedString = String.format("The value of pi is approximately %.2f", 3.14159265359);
System.out.println(formattedString);
}
}
当你运行代码时,应该会看到以下输出:
The value of pi is approximately 3.14
使用 Locales 自定义格式化设置
在这一步中,我们将学习如何使用 locales 自定义格式化设置,例如语言或区域。
- 在编辑器中打开
SimpleFormatExample.java文件。 - 修改格式化字符串,使用
%d转换字符来包含一个数字。 - 在格式化字符串后添加一个整数参数。
- 在
String.format()方法中添加第二个参数,以指定要使用的 locale。使用Locale.FRENCH常量来指定法语。 - 保存并关闭
SimpleFormatExample.java文件。 - 在终端中,通过运行以下命令编译
SimpleFormatExample.java文件:javac SimpleFormatExample.java。 - 然后,使用命令
java SimpleFormatExample运行生成的类文件。
以下是修改后的代码:
import java.util.Locale;
public class SimpleFormatExample {
public static void main(String[] args) {
String formattedString = String.format(Locale.FRENCH, "Le nombre est %d", 123456789);
System.out.println(formattedString);
}
}
当你运行代码时,应该会看到以下输出:
Le nombre est 123456789
格式化日期和时间值
在这一步中,我们将学习如何使用 String.format() 方法来格式化日期和时间值。
- 在编辑器中打开
SimpleFormatExample.java文件。 - 添加
java.util.Calendar类的导入语句,我们将使用它来创建一个日历实例。 - 使用
Calendar.getInstance()创建一个新的Calendar实例。 - 修改格式化字符串,使用
%tD转换字符来包含一个日期。 - 在格式化字符串后添加一个
Date参数,使用calendar.getTime()方法获取Date值。 - 保存并关闭
SimpleFormatExample.java文件。 - 在终端中,通过运行以下命令编译
SimpleFormatExample.java文件:javac SimpleFormatExample.java。 - 然后,使用命令
java SimpleFormatExample运行生成的类文件。
以下是修改后的代码:
import java.util.Calendar;
import java.util.Date;
public class SimpleFormatExample {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
String formattedString = String.format("%tD", calendar.getTime());
System.out.println(formattedString);
}
}
当你运行代码时,应该会看到以下输出:
08/22/21
使用大写和小写进行格式化
在这一步中,我们将学习如何使用 %S 和 %s 转换字符分别将字符串格式化为大写和小写。
- 在编辑器中打开
SimpleFormatExample.java文件。 - 修改格式化字符串,使用
%S转换字符将字符串格式化为大写。 - 修改格式化字符串,使用
%s转换字符将第二个字符串格式化为小写。 - 保存并关闭
SimpleFormatExample.java文件。 - 在终端中,通过运行以下命令编译
SimpleFormatExample.java文件:javac SimpleFormatExample.java。 - 然后,使用命令
java SimpleFormatExample运行生成的类文件。
以下是修改后的代码:
public class SimpleFormatExample {
public static void main(String[] args) {
String formattedString = String.format("The word %S is in uppercase and %s is in lowercase", "HELLO", "world");
System.out.println(formattedString);
}
}
当你运行代码时,应该会看到以下输出:
The word HELLO is in uppercase and world is in lowercase
异常处理
在这一步中,我们将学习使用 String.format() 方法时可能抛出的异常,以及如何处理它们。
- 在编辑器中打开
SimpleFormatExample.java文件。 - 修改格式化字符串,使用
%c转换字符来包含一个字符。 - 在格式化字符串后添加一个字符参数。
- 保存并关闭
SimpleFormatExample.java文件。 - 在终端中,通过运行以下命令编译
SimpleFormatExample.java文件:javac SimpleFormatExample.java。 - 然后,使用命令
java SimpleFormatExample运行生成的类文件。
以下是修改后的代码:
public class SimpleFormatExample {
public static void main(String[] args) {
try {
String formattedString = String.format("The character is %c", "too long");
System.out.println(formattedString);
} catch (Exception e) {
System.out.println("An exception occurred: " + e.getMessage());
}
}
}
当你运行代码时,应该会看到以下输出:
An exception occurred: UString.format: '%c' != java.base/java.lang.String
总结
在本实验中,我们学习了如何在 Java 中使用 String.format() 方法来格式化字符串。我们从一个简单的示例开始,使用格式化字符串和参数生成字符串。然后,我们学习了如何使用不同的转换字符来格式化字符串,以及如何使用精度修饰符和标志来控制输出。我们还学习了如何使用 locales 自定义格式化设置,以及如何格式化日期和时间值。最后,我们讨论了使用 String.format() 方法时可能抛出的一些异常以及如何处理它们。



