字符串格式化之String.format

目录

String.format方法的两种重载形式

格式化参数,format模板(占位符)语法及示例

conversion:转换字符

argument_index$:指定要使用【Object... args】中的哪一个参数作为此处占位符的原始参数

flags:指定格式化标识,此标识的作用有:填充、对齐、显示符号……

width:宽度

.precision:精确度


嘿!String.format!我盯上你已经很久了!


String.format方法的两种重载形式

  1. public static String format(String format, Object... args)
  2. public static String format(Locale l, String format, Object... args)

参数说明:
        Locale l:指定特定的语言环境。
        String format:含有一个或多个占位符的字符串模板。
        Object... args:用来传入format中对应占位符处的原始参数,当然会根据format中的模板语法进行相应处理。


格式化参数,format模板(占位符)语法及示例

%[argument_index$][flags][width][.precision]conversion

conversion:转换字符

常规类型

转换字符

类别

说明

'b', 'B'

常规

如果参数 arg 为 null,则结果为 "false"。如果 arg 是一个 boolean 值或 Boolean,则结果为 String.valueOf() 返回的字符串。否则结果为 "true"。

'h', 'H'

常规

如果参数 arg 为 null,则结果为 "null"。否则,结果为调用 Integer.toHexString(arg.hashCode()) 得到的结果。

's', 'S'

常规

如果参数 arg 为 null,则结果为 "null"。如果 arg 实现 Formattable,则调用 arg.formatTo。否则,结果为调用 arg.toString() 得到的结果。

'c', 'C'

字符

结果是一个 Unicode 字符

'd'

整数

结果被格式化为十进制整数

'o'

整数

结果被格式化为八进制整数

'x', 'X'

整数

结果被格式化为十六进制整数

'e', 'E'

浮点

结果被格式化为用计算机科学记数法表示的十进制数

'f'

浮点

结果被格式化为十进制数

'g', 'G'

浮点

根据精度和舍入运算后的值,使用计算机科学记数形式或十进制格式对结果进行格式化。

'a', 'A'

浮点

结果被格式化为带有效位数和指数的十六进制浮点数

'%'

百分比

结果为字面值 '%' ('\u0025')

'n'

行分隔符

结果为特定于平台的行分隔符

't', 'T'

时间日期转换符前缀

在使用时间/日期占位符是,需要使用此前缀。如:

。具体示例见下面的【时间/日期】部分。

……

辅助理解:

// 输出【false, true, true】
System.out.println(String.format("%b, %b, %b", null, true, 123));

// 输出【null, 20】
System.out.println(String.format("%h, %h", null, 32));

// 输出【null, 123】
System.out.println(String.format("%s, %s", null, 123));

// 输出【a】
System.out.println(String.format("%c", 'a'));

// 输出【9527】
System.out.println(String.format("%d", 9527));

// 输出【20】
System.out.println(String.format("%o", 16));

// 输出【10】
System.out.println(String.format("%x", 16));

// 输出【1.321646e+04】
System.out.println(String.format("%e", 13216.46));

// 输出【16.524000】
System.out.println(String.format("%f", 16.524));

// 输出【1123.57】
System.out.println(String.format("%g", 1123.5669));

// 输出【0x1.0p5】
System.out.println(String.format("%a", 32.0));

// 输出【54.17%】
// 注: %.2f 对应 54.17; %% 对应 %
System.out.println(String.format("%.2f%%", 54.167));

// 输出
//      A
//      B
System.out.println(String.format("A%nB"));

时间/日期:

格式化时间

转换字符

说明

'H'

24 小时制的小时,被格式化为必要时带前导零的两位数,即 00 - 23。

'I'

12 小时制的小时,被格式化为必要时带前带零的两位数,即 01 - 12。

'k'

24 小时制的小时,即 0 - 23。

'l'

12 小时制的小时,即 1 - 12。

'M'

小时中的分钟,被格式化为必要时带前导零的两位数,即 00 - 59。

'S'

分钟中的秒,被格式化为必要时带前导零的两位数,即 00 - 60 ("60" 是支持闰秒所需的一个特殊值)。

'L'

秒中的毫秒,被格式化为必要时带前导零的三位数,即 000 - 999。

'N'

秒中的纳秒,被格式化为必要时带前导零的九位数,即 000000000 - 999999999。

'p'

特定于语言环境的 上午或下午 标记以小写形式表示,例如 "am" 或 "pm"。使用转换前缀 'T' 可以强行将此输出转换为大写形式。

'z'

相对于 GMT 的 RFC 822 格式的数字时区偏移量,例如 +0800。

'Z'

表示时区缩写形式的字符串。Formatter 的语言环境将取代参数的语言环境(如果有)。

's'

