關(guān)于String.format()格式化輸出方式
String.format() 方法是 Java 的一個格式化輸出方法。它可將不同類型的數(shù)據(jù)格式化為指定格式的字符串,并將結(jié)果存儲在字符串中。
方法參數(shù)
String format(String format, Object... args)
- format:要返回的字符串格式
- args:要在格式中替換的值
format中可以包含若干個占位符,占位符由百分號%跟隨一個格式類型字符組成。
常用占位符
占位符 | 類型 | 描述 |
---|---|---|
s | 字符串 | 通常用于字符串類型和任意對象,輸出任意對象的toString()方法返回值 |
d | 整數(shù) | 通常用于整數(shù)類型和 byte、short、int、long 等原始類型 |
f | 浮點數(shù) | 通常用于浮點數(shù)類型和 float 和 double 等原始類型 |
c | 字符 | 通常用于字符類型和 char 等原始類型 |
b | 布爾 | 通常用于布爾類型和 boolean等原始類型 |
t | 日期/時間 | 通常用于日期和時間類型 |
e | 科學(xué)計數(shù)法 | 通常用于將浮點數(shù)字格式化為科學(xué)計數(shù)法。 |
占位符使用
- %s:字符串
String format1 = String.format("姓名:%s,性別:%s", "李哈哈", "男"); System.out.println(format1);
姓名:李哈哈,性別:男
- %d:整數(shù)
String format2 = String.format("年齡:%d", 28); System.out.println(format2);
年齡:28
- %f:浮點數(shù)
String format3 = String.format("語文:%f,數(shù)學(xué):%f", 95.5, 99.5); System.out.println(format3);
語文:95.500000,數(shù)學(xué):99.500000
- %c:字符
String format4 = String.format("語文:%c,數(shù)學(xué):%c", 'A', 'B'); System.out.println(format4);
語文:A,數(shù)學(xué):B
- %b:布爾
String format5 = String.format("真:%b,假:%b,真:%b,真:%b,真:%b", true, false, "", 0, -1); System.out.println(format5);
真:true,假:false,真:true,真:true,真:true
注意:除了false,其他值都輸出為true。
- %t:日期/時間
String format6 = String.format("今天是:%tF", new Date()); System.out.println(format6);
今天是:2023-06-15
注意:%t不能單獨直接使用,必須跟隨其他字母來指定時間格式
- %e:科學(xué)計數(shù)法
String format7 = String.format("%e", 950000000.1); System.out.println(format7);
9.500000e+08
特殊使用方式
- 指定最小寬度
在 % 符號和占位符之間可以添加一個數(shù)字,該數(shù)字表示占位符的最小寬度。
- 如果需要填充該寬度的字符數(shù)量不足,則會自動右對齊,前面加-則號左對齊
- 如果超過該寬度,則保留所有字符。
String s1 = String.format("|%20s|", "Hello"); String s2 = String.format("|%-20s|", "Hello"); String s3 = String.format("|%20c|", 'A'); String s4 = String.format("|%20c|", 'A'); String s5 = String.format("|%20d|", 1); String s6 = String.format("|%-20d|", 1); String s7 = String.format("|%20f|", 1.0); String s8 = String.format("|%-20f|", 1.0); String s9 = String.format("|%-20b|", true); String s10 = String.format("|%-20b|", true); String s11 = String.format("|%-20tF|", new Date()); String s12 = String.format("|%-20tF|", new Date()); System.out.println(s1); System.out.println(s2); System.out.println(s3); System.out.println(s4); System.out.println(s5); System.out.println(s6); System.out.println(s7); System.out.println(s8); System.out.println(s9); System.out.println(s10); System.out.println(s11); System.out.println(s12);
| Hello|
|Hello |
| A|
| A|
| 1|
|1 |
| 1.000000|
|1.000000 |
|true |
|true |
|2023-06-15 |
|2023-06-15 |
- 指定填充字符
可以在% 符號和占位符之間添加填充字符來指定要使用的字符。默認(rèn)的填充字符為空格(上面指定最小寬度的示例就是填充的空格),支持的填充字:
- 空格:寬度不夠時默認(rèn)使用空格填充
- 0:寬度不夠時可以使用0填充
- +:為正數(shù)或負(fù)數(shù)添加符號
- -:左對齊(默認(rèn)是右對齊)
- ,:以組分隔符形式指定的數(shù)字(每 3 個數(shù)字一個逗號)
- #:對于二進(jìn)制、八進(jìn)制、十六進(jìn)制等具有可選前綴的數(shù)字,會顯示前綴
- .:精度或字符串截斷點
- $:按順序引用參數(shù)
String s1 = String.format("%10s", "hello"); String s2 = String.format("%010d", 42); String s3 = String.format("%+d", 42); String s4 = String.format("%-10s", "hello"); String s5 = String.format("%,d", 1234567890); String s6 = String.format("%#x", 42); String s7 = String.format("%.2f", 3.1415926); String s8 = String.format("%1$s %2$s", "hello", "world"); System.out.println(s1); System.out.println(s2); System.out.println(s3); System.out.println(s4); System.out.println(s5); System.out.println(s6); System.out.println(s7); System.out.println(s8);
hello
0000000042
+42
hello
1,234,567,890
0x2a
3.14
hello world
- 確定精度
對于浮點數(shù)類型和其它數(shù)字類型,可以使用精度來指定小數(shù)位數(shù)或有效數(shù)字位數(shù)。精度由一個點號(.)和數(shù)字來表示。
double f = 1234.56789; String s1 = String.format("%.2f", f); String s2 = String.format("%.4f", f); String s3 = String.format("%+.2f", f); System.out.println(s1); System.out.println(s2); System.out.println(s3);
1234.57
1234.5679
+1234.57
在這個例子中,.2f 表示保留兩位小數(shù),.4f 表示保留四位小數(shù),而 %+.2f 表示保留兩位小數(shù),并在正數(shù)前面加上加號。
- 日期格式化
占位符 %t 后面可以跟隨字母來表示時間格式。
- %tB:日期的月份,全名,例如:June
- %tb:日期的月份,縮寫,例如:Jun
- %tm:日期的月份,兩位數(shù),例如:06
- %tY:4 位數(shù)的年份,例如:2023
- %ty:2 位數(shù)的年份,例如:23
- %tk:24 小時制的小時,不帶前導(dǎo)零,例如:20
- %tH:24 小時制的小時,兩位數(shù),例如:20
- %tl:12 小時制的小時,不帶前導(dǎo)零,例如:8
- %tI:12 小時制的小時,兩位數(shù),例如:08
- %tM:分鐘,兩位數(shù),例如:30
- %tS:秒數(shù),兩位數(shù),例如:45
- %tL:毫秒數(shù),三位數(shù),例如:886
- %tN:納秒數(shù),9 位數(shù),例如:886456123
- %tp:上午/下午(大小寫不同),例如:上午
- %tz:時區(qū)偏移量,例如:+0800
- %tZ:時區(qū)縮寫,例如:CST
- %tj:1 年中的第幾天,例如:166
- %tD:U.S.日期格式(MM/DD/YY),例如:06/15/23
- %tF:ISO 日期格式,例如:2023-06-15
- %tc:完整的日期和時間,例如:Wed Jun 15 07:32:24 CST 2023
- %tr:12 小時格式的時間(上午/下午),例如:02:45:30 下午
- %tR:24 小時格式的時間(格式為 HH:mm)
- %tT:24 小時格式的時間,例如:14:45:30
- %ts:從 1970 年 1 月 1 日 00:00:00 GMT 開始的秒數(shù),例如:168412870
- %tQ:從 1970 年 1 月 1 日 00:00:00 GMT 開始的毫秒數(shù),例如:168412870886
Calendar c = Calendar.getInstance(); Date date = c.getTime(); String s1 = String.format("%tB", date); String s2 = String.format("%tb", date); String s3 = String.format("%tm", date); String s4 = String.format("%tY", date); String s5 = String.format("%ty", date); String s6 = String.format("%tk", date); String s7 = String.format("%tH", date); String s8 = String.format("%tl", date); String s9 = String.format("%tI", date); String s10 = String.format("%tM", date); String s11 = String.format("%tS", date); String s12 = String.format("%tL", date); String s13 = String.format("%tN", date); String s14 = String.format("%tp", date); String s15 = String.format("%tz", date); String s16 = String.format("%tZ", date); String s17 = String.format("%tj", date); String s18 = String.format("%tD", date); String s20 = String.format("%tF", date); String s21 = String.format("%tc", date); String s22 = String.format("%tr", date); String s23 = String.format("%tR", date); String s24 = String.format("%tT", date); String s25 = String.format("%ts", date); String s26 = String.format("%tQ", date); System.out.println("當(dāng)前時間:"+DateUtil.format(date, "yyyy-MM-dd HH:mm:ss")); System.out.println("%tB:" + s1); System.out.println("%tb:" + s2); System.out.println("%tm:" + s3); System.out.println("%tY:" + s4); System.out.println("%ty:" + s5); System.out.println("%tk:" + s6); System.out.println("%tH:" + s7); System.out.println("%tl:" + s8); System.out.println("%tI:" + s9); System.out.println("%tM:" + s10); System.out.println("%tS:" + s11); System.out.println("%tL:" + s12); System.out.println("%tN:" + s13); System.out.println("%tp:" + s14); System.out.println("%tz:" + s15); System.out.println("%tZ:" + s16); System.out.println("%tj:" + s17); System.out.println("%tD:" + s18); System.out.println("%tF:" + s20); System.out.println("%tc:" + s21); System.out.println("%tr:" + s22); System.out.println("%tR:" + s23); System.out.println("%tT:" + s24); System.out.println("%ts:" + s25); System.out.println("%tQ:" + s26);
當(dāng)前時間:2023-06-15 16:04:26
%tB:六月
%tb:六月
%tm:06
%tY:2023
%ty:23
%tk:16
%tH:16
%tl:4
%tI:04
%tM:04
%tS:26
%tL:536
%tN:536000000
%tp:下午
%tz:+0800
%tZ:CST
%tj:166
%tD:06/15/23
%tF:2023-06-15
%tc:星期四 六月 15 16:04:26 CST 2023
%tr:04:04:26 下午
%tR:16:04
%tT:16:04:26
%ts:1686816266
%tQ:1686816266536
- 添加百分號%
要在格式化字符串中打印百分號 % 字符,可以使用 %% 來表示。
int n = 42; String s = String.format("%d%%", n); System.out.println(s);
42%
- 按順序引用參數(shù)
指定參數(shù)的引用順序,在%和字母中間添加[數(shù)字]$指定引用第幾個參數(shù)。
String s = String.format("第二個參數(shù)是:%2$s,第三個參數(shù)是:%3$s,第一個參數(shù)是:%1$s", "hello", "world", "java"); System.out.println(s);
第二個參數(shù)是:world,第三個參數(shù)是:java,第一個參數(shù)是:hello
效率問題
String.format()相對于其他字符串拼接方法在格式化輸出上具有較高的靈活性,但是在效率上的確不如其他拼接方式,例如用StringBuilder或者是StringBuffer拼接字符串。
- 在使用String.format()時,Java會創(chuàng)建一個新的字符串對象,該對象為格式化輸出的結(jié)果,并將之返回給調(diào)用方。然而,在創(chuàng)建該字符串結(jié)果時,Java需要進(jìn)行大量的字符串拼接和處理操作,因此這個過程會比較耗時。
- 相比之下,使用StringBuilder或者是StringBuffer拼接字符串時,我們可以一次性地將所有字符串拼接完成,避免了多次創(chuàng)建新的字符串對象,因此這種方式通常比使用String.format()更加高效地構(gòu)建字符串。
在實際編程中,我們需要根據(jù)具體的業(yè)務(wù)場景和需求來選擇最合適的字符串拼接方式,既要考慮效率問題,也要兼顧代碼的可讀性和可維護性。
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java之多個線程順序循環(huán)執(zhí)行的幾種實現(xiàn)
這篇文章主要介紹了Java之多個線程順序循環(huán)執(zhí)行的幾種實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09Java?Collections.sort()實現(xiàn)List排序的默認(rèn)方法和自定義方法
這篇文章主要介紹了Java?Collections.sort()實現(xiàn)List排序的默認(rèn)方法和自定義方法,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2017-06-06IntelliJ IDEA 15款超級牛逼插件推薦(自用,超級牛逼)
這篇文章主要給大家推薦介紹了IntelliJ IDEA 15款超級牛逼插件,這15款插件都是自用的,真的非常推薦,需要的朋友可以參考下2020-11-11springboot+redis過期事件監(jiān)聽實現(xiàn)過程解析
這篇文章主要介紹了springboot+redis過期事件監(jiān)聽實現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-03-03java ssm框架實現(xiàn)分頁功能的示例代碼(oracle)
這篇文章主要介紹了java ssm框架實現(xiàn)分頁功能的示例代碼(oracle),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03SpringBoot如何導(dǎo)出Jar包并測試(使用IDEA)
這篇文章主要介紹了SpringBoot如何導(dǎo)出Jar包并測試(使用IDEA),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07