java中文汉字全角_JAVA中如何处理中文的全角和半角问题- -

/**

*  把给定的中文字符串转化为定宽字符串,若源串长则裁掉,若不足则在尾端补足。

*  保证转化后的字符串只包含中文全角字符。

*/

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(); }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值