posts - 41,  comments - 40,  trackbacks - 0

/*******************************************************************************
 *
 * 对数字或文件名排列
 *
 * 没有考虑汉字显示时占两个位置的情况,即对文件名处理不太好
 *
 * Author: NeedJava
 *
 * Modified: 2007.09.09
 *
 ******************************************************************************/

public final class DotAlign
{
  private DotAlign(){}

  private static final String align( String string,
                                     char prefix,
                                     char suffix,
                                     int prefixLength,
                                     int suffixLength,
                                     boolean isNumber )
  {
    if( string==null||string.equals( "" )||string.equals( "." )||prefixLength<0||suffixLength<0 )
      {
        return string;
      }

    //如果没有小数,那么按照常理,小数点也应该省略
    char[] buffer=new char[( suffixLength>0 ? prefixLength+1+suffixLength : prefixLength )];

    int stringLength=string.length();

    int dot=stringLength-1;

    //找到小数点位置,可以用lastIndexOf()代替
    while( dot>-1 )
         {
           if( string.charAt( dot-- )=='.' )
             {
               dot++;

               break;
             }
         }

    //System.out.print( Integer.toString( dot )+"\t" );

    //★整数部分,从小数点左位向左移
    int pointer=( dot>-1 ? dot-1 : stringLength-1 );

    for( int i=prefixLength-1; i>-1; i-- )
       {
         if( pointer>-1 )
           {
             buffer[i]=string.charAt( pointer-- );
           }
         else{ buffer[i]=prefix;
             }
       }

    //如果没有小数部分,那么按照常理,小数点应该省略
    if( suffixLength>0 )
      {
        //★小数点
        buffer[prefixLength]=( dot>-1||isNumber ? '.' : suffix );

        //★小数部分,从小数点右位向右移
        pointer=( dot>-1 ? dot+1 : stringLength );

        for( int i=prefixLength+1; i<buffer.length; i++ )
           {
             if( pointer<stringLength )
               {
                 buffer[i]=string.charAt( pointer++ );
               }
             else{ buffer[i]=suffix;
                 }
           }
      }

    return new String( buffer );
  }


  public final static void main( String[] args )
  {
    System.out.println( "\r\n-------------------------------------------\r\n" );

    String[] number={ "",
                      ".",
                      ".5",
                      ".56",
                      ".567",
                      ".5678",
                      "1234",
                      "1234.",
                      "1234.5",
                      "1234.56",
                      "1234.567",
                      "1234.5678" };

    for( int i=0; i<number.length; i++ )
       {
         System.out.println( "["+DotAlign.align( number[i], ' ', '0', 7, 2, true )+"]   ["+number[i]+"]" );
       }

    System.out.println( "\r\n-------------------------------------------\r\n" );

    String[] file={ "",
                    ".",
                    ".j",
                    ".jp",
                    ".jpe",
                    ".jpeg",
                    "1234",
                    "1234.",
                    "1234.j",
                    "1234.jp",
                    "1234.jpe",
                    "1234.jpeg" };

    for( int i=0; i<file.length; i++ )
       {
         System.out.println( "["+DotAlign.align( file[i], ' ', ' ', 7, 5, false )+"]   ["+file[i]+"]" );
       }

    System.out.println( "\r\n-------------------------------------------\r\n" );
  }
}

posted on 2007-09-14 00:17 NeedJava 阅读(1120) 评论(0)  编辑  收藏 所属分类: Java

只有注册用户登录后才能发表评论。


网站导航: