一直对String.format不了解,而且也记不住那么多,记录一下以后查起来方便
1 占位符
1.1 单个占位符
String s = String.format("Hello %s,Welcome...12
1.2 多个占位符
1.2.1 按顺序
String s = String.format("Hello %s%s%s", "jerry-", "li", ",welcome!")...;1
1.2.2 按索引
String s = String.format("Hello %2$s%3$s%1$s", "li-", "heng-", "jie");1
2 对整数格式化
int n2...空格 正数前加空格
, 只用与十进制
( 若结果为负数
3 对浮点数格式化
double n = 3.1415926;
String s = String.format("%f %n", num);...如下:
Date date = new Date();
String dateFormat = String.format("%tF %tT ", date, date);
System.out.println