Java String 输出格式

HDU 2093:

System.out.format("%-10s %2d %4d\n", p[i].name, p[i].a, p[i].b); //错误

System.out.println(String.format("%-10s %2d %4d", p[i].name, p[i].a, p[i].b));// 正确

http://www.cnblogs.com/webcyz/archive/2011/12/18/2291734.html

http://blog.csdn.net/touch_2011/article/details/6860574

例子:

System.out.println(String.format("%1$,09d", -3123));
System.out.println(String.format("%1$9d", -31));
System.out.println(String.format("%1$-9d", -31));
System.out.println(String.format("%1$(9d", -31));
System.out.println(String.format("%1$#9x", 5689));


System.out.println(String.format("%1$c", 'A'));

System.out.println(String.format("%1$8c", 'A')); //前面留7个空格

System.out.println(String.format("%1$-8c", 'A')); //后面留7个空格

String str = "abcd";
System.out.println(String.format("%1$-10s", str) + "123");
System.out.println(String.format("%1$10s", str) + "123");

int n = 16;
System.out.format("%03x\n", n);

结果:

-0003,123
  -31
-31
(31)
0x1639
A
  A
A  
abcd 123
  abcd123
010

 

posted on 2013-02-24 22:19  Sure_Yi  阅读(608)  评论(0编辑  收藏  举报

导航