🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: cn.bbs.comp.lang.java
Article #414

JAVA String.format ����ʹ�ý���(zz)

#414
From: �����̶�
Date: Wed, 17 Dec 2008 01:07
97 lines
11650 bytes
    ת���ԣ�http://blog.csdn.net/andycpp/archive/2007/08/18/1749700.aspx 

    ��JDK1.5�У�String��������һ���dz����õľ�̬����format(String  format, Objece...  argues)�����Խ�������ݸ�ʽ��Ϊ�ַ����������format����ָ��������ĸ�ʽ�������Ҳ���������յ�һ�㣬��argues����һϵ�еȴ��ʽ���Ķ��󡣸ú����c������printf������÷�������һ����ģ�£������c���Ի����ѧ������������ࡣ����������������һ��format ����ĸ�ʽ�����塣 
        format�����п��԰���Ҫת�����ַ���Щ�ַ�����дʲô�����վ����ʲô��ͬʱ����һЩ�����ʽ�����ݣ���ָ�����ĸ�������ת�����Լ�ת����ʲô��ʽ����������ĸ�ʽͨͨ��%index$��ͷ��index��1��ʼȡֵ����ʾ����index�������ý������и�ʽ������һ���c����Ҫǿһ�㣬 c����ֻ�ܰ��ղ����˳�����θ�ʽ������java����ѡ���n����������ʽ�������ڸú�����Զ�����һ��������и�ʽ������ͬ�Ķ������õIJ���Ҳ��ͬ���������������������ۡ� 

1.��������и�ʽ����%[index$][��ʶ][��С���]ת����ʽ 
        ���ǿ��Կ�������ʽ���ַ���4������ɣ�����%[index$]�ĺ������������Ѿ�����[��С���]�ĺ���Ҳ�ܺ���⣬�������ո�����ת�����ַ����ٰ����λ���֡�����������ʣ��2�����ֵĺ���ɣ� 
��ʶ�� 
'-'    ����С���������룬�������롰��0��䡱ͬʱʹ�� 
'#'    ֻ������8���ƺ�16���ƣ�8����ʱ�ڽ��ǰ������һ��0��16����ʱ�ڽ��ǰ������0x 
'+'    ������ǰ���һ����ţ�һ�������ֻ������10���ƣ�������ΪBigInteger�ſ�������8���ƺ�16���ƣ� 
'  '    ��ֵǰ�ӿո񣬸�ֵǰ�Ӹ��ţ�һ�������ֻ������10���ƣ�������ΪBigInteger�ſ�������8���ƺ�16���ƣ� 
'0'    ������������ 
','    ֻ������10���ƣ�ÿ3λ����֮���á������ָ� 
'('    �������Ǹ��������в���Ӹ��Ŷ�����Բ���Ű�������������ͬ��+������ͬ������ƣ� 

ת����ʽ�� 
d-ʮ����   o-�˽���   x��X-ʮ�����        �����˵�����ڿ����������������������ӡ���Ҫ�ر�ע���һ���ǣ��󲿷ֱ�ʶ�ַ����ͬʱʹ�á�        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)); 

//����� 
//-0003,123 
//      -31 
//-31      
//     (31) 
//   0x1639 

2.�Ը�������и�ʽ����%[index$][��ʶ][���ٿ��][.����]ת����ʽ 
        ���ǿ��Կ������������ת������һ�������ȡ�ѡ����Կ���С�������λ���ʶ�� 
'-'    ����С���������룬�������롰��0��䡱ͬʱʹ�� 
'+'    ������ǰ���һ����� 
'  '    ��ֵǰ�ӿո񣬸�ֵǰ�Ӹ��� 
'0'    ������������ 
','    ÿ3λ����֮���á������ָ���ֻ������fgG��ת���� 
'('    �������Ǹ��������в���Ӹ��Ŷ�����Բ���Ű�������������ֻ������eEfgG��ת���� 

ת����ʽ�� 
'e', 'E'  --  ����ʽ��Ϊ�ü�����ѧ�����ʾ��ʮ������ 
'f'          --  ����ʽ��Ϊʮ������ͨ��ʾ��ʽ 
'g', 'G'    --  ��ݾ���������Զ�ѡ������ͨ��ʾ��ʽ���ǿ�ѧ����ʽ 
'a', 'A'    --   ����ʽ��Ϊ����Чλ���ָ���ʮ����Ƹ����� 

3.���ַ���и�ʽ���� 
        ���ַ���и�ʽ���Ƿdz��򵥵ģ�c��ʾ�ַ��ʶ��'-'��ʾ����룬�����ûʲô�ˡ� 

4.�԰ٷֱȷ�Ž��и�ʽ���� 
        ���������˵������һᷢ�ְٷֱȷ�š�%���������ʽ��һ��ǰ׺����ô����Ҫ����һ���ٷֱȷ�Ÿ���ô���أ��϶�����Ҫת���ַ��,����Ҫע����ǣ�������ת���ַ��ǡ�\�������ǡ�%�������仰˵�������������������һ����12%���� 
System.out.println(String.format("%1$d%%", 12)); 

5.ȡ��ƽ̨�������зָ��� 
        System.getProperty("line.separator")����ȡ��ƽ̨�������зָ���������format�м�δ���Եù��ڷ����ˡ�����format�����Դ���һ��ƽ̨�������зָ����Ǿ���String.format("%n")�� 

6.���������ͽ��и�ʽ���� 
         �������ں�ʱ��ת���ĺ�׺�ַ���Ϊ 't' �� 'T' ת������ġ���Щ���������ڵ�����ȫ��ͬ����Щ�� GNU date �� POSIX strftime(3c) ��������͡��ṩ����ת��������Ϊ�˷����ض��� Java �Ĺ��ܣ��罫 'L' �������еĺ��룩�� 

