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

public class Test {
public static void main(String[] args) {
String newContent = "\n\tprivate String {0};\n" +
"\tprivate boolean modify_{0} = false;\n" +
"\n" +
"\tpublic String {1}() '{'\n" +
"\t\treturn {0};\n" +
"\t'}'\n" +
"\n" +
"\tpublic void setBDCDYID(String {0}) '{'\n" +
"\t\tif (this.{0} != {0}) '{'\n" +
"\t\t\tthis.{0} = {0};\n" +
"\t\t\tmodify_{0} = true;\n" +
"\t\t'}'\n" +
"\t'}'";
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

posted @ 2020-08-09 13:25  覃上  阅读(449)  评论(0编辑  收藏  举报