Java String format()

String.format() 方法用來建立格式化的字串。

語法

String.format(String format, Object... args)

基本範例

String name = "Alice";
int age = 25;

String s = String.format("姓名:%s,年齡:%d", name, age);
System.out.println(s);  // "姓名:Alice,年齡:25"

格式化符號

符號說明範例輸出
%s字串"Hello"
%d十進位整數42
%f浮點數3.141593
%c字元A
%b布林值true
%n換行(跨平台)
%%% 符號%
%x十六進位2a
%o八進位52
%e科學記號3.14e+00

整數格式化

int n = 42;

System.out.println(String.format("%d", n));     // "42"
System.out.println(String.format("%5d", n));    // "   42" (寬度 5,靠右)
System.out.println(String.format("%-5d", n));   // "42   " (寬度 5,靠左)
System.out.println(String.format("%05d", n));   // "00042" (補零)
System.out.println(String.format("%+d", n));    // "+42" (顯示正號)
System.out.println(String.format("%,d", 1000000)); // "1,000,000" (千分位)

浮點數格式化

double d = 3.14159265;

System.out.println(String.format("%f", d));      // "3.141593" (預設 6 位小數)
System.out.println(String.format("%.2f", d));    // "3.14" (2 位小數)
System.out.println(String.format("%.0f", d));    // "3" (無小數)
System.out.println(String.format("%8.2f", d));   // "    3.14" (寬度 8)
System.out.println(String.format("%-8.2f", d));  // "3.14    " (靠左)
System.out.println(String.format("%08.2f", d));  // "00003.14" (補零)
System.out.println(String.format("%,.2f", 1234567.89)); // "1,234,567.89"

字串格式化

String s = "Java";

System.out.println(String.format("%s", s));     // "Java"
System.out.println(String.format("%10s", s));   // "      Java" (寬度 10,靠右)
System.out.println(String.format("%-10s", s));  // "Java      " (靠左)
System.out.println(String.format("%.2s", s));   // "Ja" (最多 2 字元)
System.out.println(String.format("%10.2s", s)); // "        Ja"

參數索引

可以指定參數的順序:

String s = String.format("%2$s %1$s", "World", "Hello");
System.out.println(s);  // "Hello World"

// 重複使用同一個參數
String s2 = String.format("%1$s + %1$s = %2$d", 5, 10);
System.out.println(s2);  // "5 + 5 = 10"

日期時間格式化

import java.util.Date;

Date now = new Date();

System.out.println(String.format("%tY-%tm-%td", now, now, now)); // "2024-12-10"
System.out.println(String.format("%tH:%tM:%tS", now, now, now)); // "14:30:45"

// 使用 < 表示使用上一個參數
System.out.println(String.format("%tY-%<tm-%<td %<tH:%<tM:%<tS", now));
// "2024-12-10 14:30:45"

// 常用日期時間符號
// %tY - 4位年份, %ty - 2位年份
// %tm - 月份, %td - 日
// %tH - 24小時制, %tI - 12小時制
// %tM - 分鐘, %tS - 秒
// %tA - 星期名稱, %ta - 星期縮寫
// %tB - 月份名稱, %tb - 月份縮寫

printf() vs format()

System.out.printf() 直接輸出,String.format() 回傳字串:

// printf() - 直接輸出
System.out.printf("姓名:%s%n", "Alice");

// format() - 回傳字串
String s = String.format("姓名:%s", "Alice");
System.out.println(s);

實際應用

格式化表格

String[] names = {"Alice", "Bob", "Charlie"};
int[] scores = {95, 87, 92};

System.out.println(String.format("%-10s %5s", "姓名", "分數"));
System.out.println("-".repeat(16));

for (int i = 0; i < names.length; i++) {
    System.out.println(String.format("%-10s %5d", names[i], scores[i]));
}

輸出:

姓名         分數
----------------
Alice         95
Bob           87
Charlie       92

格式化金額

double amount = 1234567.89;

String formatted = String.format("$%,.2f", amount);
System.out.println(formatted);  // "$1,234,567.89"

格式化進度

int current = 75;
int total = 100;
double percent = (double) current / total * 100;

String progress = String.format("進度:%d/%d (%.1f%%)", current, total, percent);
System.out.println(progress);  // "進度:75/100 (75.0%)"