• 字符串对齐器


      1 package com.jdk7.chapter5;
      2 
      3 public class StringAlignTest {
      4     //用于默认构造函数中对齐方式,以及switch时的case
      5     public static final int LEFT = 0;
      6     public static final int CENTER = 1;
      7     public static final int RIGHT = 2;
      8     
      9     //初始化值用于清空缓存值
     10     private int just = -1;
     11     private int maxChar = -1;
     12     
     13     //默认构造函数
     14     public StringAlignTest(){
     15         this.just = CENTER;
     16         this.maxChar = 80;
     17     }
     18     
     19     //带参构造函数,先调用一个默认构造函数,如果参数不符合,则可以调用默认构造函数赋的值
     20     public StringAlignTest(int just, int maxChar){
     21         //如果注释默认构造函数,且自定义传入的参数都不符合条件则不会调用对齐方式函数format
     22         this();
     23         this.setJust(just);
     24         this.setMaxChar(maxChar);
     25     }
     26     
     27     public String format(String str){
     28         if(str==null){
     29             System.out.println("string cannot be null!");
     30         }
     31         StringBuffer sb = new StringBuffer();
     32         int wantedLength = Math.min(str.length(), this.maxChar);
     33         String want = str.substring(0, wantedLength);
     34         switch(this.just){
     35         case LEFT:
     36             sb.append(want);
     37             pad(sb,this.maxChar-wantedLength);
     38             break;
     39             
     40         case CENTER:
     41             int num = (this.maxChar-wantedLength)/2;
     42             pad(sb,num);
     43             sb.append(want);
     44             pad(sb,num);
     45             break;
     46             
     47         case RIGHT:
     48             pad(sb,this.maxChar-wantedLength);
     49             sb.append(want);
     50             break;
     51         }
     52         if(str.length()>this.maxChar){
     53             sb.append("
    ");
     54             //将原始字符串分为两段,第一段为最大长度的行,另一段为剩下的字符串,把剩下的字符串循环作为原始字符串直到完成所有的换行
     55             String reformat = str.substring(wantedLength);
     56             sb.append(this.format(reformat));
     57         }
     58         return sb.toString();
     59     }
     60     
     61     protected final void pad(StringBuffer s, int howMany){
     62         for(int i=0;i<howMany;i++){
     63             s.append(" ");
     64         }
     65     }
     66 
     67     public int getJust() {
     68         return just;
     69     }
     70 
     71     public void setJust(int just) {
     72         switch(just){
     73         case LEFT:
     74         case CENTER:
     75         case RIGHT:
     76             this.just = just;
     77             break;
     78         default:
     79             System.out.println("Invilid just!");
     80         }
     81     }
     82 
     83     public int getMaxChar() {
     84         return maxChar;
     85     }
     86 
     87     public void setMaxChar(int maxChar) {
     88         if(maxChar<=0){
     89             System.out.println("maxChar number must be more than zore!");
     90         }else{
     91             this.maxChar = maxChar;
     92         }
     93     }
     94     
     95     public static void main(String[] args) {
     96         StringAlignTest sat = new StringAlignTest();
     97         StringAlignTest sat1 = new StringAlignTest(5, 150);
     98         
     99         String str = "The class Math contains methods for performing basic numeric operations such as the elementary "
    100                 + "exponential, logarithm, square root, and trigonometric functions. The quality of implementation "
    101                 + "specifications concern two properties, accuracy of the returned result and monotonicity of the";
    102         System.out.println("===============默认对齐方式======================");
    103         System.out.println(sat.format(str));
    104         System.out.println("===============自定义对齐方式======================");
    105         System.out.println(sat1.format(str));
    106         
    107     }
    108     
    109 }
    110 
    111 执行结果:
    112 Invilid just!
    113 ===============默认对齐方式======================
    114 The class Math contains methods for performing basic numeric operations such as 
    115 the elementary exponential, logarithm, square root, and trigonometric functions.
    116  The quality of implementation specifications concern two properties, accuracy o
    117                  f the returned result and monotonicity of the                 
    118 ===============自定义对齐方式======================
    119 The class Math contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric 
    120        functions. The quality of implementation specifications concern two properties, accuracy of the returned result and monotonicity of the       
  • 相关阅读:
    文件名中含有空格读取时产生的异常
    R 常用清洗函数汇总
    Fluid 0.4 新版本正式发布:支持数据预热,优化小文件场景
    阿里云 Serverless 再升级,从体验上拉开差距
    Dubbo-go 源码笔记(二)客户端调用过程
    高质量的缺陷分析:让自己少写 bug
    微服务框架 Go-Micro 集成 Nacos 实战之服务注册与发现
    OpenYurt 深度解读:如何构建 Kubernetes 原生云边高效协同网络?
    在大规模 Kubernetes 集群上实现高 SLO 的方法
    双十一购物节,Nacos 1.4.0 + Go SDK 1.0.1发布
  • 原文地址:https://www.cnblogs.com/celine/p/8463893.html
Copyright © 2020-2023  润新知