String.format方法使用

Easter79
• 阅读 521

转自  https://blog.csdn.net/u010137760/article/details/82869637 

1.代码中简单使用
2.源码调用的方法
3.相关类-Formatter
3.1可选的参数索引
3.2可选的标记
3.3可选的宽度
3.4可选的精度
3.5强制类型转换
3.1非日期/时间转换类型
3.1.1字符串转换
3.1.2字符转换
3.1.3整数转换
3.1.4浮点数转换
3.1.5布尔值转换
3.1.6hash值转换
3.1.7无参转换
3.2日期/时间转换
1.代码中简单使用
String.format("%.2f", 2.0f);
1
1
2.源码调用的方法
* Returns a localized formatted string, using the supplied format and arguments,
* 返回本地化格式的字符串,使用提供的格式和参数
* using the user's default locale.
* 使用用户默认的本地语言环境
*

If you're formatting a string other than for human
* consumption, you should use the {@code format(Locale, String, Object...)}
* 格式化字符串如果使用其他语言环境,你应该使用format(Locale, String, Object...)
* overload and supply {@code Locale.US}. See
* "Be wary of the default locale".
*
* @param format the format string (see {@link java.util.Formatter#format})
* @param args
* the list of arguments passed to the formatter. If there are
* more arguments than required by {@code format},
* additional arguments are ignored.
* @return the formatted string.
* @throws NullPointerException if {@code format == null}
* @throws java.util.IllegalFormatException
* if the format is invalid.
* @since 1.5
*/
public static String format(String format, Object... args) {
return format(Locale.getDefault(), format, args);
}

public static String format(Locale locale, String format, Object... args) {
if (format == null) {
throw new NullPointerException("format == null");
}
int bufferSize = format.length() + (args == null ? 0 : args.length * 10);
Formatter f = new Formatter(new StringBuilder(bufferSize), locale);
return f.format(format, args).toString();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
3.相关类-Formatter
在Formatter类的释义中有如下信息:

Format strings consist of plain text interspersed with format specifiers, such as {@code “name: %s weight: %03dkg\n”}. Being a Java string, the usual Java string literal backslash escapes are of course available.

格式字符串由纯文本组成,其中穿插了格式说明符,如"name: %s weight: %03dkg\n"中的%s、%03d。作为java字符串,反斜杠转移符也是支持的

Format specifiers (such as {@code “%s”} or {@code “%03d”} in the example) start with a {@code %} and describe how to format their corresponding argument. It includes an optional argument index, optional flags, an optional width, an optional precision, and a mandatory conversion type.

格式说明符以%开头,描述如何格式化相应参数。格式说明符包括可选的参数索引、可选的标记、可选的宽度、可选的精度、强制类型转换

3.1可选的参数索引
标记 作用 例子 结果
%2$s中的2指定显示第2个参数 format("%2s, s,%1s,s",“小明”,“小李”) 小李,小明
< 复用同一个参数 format("%o %<d %<x", 64); 100 64 40
3.2可选的标记
标记 作用 例子 结果
, 分割大数据 format("%,d", 1024); 1,234
+ 总是显示正数、负数符号 format("%+d, %+4d", -5, 5); -5,   +5
非负数有一个前导空格 String.format(“x% d% 5d”, 4, -4); x 4   -4
( 把括号括在负数上 format("%(d, %(d, %(6d", 12, -12, -12); 12, (12),   (12)
- 对齐
需要宽度;
-默认右对齐,-号左对齐; format("%-6dx", 5);
format("%-3C,%3C", ‘d’, 0x65); 5     x
D  ,  E
0 用前导零填充数字 format("%07d, %03d", 4, 5555); 0000004, 5555
# 变形
仅八进制、十六进制; format("%o %#o %d", 010, 010,010);
format("%x %#x %d", 0x12, 0x12,0x12); 10 010 8
12 0x12 18
3.3可选的宽度
The width is a decimal integer specifying the minimum number of characters to be used to represent the argument.

宽度是一个十进制整数,指定参数显示的最小字符数。
注:不能使用宽度来截断字段

标记 作用 例子 结果
5前面有两个空格 format("%3d",5);   5
3.4可选的精度
The precision is a {@code .} followed by a decimal integer

精度是.后面跟的整形数字

标记 作用 例子 结果
. 小数点后面跟几位 format("%.1f", 123.456f); 123.5
3.5强制类型转换
3.1非日期/时间转换类型
3.1.1字符串转换
标记 作用 例子 结果
s 字符串 format("%s %s", “hello”, “Hello”); hello Hello
S 大写字符串 format("%S %S", “hello”, “Hello”); HELLO HELLO
3.1.2字符转换
标记 作用 例子 结果
c 字符 format("%c %c", ‘d’, ‘E’); d E
C 大写字符 format("%C %C", ‘d’, ‘E’); D E
3.1.3整数转换
标记 作用 例子 结果
d 十进制 format("%d", 26); 26
o 八进制 format("%o", 032); 32
x 十六进制 format("%x %X", 0x1a, 0x1a); 1a 1A
3.1.4浮点数转换
标记 作用 例子 结果
f 十进制小数 format("%f", 123.456f);
format("%.1f", 123.456f);
format("%1.5f", 123.456f);
format("%10f", 123.456f);
format("%6.0f", 123.456f); 123.456001
123.5
123.45600
123.456001
   123
e,E 指数浮点数 format("%e", 123.456f);
format("%.1e", 123.456f);
format("%1.5E", 123.456f);
format("%10E", 123.456f);
format("%6.0E", 123.456f); 1.234560e+02
1.2e+02
1.23456E+02
1.234560E+02
 1E+02
g,G 十进制或指数取决于数的大小 format("%g %g", 0.123, 0.0000123); 0.123000 1.23000e-05
a,A 十六进制小数 format("%a", 123.456f); 0x1.edd2f2p6
3.1.5布尔值转换
支持Boolean值。null被认为是false,其他类型实例为true

标记 作用 例子 结果
b,B 布尔值 format("%b %b", true, false);
format("%B %B", true, false);
format("%b", null);
format("%b", “hello”); true false
TRUE FALSE
false
true
3.1.6hash值转换
调用参数的hashCode方法,支持所有类型

标记 作用 例子 结果
h,H 十六进制hashcode format("%h", this);
format("%H", this);
format("%h", null); 190d11
190D11
null
3.1.7无参转换
标记 作用 例子 结果
% 输出%字符 format("%d%%", 50); 50%
n 换行 format(“first%nsecond”); first\nsecond
3.2日期/时间转换
日历、日期、毫秒值均可以作为日期/时间参数。如果类型有错误,则返回1970-01-01 00:00:00 UTC

标记 作用 例子 结果
ta 周内第几天(缩写) format("%ta", cal, cal); Tue
tA 周内第几天(全) format("%tA", cal, cal); Tuesday
tb 月份第几月(缩写)) format("%tb", cal); Apr
tB 月份第几月(全) format("%tB", cal); April
tc 时间,请勿使用 format("%tc", cal); Tue Apr 01 16:19:17 CEST 2008
tC 世纪,两位数 format("%tC", cal); 20
td 月份第几天,两位数 format("%td", cal); 01
tD 美国日期格式,请勿使用 format("%tD", cal); 04/01/08
te 月中的1天(1-31) format("%te", cal); 1
tF ISO 8601标准下的全面日期格式(YYYY-MM-DD) format("%tF", cal); 2008-04-01
th %tb的同义词
tH 2位数表示小时,24小时制(00-23) format("%tH", cal); 16
tI 2位数表示小时,12小时制(01-12) format("%tI", cal); 04
tj 三位数表示一年中的第几天(001-366) format("%tj", cal); 092
tk 表示小时,24小时制(0-23) format("%tk", cal); 16
tl 表示小时,12小时制(1-12) format("%tl", cal); 4
tL 毫秒数 format("%tL", cal); 359
tm 2位数一年中的第几个月(01-12) format("%tm", cal); 04
tM 2位数分钟 format("%tM", cal); 08
tN 纳秒 format("%tN", cal); 359000000
tp 上午或下午 format("%tp %Tp", cal, cal); pm PM
tQ 自时代以来的毫秒值 format("%tQ", cal); 1207059412656
tr 完整的12小时时间 %tI:%tM:%tS %Tp format("%tr", cal); 04:15:32 PM
tR 短时24小时制 format("%tR", cal); 16:15
ts 自时代以来的秒值 format("%ts", cal); 1207059412
tS 两位数秒(00-60) format("%tS", cal); 17
tT 完整24小时制 %tH:%tM:%tS format("%tT", cal); 16:15:32
ty 两位数年(00-99) format("%ty", cal); 08
tY 四位数年 format("%tY", cal); 2008
tz 时间区格林尼治时间偏移 format("%tz", cal); +0100
tZ 本地时区缩写 format("%tZ", cal); CEST

---------------------
作者:洛漠O_o
来源:CSDN
原文:https://blog.csdn.net/u010137760/article/details/82869637
版权声明:本文为博主原创文章,转载请附上博文链接!

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Wesley13 Wesley13
2年前
java将前端的json数组字符串转换为列表
记录下在前端通过ajax提交了一个json数组的字符串,在后端如何转换为列表。前端数据转化与请求varcontracts{id:'1',name:'yanggb合同1'},{id:'2',name:'yanggb合同2'},{id:'3',name:'yang
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Souleigh ✨ Souleigh ✨
3年前
前端性能优化 - 雅虎军规
无论是在工作中,还是在面试中,web前端性能的优化都是很重要的,那么我们进行优化需要从哪些方面入手呢?可以遵循雅虎的前端优化35条军规,这样对于优化有一个比较清晰的方向.35条军规1.尽量减少HTTP请求个数——须权衡2.使用CDN(内容分发网络)3.为文件头指定Expires或CacheControl,使内容具有缓存性。4.避免空的
Wesley13 Wesley13
2年前
Java日期时间API系列31
  时间戳是指格林威治时间1970年01月01日00时00分00秒起至现在的总毫秒数,是所有时间的基础,其他时间可以通过时间戳转换得到。Java中本来已经有相关获取时间戳的方法,Java8后增加新的类Instant等专用于处理时间戳问题。 1获取时间戳的方法和性能对比1.1获取时间戳方法Java8以前
Stella981 Stella981
2年前
Android So动态加载 优雅实现与原理分析
背景:漫品Android客户端集成适配转换功能(基于目标识别(So库35M)和人脸识别库(5M)),导致apk体积50M左右,为优化客户端体验,决定实现So文件动态加载.!(https://oscimg.oschina.net/oscnet/00d1ff90e4b34869664fef59e3ec3fdd20b.png)点击上方“蓝字”关注我
Stella981 Stella981
2年前
HIVE 时间操作函数
日期函数UNIX时间戳转日期函数: from\_unixtime语法:   from\_unixtime(bigint unixtime\, string format\)返回值: string说明: 转化UNIX时间戳(从19700101 00:00:00 UTC到指定时间的秒数)到当前时区的时间格式举例:hive   selec
Stella981 Stella981
2年前
Python time模块 返回格式化时间
常用命令  strftimetime.strftime("%Y%m%d%H:%M:%S",formattime)第二个参数为可选参数,不填第二个参数则返回格式化后的当前时间日期201812112:00:00time.strftime('%H:%M:%S')返回当前时间的时分秒time.strftim
暗箭伤人 暗箭伤人
8个月前
【www.ithunter.club】 20230922下午
不容易的2023年,我们一起努力【www.ithunter.club】(2023092208:00:00.8872062023092216:00:00.887206)1.人事招聘专员数名(可选远程或入职)2.招聘向坐标东京Yahoo、Shift、L
Python进阶者 Python进阶者
4个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这
Easter79
Easter79
Lv1
今生可爱与温柔,每一样都不能少。
文章
2.8k
粉丝
5
获赞
1.2k