自协调世界时 (UTC) 1970 年 1 月 1 日 00:00:00 至现在所经过的秒数。

'Q'

自协调世界时 (UTC) 1970 年 1 月 1 日 00:00:00 至现在所经过的毫秒数。

格式化日期

转换字符

说明

'B'

特定于语言环境的月份全称,例如 "January" 和 "February"。

'b'

特定于语言环境的月份简称,例如 "Jan" 和 "Feb"。

'h'

与 'b' 相同。

'A'

特定于语言环境的星期几全称,例如 "Sunday" 和 "Monday"

'a'

特定于语言环境的星期几简称,例如 "Sun" 和 "Mon"

'C'

除以 100 的四位数表示的年份,被格式化为必要时带前导零的两位数,即 00 - 99

'Y'

年份,被格式化为必要时带前导零的四位数(至少),例如,0092 等于格里高利历的 92 CE。

'y'

年份的最后两位数,被格式化为必要时带前导零的两位数,即 00 - 99。

'j'

当前天是一年中的第几天,被格式化为必要时带前导零的三位数,例如,对于格里高利历是 001 - 366。

'm'

月份,被格式化为必要时带前导零的两位数,即 01 - 13。

'd'

当前天是一个月中的第几天,被格式化为必要时带前导零两位数,即 01 - 31。

'e'

当前天是一个月中的第几天,被格式化为两位数,即 1 - 31。

格式化日期/时间组合

转换字符

说明

'R'

24 小时制的时间,被格式化为 "%tH:%tM"

'T'

24 小时制的时间,被格式化为 "%tH:%tM:%tS"。

'r'

12 小时制的时间,被格式化为 "%tI:%tM:%tS %Tp"。上午或下午标记 ('%Tp') 的位置可能与语言环境有关。

'D'

日期,被格式化为 "%tm/%td/%ty"。

'F'

ISO 8601 格式的完整日期,被格式化为 "%tY-%tm-%td"。

'c'

日期和时间,被格式化为 "%ta %tb %td %tT %tZ %tY",例如 "Sun Jul 20 16:17:00 EDT 1969"。

……

辅助理解(格式化时间):

// 输出【19】
// 注: 当前时间点为: 2019-03-13 19:09
System.out.println(String.format("%tH", new Date()));

// 输出【07】
// 注: 当前时间点为: 2019-03-13 19:09
System.out.println(String.format("%tI", new Date()));

// 输出【19】
// 注: 当前时间点为: 2019-03-13 19:09
System.out.println(String.format("%tk", new Date()));

// 输出【7】
// 注: 当前时间点为: 2019-03-13 19:09
System.out.println(String.format("%tl", new Date()));

// 输出【09】
// 注: 当前时间点为: 2019-03-13 19:09
System.out.println(String.format("%tM", new Date()));

// 输出【12】
// 注: 当前时间点为: 2019-03-13 19:09:12
System.out.println(String.format("%tS", new Date()));

// 输出【495】
// 注: 当前时间点为: 2019-03-13 19:09:12.495
System.out.println(String.format("%tL", new Date()));

// 输出【496000000】
// 注: 当前时间点为: 2019-03-13 19:09:12.496000000
System.out.println(String.format("%tN", new Date()));

// 输出【下午】
// 注: 当前时间点为: 2019-03-13 19:09
System.out.println(String.format("%tp", new Date()));

// 输出【+0800】
// 注: 当前时间点为: 2019-03-13 19:09
System.out.println(String.format("%tz", new Date()));

// 输出【CST】
// 注: 当前时间点为: 2019-03-13 19:09
System.out.println(String.format("%tZ", new Date()));

// 输出【1552475352】
// 注: 当前时间点为: 2019-03-13 19:09
System.out.println(String.format("%ts", new Date()));

// 输出【1552475352503】
// 注: 当前时间点为: 2019-03-13 19:09
System.out.println(String.format("%tQ", new Date()));

辅助理解(格式化时间):

// 输出【三月】
// 注: 当前时间点为: 2019-03-13 19:19
System.out.println(String.format("%tB", new Date()));

// 输出【三月】
// 注: 当前时间点为: 2019-03-13 19:19
System.out.println(String.format("%tb", new Date()));

// 输出【三月】
// 注: 当前时间点为: 2019-03-13 19:19
System.out.println(String.format("%th", new Date()));

// 输出【星期三】
// 注: 当前时间点为: 2019-03-13 19:19
System.out.println(String.format("%tA", new Date()));

// 输出【星期三】
// 注: 当前时间点为: 2019-03-13 19:19
System.out.println(String.format("%ta", new Date()));

// 输出【20】
// 注: 当前时间点为: 2019-03-13 19:19
System.out.println(String.format("%tC", new Date()));

