add StringAlign

dev
ehlxr 2017-08-17 17:24:15 +08:00
parent 6f6fb864ab
commit 26176b16b8
1 changed files with 164 additions and 0 deletions

View File

@ -0,0 +1,164 @@
package me.ehlxr.utils;
/**
* Created by lixiangrong on 2017/8/17.
*
*/
public class StringAlign {
/**
*
*/
public static final int JUST_LEFT = 0;
/**
*
*/
public static final int JUST_CENTER = 1;
/**
*
*/
public static final int JUST_RIGHT = 2;
/**
*
*/
private int just;
/**
*
*/
private int maxChars;
/**
*
*/
public StringAlign() {
// 默认为居中对齐
this.just = JUST_CENTER;
// 默认一行的最大长度为80
this.maxChars = 80;
}
/**
*
*
* @param maxChars
* @param just
*/
public StringAlign(int maxChars, int just) {
// 首先构造一个默认字符串对齐器
this();
// 根据传入参数修改字符串对齐器的属性
this.setJust(just);
this.setMaxChars(maxChars);
}
public static void main(String[] args) {
// 一行最多70个字符居中显示。
StringAlign formatter = new StringAlign(20, StringAlign.JUST_CENTER);
// 比如显示页码
System.out.println(formatter.format("- i -"));
System.out.println(formatter.format(Integer.toString(444)));
System.out.println(formatter.format("kkkkkkkkkkkkkkkkkkkkkkkkkkkk"));
// 左对齐
System.out.println();
formatter = new StringAlign(20, StringAlign.JUST_LEFT);
System.out.println(formatter.format("- i -") + "1");
System.out.println(formatter.format(Integer.toString(444)) + "1");
System.out.println(formatter.format("kkkkkkkkkkkkkkkkkkkkkkkkkkkk") + "1");
// 右对齐
System.out.println();
formatter = new StringAlign(20, StringAlign.JUST_RIGHT);
System.out.println(formatter.format("- i -"));
System.out.println(formatter.format(Integer.toString(444)));
System.out.println(formatter.format("kkkkkkkkkkkkkkkkkkkkkkkkkkkk"));
}
/**
*
*
* @param s
*/
public String format(String s) {
StringBuffer where = new StringBuffer();
// 从待对齐的字符串中取出一段子字符串子串的长度为行最大长度和s长度的较小值
int wantedLength = Math.min(s.length(), this.maxChars);
String wanted = s.substring(0, wantedLength);
// 根据对齐模式,将空格放在合适的位置
switch (this.just) {
case JUST_RIGHT:
// 如果是右对齐,那么将缺少的的字符用空格代替放在左边
pad(where, maxChars - wantedLength);
// 将字符串添加在右边
where.append(wanted);
break;
case JUST_CENTER:
// 居中对齐,将空格字符平均分在字符串两边。
int startPos = where.length();
pad(where, (maxChars - wantedLength) / 2);
where.append(wanted);
pad(where, (maxChars - wantedLength) / 2);
// 调整舍入误差
pad(where, maxChars - (where.length() - startPos));
break;
case JUST_LEFT:
// 左对齐,将空格字符放在字符串右边。
where.append(wanted);
pad(where, maxChars - wantedLength);
break;
}
// 如果原字符串的长度大于一行的最大长度,则将余下部分放入下一行
if (s.length() > wantedLength) {
String remainStr = s.substring(wantedLength);
where.append("\n").append(this.format(remainStr));
}
return where.toString();
}
/**
* toappend howMany
*
* @param to
* @param howMany
*/
protected final void pad(StringBuffer to, int howMany) {
for (int i = 0; i < howMany; i++)
to.append(" ");
}
public int getJust() {
return just;
}
/**
*
*
* @param just
*/
public void setJust(int just) {
switch (just) {
case JUST_LEFT:
case JUST_CENTER:
case JUST_RIGHT:
this.just = just;
break;
default:
System.out.println("invalid justification arg.");
}
}
public int getMaxChars() {
return maxChars;
}
/**
*
*
* @param maxChars
*/
public void setMaxChars(int maxChars) {
if (maxChars < 0) {
System.out.println("maxChars must be positive.");
} else {
this.maxChars = maxChars;
}
}
}