import java.text.MessageFormat;
public class MessageFormatExample {
public static void main(String[] args) {
// 定义一个消息模板,其中 {0} 和 {1} 是占位符
String pattern = "Hello, {0}! Your appointment is on {1}.";
// 使用 MessageFormat.format 方法来格式化消息
String message = MessageFormat.format(pattern, "John Doe", "2023-10-15");
// 输出格式化后的消息
System.out.println(message);
}
}
MessageFormat
是 Java 中用于格式化字符串的类,它支持复杂的模式和参数替换。{0}
和 {1}
的模板字符串 pattern
。MessageFormat.format
方法将实际的参数值(如 "John Doe"
和 "2023-10-15"
)替换到模板中的占位符位置。Hello, John Doe! Your appointment is on 2023-10-15.
下一篇:java反射获取class对象
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站