Java String.Format() ·½·¨¼°²ÎÊý˵Ã÷
JDK1.5ÖУ¬StringÀàÐÂÔöÁËÒ»¸öºÜÓÐÓõľ²Ì¬·½·¨String.format():
format(Locale l, String format, Object... args) ʹÓÃÖ¸¶¨µÄÓïÑÔ»·¾³¡¢¸ñʽ×Ö·û´®ºÍ²ÎÊý·µ»ØÒ»¸ö¸ñʽ»¯×Ö·û´®¡£
format(String format, Object... args) ʹÓÃÖ¸¶¨µÄ¸ñʽ×Ö·û´®ºÍ²ÎÊý·µ»ØÒ»¸ö¸ñʽ»¯×Ö·û´®¡£
¾Ù¼¸¸öÕâ¸ö·½·¨ÊµÓõÄÀý×Ó(×¢ÊÍÊÇÊä³ö½á¹û)£º
CODE:
long now = System.currentTimeMillis();
String s = String.format("%tR", now); // "15:12"
CODE:
// Current month/day/year
Date d = new Date(now);
s = String.format("%tD", d); // "07/13/04"
CODE:
s = String.format("%,d", Integer.MAX_VALUE); // "2,147,483,647"
CODE:
s = String.format("%05d", 123); // "00123"
ÊDz»ÊǺܷ½±ã£¬ÈÃÈ˶¯ÐÄ°¡£¿¹þ¹þ£¬»¹Óиü¶àµÄЧ¹û!
Æäʵformatº¯ÊýÓÐЩÀàËÆcÓïÑÔÖÐprintfº¯Êý£¬Ò»Ð©¸ñʽ×Ö·û´®Óë C ÀàËÆ£¬µ«ÒѽøÐÐÁËijЩ¶¨ÖÆ£¬ÒÔÊÊÓ¦ Java ÓïÑÔ£¬²¢ÇÒÀûÓÃÁËÆäÖÐһЩÌØÐÔ¡£´Ë·½·¨ÌṩÁ˶Բ¼¾Ö¶ÔÆëºÍÅÅÁеÄÖ§³Ö£¬ÒÔ¼°¶ÔÊýÖµ¡¢×Ö·û´®ºÍÈÕÆÚ/ʱ¼äÊý¾ÝµÄ³£¹æ¸ñʽºÍÌض¨ÓÚÓïÑÔ»·¾³µÄÊä³öµÄÖ§³Ö¡£Ö§³ÖÖîÈç byte¡¢BigDecimal ºÍ Calendar µÈ³£¼û Java ÀàÐÍ¡£
²úÉú¸ñʽ»¯Êä³öµÄÿ¸ö·½·¨¶¼ÐèÒª¸ñʽ×Ö·û´® ºÍ²ÎÊýÁÐ±í¡£¸ñʽ×Ö·û´®ÊÇÒ»¸ö String£¬Ëü¿ÉÒÔ°üº¬¹Ì¶¨Îı¾ÒÔ¼°Ò»¸ö»ò¶à¸öǶÈëµÄ¸ñʽ˵Ã÷·û¡£Ç뿼ÂÇÒÔÏÂʾÀý£º
Calendar c = ...;
String s = String.format("Duke's Birthday: %1$tm %1$te,%1$tY", c);
¸ñʽ×Ö·û´®ÊÇ format ·½·¨µÄµÚÒ»¸ö²ÎÊý¡£Ëü°üº¬Èý¸ö¸ñʽ˵Ã÷·û "%1$tm"¡¢"%1$te" ºÍ "%1$tY"£¬ËüÃÇÖ¸³öÓ¦¸ÃÈçºÎ´¦Àí²ÎÊýÒÔ¼°ÔÚÎı¾µÄʲôµØ·½²åÈëËüÃÇ¡£¸ñʽ×Ö·û´®µÄÆäÓಿ·ÖÊÇ°üÀ¨ "Dukes Birthday: " ºÍÆäËûÈκοոñ»ò±êµã·ûºÅµÄ¹Ì¶¨Îı¾¡£ ²ÎÊýÁбíÓÉ´«µÝ¸øλÓÚ¸ñʽ×Ö·û´®Ö®ºóµÄ·½·¨µÄËùÓвÎÊý×é³É¡£ÔÚÉÏÊöʾÀýÖУ¬²ÎÊýÁбíµÄ´óСΪ 1£¬ÓÉжÔÏó Calendar ×é³É¡£
1.³£¹æÀàÐÍ¡¢×Ö·ûÀàÐͺÍÊýÖµÀàÐ͵ĸñʽ˵Ã÷·ûµÄÓï·¨ÈçÏ£º%[argument_index$][flags][width][.precision]conversion
¿ÉÑ¡µÄ argument_index ÊÇÒ»¸öÊ®½øÖÆÕûÊý£¬ÓÃÓÚ±íÃ÷²ÎÊýÔÚ²ÎÊýÁбíÖеÄλÖᣵÚÒ»¸ö²ÎÊýÓÉ "1$" ÒýÓ㬵ڶþ¸ö²ÎÊýÓÉ "2$" ÒýÓã¬ÒÀ´ËÀàÍÆ¡£
¿ÉÑ¡µÄ flags ÊÇÐÞ¸ÄÊä³ö¸ñʽµÄ×Ö·û¼¯
Ïà¹ØÎĵµ£º
»ª×ÐÔÚÍøÉÏÔĹýÖî¶à¹ØÓÚStringµÄÎÄÕ£¬ÈÔÊÇÓÐÒÉÔÚÐÄÖв»½â£¬ËìÑ°JackÇó»ó¡£
JackµÀ£ººÎ»ó²»½â£¿ÄÃÓëÎÒÃæÇ°±ã¿É¡£
»ª×ÐÊÖÓÚÐÄ£¬È´Äò»³öStringÕâÍæÒâ...JackòÈç´óʦһ̾,½«String³ÊÓÚËûÃæÇ°¡£
“StringÖÐÎÄÃûΪ'×Ö·û´®'£¬Èç´Ë¶øÒÑ£»Éí֮ѪҺ£¬ÄÜÁ¿Ö®ÔØÌå¡£”
1¡¢¼òÊöStringÓëStringBufferµÄÒìͬ¡£
&nb ......
Eclipse Java×¢ÊÍÄ£°åÉèÖÃÏê½â
src url£ºhttp://blog.csdn.net/ahhsxy/archive/2009/09/11/4542682.aspx
ÉèÖÃ×¢ÊÍÄ£°åµÄÈë¿Ú£º Window->Preference->Java->Code Style->Code Template È»ºóÕ¹¿ªComments½Úµã¾ÍÊÇËùÓÐÐèÉèÖÃ×¢Ê͵ÄÔªËØÀ²¡£ÏÖ¾Íÿһ¸öÔªËØÖðÒ»½éÉÜ£º
Îļþ(Files)×¢ÊͱêÇ©£º
/**
......
public void sort(int[] array) {
for(int i=0; i<array.length; i++) {
boolean flag = true;
for(int j=0; j<array.length-i-1; j++) {
if(array[j]>array[j+1]) {
int tmp = ar ......
ÕâÊÇһƪ¹ØÓÚ±àÂëµÄÎÄÕÂдµÄºÜºÃ!
ÊÀ½çÉϵĸ÷µØÇø¶¼Óб¾µØµÄÓïÑÔ¡£µØÇø²îÒìÖ±½Óµ¼ÖÂÁËÓïÑÔ»·¾³µÄ²îÒì¡£ÔÚ¿ª·¢Ò»¸ö¹ú¼Ê»¯³ÌÐòµÄ¹ý³ÌÖУ¬´¦ÀíÓïÑÔÎÊÌâ¾ÍÏԵúÜÖØÒªÁË¡£
¡¡¡¡ÕâÊÇÒ»¸öÊÀ½ç·¶Î§ÄÚ¶¼´æÔÚµÄÎÊÌ⣬ËùÒÔ£¬JavaÌṩÁËÊÀ½çÐԵĽâ¾ö·½·¨¡£±¾ÎÄÃèÊöµÄ·½·¨ÊÇÓÃÓÚ´¦ÀíÖÐÎĵģ¬µ«ÊÇ£¬Íƶø¹ãÖ®£¬¶ÔÓÚ´¦À ......
java µÄÈ¥µô¿Õ¸ñ¿ÕÐеÄÀý×Ó£º
String so = "haolong, hei eip, "
+"ting, " +
& ......