import org.apache.commons.lang.StringUtils;
/**
* <code>StringTools.java</code>
* <p>功能:
*
* <p>Copyright 三泰电子 2016 All right reserved.
* @author zhaochunjian
* @version 1.0
* </br>最后修改人 无
*/
public class StringTools {
/**
* 右增加字节 注意:不能用StringUtils,这是按照字符处理的
* @param s
* @param length
* @return
*/
public static String rightAddStr(String s,int length){
if(length==0){
return s;
}
int len = s.getBytes().length;
if(len<length){
int dif=length-len;
for(int i=0;i<dif;i++){
s+=" ";
}
return s;
}else if(len>length){
s=subStringByByte(s, length);
s=rightAddStr(s,length);
return s;
}
return s;
}
public static String rightAddStr(String s,int length,String add){
int len = s.getBytes().length;
if(len<length){
int dif=length-len;
for(int i=0;i<dif;i++){
s+=add;
}
return s;
}else if(len>length){
s=s.substring(0, length);
return s;
}
return s;
}
/**
* 左边加字节
* @param s
* @param length
* @return
*/
public static String leftAddStr(String s,int length){
if(s==null){
return s;
}
int len = s.getBytes().length;
if(len<length){
int dif=length-len;
for(int i=0;i<dif;i++){
s=" "+s;
}
return s;
}else if(len>length){
s=subStringByByte(s, length);
return s;
}
return s;
}
public static String leftAddStr(String s,int length,String add){
if(s==null){
return s;
}
int len = s.getBytes().length;
if(len<length){
int dif=length-len;
for(int i=0;i<dif;i++){
s=add+s;
}
return s;
}else if(len>length){
s=s.substring(0, length);
return s;
}
return s;
}
/**
* 按字节截取,处理有中文的情况
* @param str
* @param len
* @return
*/
private static String subStringByByte(String str, int len) {
String result = null;
if (str != null) {
byte[] a = str.getBytes();
if (a.length <= len) {
result = str;
} else if (len > 0) {
result = new String(a, 0, len);
int length = result.length();
if (str.charAt(length - 1) != result.charAt(length - 1)) {
if (length < 2) {
result = null;
} else {
result = result.substring(0, length - 1);
}
}
}
}
return result;
}
/**
* 功能:字符居中
* <p>作者 xuecc 2016-8-2 下午3:32:10
* @param str 居中字符串
* @param len 总长
* @return
*/
public static String middleStr(String str,int len){
if(StringUtils.isEmpty(str)){
return str;
}
int strLen = str.getBytes().length;
if(len>strLen){
int diff = len-strLen;
int needApp = diff/2+strLen;
return leftAddStr(str, needApp)+rightAddStr(" ", diff/2);
}
return str;
}
/**
* 功能:字符居中
* <p>作者 xuecc 2016-8-2 下午4:16:21
* @param str 居中字符
* @param len 总长
* @param ap 补位符
* @return
*/
public static String middleStr(String str,int len,String ap){
if(str==null){
return str;
}
int strLen = str.getBytes().length;
if(len>strLen){
int diff = len-strLen;
int needApp = diff/2+strLen;
return leftAddStr(str, needApp,ap)+rightAddStr(" ", diff/2,ap);
}
return str;
}
// 根据Unicode编码完美的判断中文汉字和符号
private static boolean isChinese(char c) {
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B
|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) {
return true;
}
return false;
}
/**
* 功能:完整的判断中文汉字和符号
* <p>作者 xuecc 2016-8-5 下午3:04:00
* @param strName
* @return
*/
public static boolean isChinese(String strName) {
if(StringUtils.isEmpty(strName)){
return false;
}
char[] ch = strName.toCharArray();
for (int i = 0; i < ch.length; i++) {
char c = ch[i];
if (isChinese(c)) {
return true;
}
}
return false;
}
public static String sameStrLen(String str,int len){
if(isChinese(str)){
int strLen = str.getBytes().length;
int pad = (len-strLen)+(strLen/2);
return StringUtils.rightPad(str, pad);
}else{
return StringUtils.rightPad(str, len);
}
}
public static void main(String[] args) {
String ss="中国";
String s="zg";
System.out.println("<"+sameStrLen(ss,10)+">");
System.out.println("<"+sameStrLen(s,10)+">");
}
}
package com.isantai.test;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.isantai.dao.T_Receipt_DataDao;
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);
}
/**
* 对齐一个字符串
*
* @param obj
* 待对齐的字符串
*/
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" + this.format(remainStr));
}
return where.toString();
}
/**
* 在to的后面append 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;
}
}
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 -"));
System.out.println(formatter.format(Integer.toString(444)));
System.out.println(formatter.format("kkkkkkkkkkkkkkkkkkkkkkkkkkkk"));
// 右对齐
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"));
}
}