Đây là một danh sách những gì String.format
có thể làm. Cũng vậy với printf
int i = 123;
o.printf("|%d|%d|%n" , i, -i); // |123|-123|
o.printf("|%5d|%5d|%n" , i, -i); // | 123| –123|
o.printf("|%-5d|%-5d|%n" , i, -i); // |123 |-123 |
o.printf("|%+-5d|%+-5d|%n" , i, -i); // |+123 |-123 |
o.printf("|%05d|%05d|%n%n", i, -i); // |00123|-0123|
o.printf("|%X|%x|%n", 0xabc, 0xabc); // |ABC|abc|
o.printf("|%04x|%#x|%n%n", 0xabc, 0xabc); // |0abc|0xabc|
double d = 12345.678;
o.printf("|%f|%f|%n" , d, -d); // |12345,678000| |-12345,678000|
o.printf("|%+f|%+f|%n" , d, -d); // |+12345,678000| |-12345,678000|
o.printf("|% f|% f|%n" , d, -d); // | 12345,678000| |-12345,678000|
o.printf("|%.2f|%.2f|%n" , d, -d); // |12345,68| |-12345,68|
o.printf("|%,.2f|%,.2f|%n" , d, -d); // |12.345,68| |-12.345,68|
o.printf("|%.2f|%(.2f|%n", d, -d); // |12345,68| |(12345,68)|
o.printf("|%10.2f|%10.2f|%n" , d, -d); // | 12345,68| | –12345,68|
o.printf("|%010.2f|%010.2f|%n",d, -d); // |0,68| |-,68|
String s = "Monsterbacke";
o.printf("%n|%s|%n", s); // |Monsterbacke|
o.printf("|%S|%n", s); // |MONSTERBACKE|
o.printf("|%20s|%n", s); // | Monsterbacke|
o.printf("|%-20s|%n", s); // |Monsterbacke |
o.printf("|%7s|%n", s); // |Monsterbacke|
o.printf("|%.7s|%n", s); // |Monster|
o.printf("|%20.7s|%n", s); // | Monster|
Date t = new Date();
o.printf("%tT%n", t); // 11:01:39
o.printf("%tD%n", t); // 04/18/08
o.printf("%1$te. %1$tb%n", t); // 18. Apr
Đặt tên biến "chuỗi" đốt cháy thêm một số cylces não cpu để nhận ra nó không phải là loại, nhưng chỉ một tên biến. Chỉ muốn chỉ ra rằng những điều nhỏ nhặt như vậy có thể tạo ra sự khác biệt khi giải thích điều gì đó. –
Điểm tốt, tôi sẽ sửa lỗi đó. – pr1001
+1 cho thông điệp phù hợp với Ivan! –