一. 字符串的对齐方式
我们在Word 文档处理的时候, 对于内容常常有 左对齐, 中间对齐,右对齐 等三种常见的方式。
这种对齐方式,是如何实现的呢? 填充空格。
如果是左对齐, 那么就把 右边的不足部分填充空格。
如果是中间对齐, 那么就把左边填充一半空格, 右边填充一半空格。
如果是右对齐, 那么就把左边的部分填充空格。
老蝴蝶仿照 其他文章,写了字符串对齐方式的工具类。
一.一 字符串对齐工具
package com.yjl.collection; import java.text.FieldPosition; import java.text.Format; import java.text.ParsePosition; /** * package: com.yjl.collection * className: StringAlign * Description: 请输入相应的描述 * * @author : yuezl * @Date :2020/6/10 19:25 */ public class StringAlign extends Format { //定义对齐方式,设置成枚举形式 public enum Justify{ /**左对象*/ LEFT, /**中间对齐*/ CENTER, /**右对齐*/ RIGHT } // 当前的对齐方式 private Justify justify; //一行最大能显示的字符 private int lineMaxLength; public StringAlign() { } public StringAlign(Justify justify, int lineMaxLength) { switch(justify){ case LEFT: case RIGHT: case CENTER: this.justify=justify; break; default:{ throw new IllegalArgumentException("invalid justification arg."); } } if(lineMaxLength<=0){ throw new IllegalArgumentException("lineMaxLength must be positive."); } this.lineMaxLength = lineMaxLength; } //从Format 抽象类中重写的方法 @Override public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) { //1. 将传入的值,变成字符串 String s=obj.toString(); //截取字符串s. String wanted=s.substring(0,Math.min(s.length(),lineMaxLength)); //根据对齐方式,进行设置空格。 switch(justify){ case LEFT:{ //设置进来值 toAppendTo.append(wanted); //右边追加相应的空格 pad(toAppendTo,lineMaxLength-wanted.length()); break; } case CENTER:{ //相差的长度 int toAdd=lineMaxLength-wanted.length(); //左边追加空格 pad(toAppendTo,toAdd/2); //设置内容 toAppendTo.append(wanted); //总的减去左边的 pad(toAppendTo,toAdd-toAdd/2); break; } case RIGHT:{ //先添加空格 pad(toAppendTo,lineMaxLength-wanted.length()); //设置值 toAppendTo.append(wanted); break; } } return toAppendTo; } /** * 追加空格 * @param to 字符串 * @param howMany 要放置几个空格 */ private final void pad(StringBuffer to,int howMany){ for(int i=0;i<howMany;i++){ to.append(" "); } } //从Format 抽象类中 重写的方法 @Override public Object parseObject(String source, ParsePosition pos) { return source; } /** * 格式化展示对齐方式 * @param str 要对齐的字符串 * @return 返回对齐格式化后字符串 */ public String format(String str){ if(str==null){ return null; } if(str.length()==0||str.trim().length()==0){ return ""; } //获取长度 int countLength=str.length(); //可以占几行 int circleCount=countLength/lineMaxLength; StringBuffer sb=new StringBuffer(); for(int i=0;i<circleCount;i++){ sb.append(str.substring(0,lineMaxLength)).append("\r\n"); str=str.substring(lineMaxLength); } return format(str,sb,null).toString(); } /** * * @return 对齐方式 */ public Justify getJustify() { return justify; } /** * * @param justify 设置对齐方式 */ public void setJustify(Justify justify) { this.justify = justify; } /** * * @return 返回当前行最多能显示的字符个数 */ public int getLineMaxLength() { return lineMaxLength; } /** * * @param lineMaxLength 行最多显示的字符个数 */ public void setLineMaxLength(int lineMaxLength) { this.lineMaxLength = lineMaxLength; } }
一.二 测试对齐方式
package com.yjl.collection; import org.junit.Test; /** * package: com.yjl.collection * className: StringAlignTest * Description: 请输入相应的描述 * * @author : yuezl * @Date :2020/6/10 19:44 */ public class StringAlignTest { public static void main(String[] args) { //要对齐的字符串 String s="Hello,My Name is TwoButterfly, Chinese Name is YueZL. I love China,love Java,love World!!!"; //设置对象 StringAlign stringAlign=new StringAlign(StringAlign.Justify.LEFT,20); //左对齐 String left=stringAlign.format(s); System.out.println("左对齐:\n"+left); //设置成中间对齐 stringAlign.setJustify(StringAlign.Justify.CENTER); String center=stringAlign.format(s); System.out.println("中间对齐:\n"+center); //设置成右对齐 stringAlign.setJustify(StringAlign.Justify.RIGHT); String right=stringAlign.format(s); System.out.println("右对齐:\n"+right); } }
控制台打印输出:
谢谢您的观看,如果喜欢,请关注我,再次感谢 !!!