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("\n"); 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