����ת���ַ�������ʽ��ʱ�䣺 
'H'     24 Сʱ�Ƶ�Сʱ������ʽ��Ϊ��Ҫʱ��ǰ�������λ�� 00 - 23�� 
'I'     12 Сʱ�Ƶ�Сʱ������ʽ��Ϊ��Ҫʱ��ǰ�������λ�� 01 - 12�� 
'k'     24 Сʱ�Ƶ�Сʱ���� 0 - 23�� 
'l'     12 Сʱ�Ƶ�Сʱ���� 1 - 12�� 
'M'     Сʱ�еķ��ӣ�����ʽ��Ϊ��Ҫʱ��ǰ�������λ�� 00 - 59�� 
'S'     �����е��룬����ʽ��Ϊ��Ҫʱ��ǰ�������λ�� 00 - 60 ��"60" ��֧�����������һ������ֵ���� 
'L'     ���еĺ��룬����ʽ��Ϊ��Ҫʱ��ǰ�������λ�� 000 - 999�� 
'N'     ���еĺ�΢�룬����ʽ��Ϊ��Ҫʱ��ǰ����ľ�λ�� 000000000 - 999999999�� 
'p'     �ض������Ի����� ��������� �����Сд��ʽ��ʾ������ "am" �� "pm"��ʹ��ת��ǰ׺ 'T' ����ǿ�н������ת��Ϊ��д��ʽ�� 
'z'     ����� GMT �� RFC 822 ��ʽ������ʱ��ƫ���������� -0800�� 
'Z'     ��ʾʱ����д��ʽ���ַ�Formatter �����Ի�����ȡ���������Ի���������У��� 
's'     ��Э������ʱ (UTC) 1970 �� 1 �� 1 �� 00:00:00 �������������� Long.MIN_VALUE/1000 �� Long.MAX_VALUE/1000 ֮��IJ�ֵ�� 
'Q'     ��Э������ʱ (UTC) 1970 �� 1 �� 1 �� 00:00:00 ���������ĺ����� Long.MIN_VALUE �� Long.MAX_VALUE ֮��IJ�ֵ�� 

����ת���ַ�������ʽ�����ڣ� 
'B'     �ض������Ի������·�ȫ�ƣ����� "January" �� "February"�� 
'b'     �ض������Ի������·ݼ�ƣ����� "Jan" �� "Feb"�� 
'h'     �� 'b' ��ͬ�� 
'A'     �ض������Ի��������ڼ�ȫ�ƣ����� "Sunday" �� "Monday" 
'a'     �ض������Ի��������ڼ���ƣ����� "Sun" �� "Mon" 
'C'     ���� 100 ����λ���ʾ����ݣ�����ʽ��Ϊ��Ҫʱ��ǰ�������λ�� 00 - 99 
'Y'     ��ݣ�����ʽ��Ϊ��Ҫʱ��ǰ�������λ�����٣������磬0092 ���ڸ��������� 92 CE�� 
'y'     ��ݵ������λ���ʽ��Ϊ��Ҫʱ��ǰ�������λ�� 00 - 99�� 
'j'     һ���е������ʽ��Ϊ��Ҫʱ��ǰ�������λ�����磬���ڸ���������� 001 - 366�� 
'm'     �·ݣ�����ʽ��Ϊ��Ҫʱ��ǰ�������λ�� 01 - 13�� 
'd'     һ�����е������ʽ��Ϊ��Ҫʱ��ǰ������λ�� 01 - 31 
'e'     һ�����е������ʽ��Ϊ��λ�� 1 - 31�� 

����ת���ַ����ڸ�ʽ�����������/ʱ����ϡ� 
'R'     24 Сʱ�Ƶ�ʱ�䣬����ʽ��Ϊ "%tH:%tM" 
'T'     24 Сʱ�Ƶ�ʱ�䣬����ʽ��Ϊ "%tH:%tM:%tS"�� 
'r'     12 Сʱ�Ƶ�ʱ�䣬����ʽ��Ϊ "%tI:%tM:%tS %Tp"������������� ('%Tp') ��λ�ÿ��������Ի����йء� 
'D'     ���ڣ�����ʽ��Ϊ "%tm/%td/%ty"�� 
'F'     ISO 8601 ��ʽ���������ڣ�����ʽ��Ϊ "%tY-%tm-%td"�� 
'c'     ���ں�ʱ�䣬����ʽ��Ϊ "%ta %tb %td %tT %tZ %tY"������ "Sun Jul 20 16:17:00 EDT 1969"�� 
--
�� ��Դ:�������ʵBBS http://bbs.tju.edu.cn [FROM: 163.135.10.36]
Technical Details
Message-ID: <4dTAai$AOI@bbs.tju.edu.cn>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!news.glorb.com!news.neu.edu.cn!news.byr.edu.cn!TJU

Show Full Headers
Complete Article Headers
X-Google-Thread: f6cb0,f6d34ceb5d7b7340,start
X-Google-Attributes: gidf6cb0,domainid0,public,usenet
X-Google-Language: Chinese,GB
Path: arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!news.glorb.com!news.neu.edu.cn!news.byr.edu.cn!TJU
From: barani.bbs@bbs.tju.edu.cn.no.spam (�����̶�)
Newsgroups: cn.bbs.comp.lang.java
Subject: JAVA String.format ����ʹ�ý���(zz)
Date: 17 Dec 2008 01:07:56 GMT
Organization: �����ʵBBSվ
Message-ID: <4dTAai$AOI@bbs.tju.edu.cn>
Mime-Version: 1.0
Content-Type: text/plain; charset="gb2312"
Content-Transfer-Encoding: 8bit
X-Filename: Java/M.1229476076.A