字符串对齐-字符串表格

参考《java经典实例》

先定义对齐方式

public enum Justify{
    LEFT,CENTER,RIGHT;
}

创建一个字符串对齐格式类

public class StringAlign{

    private Justify justify;
    private Integer maxLength;
    
    public StringAlign(Integer maxLength,Justify justify) {
        switch (justify){
            case LEFT:
            case RIGHT:
            case CENTER:
                this.justify = justify;
                break;
            default:
                throw new IllegalArgumentException("invalid justification argument");
        }
        if(maxLength<0){
            throw new IllegalArgumentException("invalid maxLength argument");
        }
        this.maxLength = maxLength;
    }

    public String format(Object obj) {
        String s = obj.toString();
        String realStr = s.substring(0,Math.min(s.length(),maxLength));
        StringBuilder formatResult = new StringBuilder();
        switch (justify){
            case LEFT:{
                formatResult.append(realStr);
                pad(formatResult,maxLength-realStr.length());
                break;
            }
            case RIGHT:{
                pad(formatResult,maxLength-realStr.length());
                formatResult.append(realStr);
                break;
            }
            case CENTER:{
                int leftBlank = (maxLength - realStr.length())/2;
                pad(formatResult,leftBlank);
                formatResult.append(realStr);
                pad(formatResult,maxLength-leftBlank-realStr.length());
            }
        }
        return formatResult.toString();
    }

    private void pad(StringBuilder str,int size){
        for(int i=0;i<size;i++){
            str.append(" ");
        }
    }
}

在创建字符串表格格式化类

public class StringTableAlign {
    private Justify justify;
    private Integer maxLength;
    private String rowSplit;
    private String cellSplit;
    public StringTableAlign(Justify justify){
        this(justify,Integer.MAX_VALUE);
    }

    public StringTableAlign(Justify justify, Integer maxLength){
        this(justify,maxLength,"-","|");
    }

    public StringTableAlign(Justify justify, Integer maxLength,String rowSplit,String cellSplit) {
        switch (justify){
            case LEFT:
            case RIGHT:
            case CENTER:
                this.justify = justify;
                break;
            default:
                throw new IllegalArgumentException("invalid justification argument");
        }
        if(maxLength<0){
            throw new IllegalArgumentException("invalid maxLength argument");
        }
        this.maxLength = maxLength;
        this.rowSplit = rowSplit;
        this.cellSplit = cellSplit;
    }

    // header 可能为空,data不能为空
    public String format(List<String> header,List<List<String>> data){
        if(data == null || data.size() == 0){
            throw new IllegalArgumentException("data should not be empty");
        }
        List<String> allData = new ArrayList<>();
        if(header != null){
            allData.addAll(header);
        }
        for(List<String> row  : data){
            allData.addAll(row);
        }
        int maxLength = getCellMaxLength(allData);
        int rowLength = ((data.get(0).size())*maxLength)+((data.get(0).size()+1)*cellSplit.length());
        StringBuilder formatTableResult = new StringBuilder();
        StringAlign stringAlign = new StringAlign(maxLength,justify);

        formatTableResult.append(duplicateSymbol(rowSplit,rowLength));
        formatTableResult.append("\n");
        if(header != null && header.size()>0){
            formatTableResult.append(printRow(header,stringAlign));
        }
        for(int i=0;i<data.size();i++){
            formatTableResult.append(printRow(data.get(i),stringAlign));
            formatTableResult.append(duplicateSymbol(rowSplit,rowLength));
            formatTableResult.append("\n");
        }

        return  formatTableResult.toString();
    }


    private String printRow(List<String> data,StringAlign stringAlign) {
        StringBuilder headerBuilder = new StringBuilder();
        for (int i = 0; i < data.size(); i++) {
            if (i == 0) {
                headerBuilder.append(cellSplit);
            }
            headerBuilder.append(stringAlign.format(data.get(i)));
            headerBuilder.append(cellSplit);
        }
        headerBuilder.append("\n");
        return headerBuilder.toString();
    }

    private String duplicateSymbol(String symbol,int count){
        StringBuilder stringBuilder = new StringBuilder();
        for(int i=0;i<count;i++){
            stringBuilder.append(symbol);
        }
        return stringBuilder.toString();
    }

    private Integer getCellMaxLength(List<String> allData){
        int cellMaxLength = 0;
        for(String cellItem : allData){
            if(cellMaxLength<cellItem.length()){
                cellMaxLength = cellItem.length();
            }
        }
        if(cellMaxLength>maxLength){
            throw new IllegalArgumentException("cell length is bigger than limited maxLength");
        }
        return cellMaxLength;
    }
}

调用方式

public static void main(String[] args){

        List<String> header = new ArrayList<String>(){{
            add("name");add("age");add("location1231231231523sdfgsadfadf");
        }};
        List<List<String>> allData = new ArrayList<>();
        List<String> data = new ArrayList<String>(){{
            add("zhang3");add("26");add("sx");
        }};
        List<String> data1 = new ArrayList<String>(){{
            add("zhang4");add("27");add("zj");
        }};
        List<String> data2 = new ArrayList<String>(){{
            add("zhang5");add("28");add("hz");
        }};
        allData.add(data);
        allData.add(data1);
        allData.add(data2);
        StringTableAlign stringTableAlign = new StringTableAlign(Justify.CENTER);
        System.out.println(stringTableAlign.format(header, allData));
    }

结果

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值