String s1 = "a";
String s2 = "b";
String s3 = "c";
String s4 = null;
System.out.println(MessageFormat.format("{0},{0},{0}", s1,s2,s3));
System.out.println(MessageFormat.format("{0},{1},{2}", s1,s2,s3));
System.out.println(MessageFormat.format("{0},{1},{2}", s1,s2,s3));
System.out.println(MessageFormat.format("{0},'{1}',''{2}''", s1,s2,s3));
System.out.println(MessageFormat.format("{0}", s4));
System.out.println(MessageFormat.format("{0},{1}", StringUtils.defaultIfBlank(s4,"空的"), StringUtils.defaultIfBlank(s1,"空的")));
输出:
a,a,a
a,b,c
a,b,c
a,{1},'c'
null
空的,a
类引入
import java.text.MessageFormat;
import org.apache.commons.lang3.StringUtils;