MessageFormat
可能是您正在寻找的:
final MessageFormat format = new MessageFormat("Hi {0}, you are {1, number, #} years old today!");
final String expanded = format.format(new Object[]{person.getName(), person.getAge()});
还有一个像这样的C String.format
:
final String expanded = String.format("Hi %1s, you are %2s years old today!", person.getName(), person.getAge());
测试:
public static void main(String[] args) {
final MessageFormat format = new MessageFormat("Hi {0}, you are {1,number,#} years old today!");
System.out.println(format.format(new Object[]{"Name", 15}));
System.out.println(String.format("Hi %1s, you are %2s years old today!", "Name", 15));
}
输出:
Hi Name, you are 15 years old today!
Hi Name, you are 15 years old today!