// 输出【2019】
// 注: 当前时间点为: 2019-03-13 19:19
System.out.println(String.format("%tY", new Date()));

// 输出【19】
// 注: 当前时间点为: 2019-03-13 19:19
System.out.println(String.format("%ty", new Date()));

// 输出【072】
// 注: 当前时间点为: 2019-03-13 19:19
System.out.println(String.format("%tj", new Date()));

// 输出【03】
// 注: 当前时间点为: 2019-03-13 19:19
System.out.println(String.format("%tm", new Date()));

// 输出【13】
// 注: 当前时间点为: 2019-03-13 19:19
System.out.println(String.format("%td", new Date()));

// 输出【13】
// 注: 当前时间点为: 2019-03-13 19:19
System.out.println(String.format("%te", new Date()));

辅助理解(格式化时间):

// 输出【19:31】
// 注: 当前时间点为: 2019-03-13 19:31:49
System.out.println(String.format("%tR", new Date()));

// 输出【19:31:49】
// 注: 当前时间点为: 2019-03-13 19:31:49
System.out.println(String.format("%tT", new Date()));

// 输出【07:31:49 下午】
// 注: 当前时间点为: 2019-03-13 19:31:49
System.out.println(String.format("%tr", new Date()));

// 输出【03/13/19】
// 注: 当前时间点为: 2019-03-13 19:31:49
System.out.println(String.format("%tD", new Date()));

// 输出【2019-03-13】
// 注: 当前时间点为: 2019-03-13 19:31:49
System.out.println(String.format("%tF", new Date()));

// 输出【星期三 三月 13 19:31:49 CST 2019】
// 注: 当前时间点为: 2019-03-13 19:31:49
System.out.println(String.format("%tc", new Date()));

argument_index$:指定要使用【Object... args】中的哪一个参数
                                  作为此处占位符的原始参数

注:argument_index$使用方式形如1$2$3$……

注:argument_index索引从1开始。

注:如果不显式的指定argument_index$的话,那么默认采用【Object... args】中位置与各个占位符位置对应的参数。

注:argument_index$处可以写0$,其效果就等于没写argument_index$一样,会走默认的方式。

辅助理解:

// 输出 100, 500
System.out.println(String.format("%d, %d", 100, 500));

// 输出 500, 100
System.out.println(String.format("%2$d, %1$d", 100, 500));

flags:指定格式化标识,此标识的作用有:填充、对齐、显示符号……

合法的flags有:

辅助理解:

// 输出【100, 500】
System.out.println(String.format("%d, %d", 100, 500));

// 输出【100       , 500】
// 注: -10表示预留10个位置给参数,并且左对齐;
System.out.println(String.format("%-10d, %d", 100, 500));

// 输出【0x20, 040】
// 将十进制转换为对其他进制数。如这里分别转换为16进制 和 8进制
System.out.println(String.format("%#x, %#o", 32, 32));

// 输出【+100, -500】
System.out.println(String.format("%+d, %+d", 100, -500));

// 输出【>-100, > 100】
System.out.println(String.format(">% d, >% d", -100, 100));

// 输出【100, 0100】
System.out.println(String.format("%d, %04d", 100, 100));

// 输出【10,000, 1,000.123000】
System.out.println(String.format("%,d, %,f", 10000, 1000.123));

// 输出【100, (100)】
System.out.println(String.format("%(d, %(d", 100, -100));

width:宽度

      The width is the minimum number of characters to be written to the output. For the line separator conversion, width is not applicable; if it is provided, an exception will be thrown.

辅助理解:

// 输出【> 100, >100】
// 注: 4表示预留10个位置给参数,并且右对齐;
System.out.println(String.format(">%4d, >%d", 100, 100));

.precision精确度

        对于常规参数类型,精度是将向输出中写入的最多字符数。

        对于浮点转换 'e'、'E' 和 'f',精度是小数点分隔符后的位数。如果转换是 'g' 或 'G',那么精度是舍入计算后所得数值的所有位数。如果转换是 'a' 或 'A',则不必指定精度。

        对于字符、整数和日期/时间参数类型转换,以及百分比和行分隔符转换,精度是不适用的;如果提供精度,则会抛出异常。

辅助理解:

// 输出【521.55, 5.22e+02】
// 注: 四舍五入。
System.out.println(String.format("%.2f, %.2e", 521.54567, 521.54567));

 

^_^ 如有不当之处,欢迎指正

^_^ 参考文档
             
[Java参考文档].JDK_API_1_6_zh_CN.CHM

^_^ 参考链接
               
https://blog.csdn.net/lonely_fireworks/article/details/7962171/

^_^ 本文已经被收录进《程序员成长笔记(四)》,笔者JustryDeng

  • 4
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值