您的位置:首页 > 编程语言 > Java开发

java代码全角半角转换

2015-04-22 15:54 591 查看
/**

* 全角字符串转半角字符串,比如“(”--->"("

*/

public static String full2HalfWidth(String str) {

String outStr = "";

String Tstr = "";

byte[] b = null;

if ( str != null ) {

for (int i = 0; i < str.trim().length(); i++) {

try {

Tstr = str.substring(i, i + 1);

b = Tstr.getBytes("unicode");

} catch (java.io.UnsupportedEncodingException e) {

e.printStackTrace();

}

if ( b[2] == -1 ) {

b[3] = (byte)(b[3] + 32);

b[2] = 0;

try {

outStr = outStr + new String(b, "unicode");

} catch (java.io.UnsupportedEncodingException e) {

e.printStackTrace();

}

} else {

outStr = outStr + Tstr;

}

}

}

return outStr;

}

/**

* 半角字符转成全角字符串,比如"("--->"("

*/

public static String half2FullWidth(String str) {

String outStr = "";

String Tstr = "";

byte[] b = null;

if ( str != null ) {

for (int i = 0; i < str.trim().length(); i++) {

try {

Tstr = str.substring(i, i + 1);

b = Tstr.getBytes("unicode");

} catch (java.io.UnsupportedEncodingException e) {

e.printStackTrace();

}

if ( b[2] == 0 ) {

b[3] = (byte)(b[2] - 32);

b[2] = -1;

try {

outStr = outStr + new String(b, "unicode");

} catch (java.io.UnsupportedEncodingException e) {

e.printStackTrace();

}

} else {

outStr = outStr + Tstr;

}

}

}

return outStr;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: