java 1.5 String.format 以前就覺得很好用,本想查查有多少 Conversion 可以用,沒想到一查 Formatter 中的各種 Conversion ,稍微試了一下,以前像 Date 處理、add leading zero、金額加上","等情況,都變得很容易處理了。
import java.util.Date;

public class StringFormatT {

public static void main(String[] args) {
System.out.println("===== test argument index ======================================================");
System.out.println("original\t10\t20");
System.out.println("format\t\t%1$s\t%1$d\t%2$s\t%2$d");
System.out.println(String.format("result\t\t\"%1$s\"\t%1$d\t\"%2$s\"\t%2$d", 10, 20));
System.out.println("original\t10\t20\t30\t40");
System.out.println("format\t\t%1$s\t%3$d\t%4$s\t%2$d");
System.out.println(String.format("result\t\t\"%1$s\"\t%3$d\t\"%4$s\"\t%2$d", 10, 20, 30, 40));
System.out.println("================================================================================");
System.out.println("===== formatted as string ======================================================");
System.out.println("original\t\"abc\"\tnull\t(byte)1\t3.14");
System.out.println("format\t\t%s\t%S\t%s\t%S");
System.out.println(String.format("result\t\t%s\t%S\t%s\t%S", "abc", null, (byte)1, 3.14));
System.out.println("================================================================================");
System.out.println("===== formatted as boolean =====================================================");
System.out.println("original\t\"abc\"\tnull\t(byte)1\t3.14");
System.out.println("format\t\t%b\t%B\t%b\t%B");
System.out.println(String.format("result\t\t%b\t%B\t%b\t%B", "abc", null, (byte)1, 3.14));
System.out.println("================================================================================");
System.out.println("===== formatted as hex =========================================================");
System.out.println("original\t\"100\"\tnull\t100\tnew Integer(100)\t3.14");
System.out.println("format\t\t%h\t%H\t%h\t%H\t\t\t%h");
System.out.println(String.format("result\t\t%h\t%H\t%h\t%H\t\t\t%h", "100", null, 100, new Integer(100), 3.14));
System.out.println("================================================================================");
System.out.println("===== formatted as character ===================================================");
System.out.println("original\t'A'\t97\t97");
System.out.println("format\t\t%s\t%C\t%c");
System.out.println(String.format("result\t\t%s\t%C\t%c", 'A', 97, 97));
System.out.println("================================================================================");
System.out.println("===== formatted as integer =====================================================");
System.out.println("original\t100\t100\t100");
System.out.println("format\t\t%d\t%o\t%h");
System.out.println(String.format("result\t\t%d\t%o\t%h", 100, 100, 100));
System.out.println("================================================================================");
System.out.println("===== formatted as big number ==================================================");
System.out.println("original\t123456789L");
System.out.println("format\t\t%1$d\t\t%1$,d");
System.out.println(String.format("result\t\t%1$d\t%1$,d", 123456789L));
System.out.println("================================================================================");
System.out.println("===== formatted as float =======================================================");
System.out.println("original\t123456789.98765");
System.out.println("format\t\t%1$.2a\t\t%1$.2e\t\t%1$.2f\t\t%1$.2g");
System.out.println(String.format("result\t\t%1$.2a\t%1$.2e\t%1$.2f\t%1$.2g", 123456789.98765));
System.out.println("================================================================================");
System.out.println("===== formatted as date ========================================================");
long currentTime = System.currentTimeMillis();
Date date = new Date();
System.out.println("original\tCurrent datetime use System.currentTimeMillis()");
System.out.println("format\t\t%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS");
System.out.println(String.format("result\t\t%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS", currentTime));
System.out.println("format\t\t%1$tF %1$tT");
System.out.println(String.format("result\t\t%1$tF %1$tT", currentTime));
System.out.println("original\tCurrent datetime use java.util.Date");
System.out.println("format\t\t%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS");
System.out.println(String.format("result\t\t%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS", date));
System.out.println("================================================================================");
System.out.println("===== What's the effect of width.precision on String? ==========================");
System.out.println("original\tabcdefg");
System.out.println("format\t\t[%1$s]\t\t[%1$10s]\t[%1$10.4s]\t[%1$-10s]");
System.out.println(String.format("result\t\t[%1$s]\t[%1$10s]\t[%1$10.4s]\t[%1$-10s]", "abcdefg"));
System.out.println("================================================================================");
System.out.println("===== padding easy ==========================");
System.out.println("original\tadd leading zero");
System.out.println("format\t\t%012d");
System.out.println(String.format("result\t\t%012d", 123));
System.out.println("================================================================================");
System.out.println("===== test new line ============================================================");
System.out.println("original\t[%n]");
System.out.println("format\t\t%n");
System.out.println(String.format("result\t\t[%n]"));
System.out.println("================================================================================");
}
}


結果顯示:
===== test argument index ====================================================== original 10 20 format %1$s %1$d %2$s %2$d result "10" 10 "20" 20 original 10 20 30 40 format %1$s %3$d %4$s %2$d result "10" 30 "40" 20 ================================================================================ ===== formatted as string ====================================================== original "abc" null (byte)1 3.14 format %s %S %s %S result abc NULL 1 3.14 ================================================================================ ===== formatted as boolean ===================================================== original "abc" null (byte)1 3.14 format %b %B %b %B result true FALSE true TRUE ================================================================================ ===== formatted as hex ========================================================= original "100" null 100 new Integer(100) 3.14 format %h %H %h %H %h result bdf1 NULL 64 64 11e29ba7 ================================================================================ ===== formatted as character =================================================== original 'A' 97 97 format %s %C %c result A A a ================================================================================ ===== formatted as integer ===================================================== original 100 100 100 format %d %o %h result 100 144 64 ================================================================================ ===== formatted as big number ================================================== original 123456789L format %1$d %1$,d result 123456789 123,456,789 ================================================================================ ===== formatted as float ======================================================= original 123456789.98765 format %1$.2a %1$.2e %1$.2f %1$.2g result 0x1.d7p26 1.23e+08 123456789.99 1.2e+08 ================================================================================ ===== formatted as date ======================================================== original Current datetime use System.currentTimeMillis() format %1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS result 2013-05-13 16:08:57 format %1$tF %1$tT result 2013-05-13 16:08:57 original Current datetime use java.util.Date format %1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS result 2013-05-13 16:08:57 ================================================================================ ===== What's the effect of width.precision on String? ========================== original abcdefg format [%1$s] [%1$10s] [%1$10.4s] [%1$-10s] result [abcdefg] [ abcdefg] [ abcd] [abcdefg ] ================================================================================ ===== padding easy ========================== original add leading zero format %012d result 000000000123 ================================================================================ ===== test new line ============================================================ original [%n] format %n result [ ] ================================================================================

mark528 發表在 痞客邦 PIXNET 留言(0) 人氣()