参考《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));
}
结果