67,515
社区成员
发帖
与我相关
我的任务
分享
String str = MessageFormat.format("我是{0},我来自{1},今年{2}岁", "中国人", "北京", "22");
public static void main(String[] args){
String str="我是{0},我来自{1},今年{2}岁";
String[] arr={"中国人","北京","22"};
System.out.println(fillStringByArgs(str, arr));
}
private static String fillStringByArgs(String str,String[] arr){
Matcher m=Pattern.compile("\\{(\\d)\\}").matcher(str);
while(m.find()){
str=str.replace(m.group(),arr[Integer.parseInt(m.group(1))]);
}
return str;
}