import java.text.MessageFormat; {} 包裹。例如:String template = "Hello, {0}! Your age is {1}."; 在这个例子中,{0} 和 {1} 是占位符,它们分别表示第一个和第二个参数。
String name = "John"; int age = 30; String formattedString = MessageFormat.format(template, name, age); System.out.println(formattedString); // 输出:Hello, John! Your age is 30. 这就是如何使用 Java MessageFormat 来格式化字符串的基本方法。你可以根据需要使用更多的占位符和参数来构建复杂的格式化字符串。