/**
* 把给定的中文字符串转化为定宽字符串,若源串长则裁掉,若不足则在尾端补足。
* 保证转化后的字符串只包含中文全角字符。
*/
public static String fixedWidthStringDBCS(String str, int width, char fill) {
if (str==null) str = "";
if (fill < 256) fill = ' ';
StringBuffer sb = new StringBuffer();
int i=0;
for ( ; i
int c = str.charAt(i);
if (c >= 'a' && c<='z') { //azAZ 019
c = c + 'a' - 'a';
} else if (c >= 'A' && c<= 'Z') {
c = c + 'A' - 'A';
} else if (c >= '0' && c<= '9') {
c = c + '0' - '0';
} else if (c < 256) {
c = fill;
}
sb.append((char)c);
}
for ( ; i
sb.append(fill);
}
return sb.toString();
}
我改写的一个简便功能的方法:
public String fixedWidthStringDBCS(String str) {
if (str==null) str = "";
int width = str.length();
StringBuffer sb = new StringBuffer();
int i=0;
for ( ; i
int c = str.charAt(i);
if (c >= 'a' && c<='z') { //azAZ 019
c = c + 'a' - 'a';
} else if (c >= 'A' && c<= 'Z') {
c = c + 'A' - 'A';
} else if (c >= '0' && c<= '9') {
c = c + '0' - '0';
}
sb.append((char)c);
}
return sb.toString(); }