字符串用占位符拼接 String ms = MessageFormat.format("{0,number,#.##}", 3.656);

public class Test {
public static void main(String[] args) {
String newContent = " private String {0}; " +
" private boolean modify_{0} = false; " +
" " +
" public String {1}() '{' " +
" return {0}; " +
" '}' " +
" " +
" public void setBDCDYID(String {0}) '{' " +
" if (this.{0} != {0}) '{' " +
" this.{0} = {0}; " +
" modify_{0} = true; " +
" '}' " +
" '}'";
String dd = MessageFormat.format(newContent,"bdcdyic","setBdcdyid");
System.out.println(dd);
    //特殊符号{ } 需要用左右单引号围起来 '{' '}',否则和占位符冲突
System.out.println(MessageFormat.format("{1} '{' '}' hello {0} {1}","world","lili"));
}

private String id;
public void setId(String id){
this.id = id;
}
}

第二个参数可选。

String ms = MessageFormat.format("{0,number,#.##}", 3.656);

若格式不匹配则报异常  如第二个参数限制为数值  但内容为 String

参考 https://blog.csdn.net/Mr_Tony/article/details/50175447?utm_source=blogxgwz9

原文地址:https://www.cnblogs.com/shihx/p/13462412.html