java中strings创建格式字符串

类String有一个等效的类方法 ,format()它返回一个String对象而不是一个PrintStream对象。

使用String的静态format()方法允许您创建可以重用的格式化字符串,而不是一次性打印语句。

例如:

System.out.printf("The value of the float " +
                  "variable is %f, while " +
                  "the value of the " + 
                  "integer variable is %d, " +
                  "and the string is %s", 
                  floatVar, intVar, stringVar); 

你可以写:

String fs;
fs = String.format("The value of the float " +
                   "variable is %f, while " +
                   "the value of the " + 
                   "integer variable is %d, " +
                   " and the string is %s",
                   floatVar, intVar, stringVar);
System.out.println(fs);
java中strings创建格式字符串

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注