日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

String.format()格式化輸出

作者:騎個小蝸牛 更新時間: 2023-07-10 編程語言

文章目錄

  • 方法參數
  • 常用占位符
  • 占位符使用
    • - %s:字符串
    • - %d:整數
    • - %f:浮點數
    • - %c:字符
    • - %b:布爾
    • - %t:日期/時間
    • - %e:科學計數法
  • 特殊使用方式
    • - 指定最小寬度
    • - 指定填充字符
    • - 確定精度
    • - 日期格式化
    • - 添加百分號%
    • - 按順序引用參數
  • 效率問題


String.format() 方法是 Java 的一個格式化輸出方法。它可將不同類型的數據格式化為指定格式的字符串,并將結果存儲在字符串中。

方法參數

String format(String format, Object... args)
  • format:要返回的字符串格式
  • args:要在格式中替換的值

format中可以包含若干個占位符,占位符由百分號%跟隨一個格式類型字符組成。

常用占位符

占位符 類型 描述
s 字符串 通常用于字符串類型和任意對象,輸出任意對象的toString()方法返回值
d 整數 通常用于整數類型和 byte、short、int、long 等原始類型
f 浮點數 通常用于浮點數類型和 float 和 double 等原始類型
c 字符 通常用于字符類型和 char 等原始類型
b 布爾 通常用于布爾類型和 boolean等原始類型
t 日期/時間 通常用于日期和時間類型
e 科學計數法 通常用于將浮點數字格式化為科學計數法。

占位符使用

- %s:字符串

String format1 = String.format("姓名:%s,性別:%s", "李哈哈", "男");
System.out.println(format1);
姓名:李哈哈,性別:男

- %d:整數

String format2 = String.format("年齡:%d", 28);
System.out.println(format2);
年齡:28

- %f:浮點數

String format3 = String.format("語文:%f,數學:%f", 95.5, 99.5);
System.out.println(format3);
語文:95.500000,數學:99.500000

- %c:字符

String format4 = String.format("語文:%c,數學:%c", 'A', 'B');
System.out.println(format4);
語文:A,數學: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:科學計數法

String format7 = String.format("%e", 950000000.1);
System.out.println(format7);
9.500000e+08

特殊使用方式

- 指定最小寬度

在 % 符號和占位符之間可以添加一個數字,該數字表示占位符的最小寬度。

  • 如果需要填充該寬度的字符數量不足,則會自動右對齊,前面加-則號左對齊
  • 如果超過該寬度,則保留所有字符。
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          |

- 指定填充字符

可以在% 符號和占位符之間添加填充字符來指定要使用的字符。默認的填充字符為空格(上面指定最小寬度的示例就是填充的空格),支持的填充字:

  • 空格:寬度不夠時默認使用空格填充
  • 0:寬度不夠時可以使用0填充
  • +:為正數或負數添加符號
  • -:左對齊(默認是右對齊)
  • ,:以組分隔符形式指定的數字(每 3 個數字一個逗號)
  • #:對于二進制、八進制、十六進制等具有可選前綴的數字,會顯示前綴
  • .:精度或字符串截斷點
  • $:按順序引用參數
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

- 確定精度

對于浮點數類型和其它數字類型,可以使用精度來指定小數位數或有效數字位數。精度由一個點號(.)和數字來表示。

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 表示保留兩位小數,.4f 表示保留四位小數,而 %+.2f 表示保留兩位小數,并在正數前面加上加號。

- 日期格式化

占位符 %t 后面可以跟隨字母來表示時間格式。

  • %tB:日期的月份,全名,例如:June
  • %tb:日期的月份,縮寫,例如:Jun
  • %tm:日期的月份,兩位數,例如:06
  • %tY:4 位數的年份,例如:2023
  • %ty:2 位數的年份,例如:23
  • %tk:24 小時制的小時,不帶前導零,例如:20
  • %tH:24 小時制的小時,兩位數,例如:20
  • %tl:12 小時制的小時,不帶前導零,例如:8
  • %tI:12 小時制的小時,兩位數,例如:08
  • %tM:分鐘,兩位數,例如:30
  • %tS:秒數,兩位數,例如:45
  • %tL:毫秒數,三位數,例如:886
  • %tN:納秒數,9 位數,例如:886456123
  • %tp:上午/下午(大小寫不同),例如:上午
  • %tz:時區偏移量,例如:+0800
  • %tZ:時區縮寫,例如: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 開始的秒數,例如:168412870
  • %tQ:從 1970 年 1 月 1 日 00:00:00 GMT 開始的毫秒數,例如: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("當前時間:"+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);
當前時間: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%

- 按順序引用參數

指定參數的引用順序,在%和字母中間添加[數字]$指定引用第幾個參數。

String s = String.format("第二個參數是:%2$s,第三個參數是:%3$s,第一個參數是:%1$s", "hello", "world", "java");
System.out.println(s);
第二個參數是:world,第三個參數是:java,第一個參數是:hello

效率問題

String.format()相對于其他字符串拼接方法在格式化輸出上具有較高的靈活性,但是在效率上的確不如其他拼接方式,例如用StringBuilder或者是StringBuffer拼接字符串。

  • 在使用String.format()時,Java會創建一個新的字符串對象,該對象為格式化輸出的結果,并將之返回給調用方。然而,在創建該字符串結果時,Java需要進行大量的字符串拼接和處理操作,因此這個過程會比較耗時。

  • 相比之下,使用StringBuilder或者是StringBuffer拼接字符串時,我們可以一次性地將所有字符串拼接完成,避免了多次創建新的字符串對象,因此這種方式通常比使用String.format()更加高效地構建字符串。

在實際編程中,我們需要根據具體的業務場景和需求來選擇最合適的字符串拼接方式,既要考慮效率問題,也要兼顧代碼的可讀性和可維護性。

原文鏈接:https://blog.csdn.net/JokerLJG/article/details/131225162

  • 上一篇:沒有了
  • 下一篇:沒有了
欄目分類
最近更新