介绍
在 Java 中,String.format()
方法通过结合格式化字符串、区域设置(locale)和参数来格式化字符串。格式化字符串指定了参数应转换成的格式,而区域设置则指定了应使用的任何设置,例如语言或地区。在本实验中,我们将学习如何使用 String.format()
方法来格式化字符串。
在 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 自定义格式化设置,例如语言或区域。
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()
方法时可能抛出的一些异常以及如何处理它们。