java中全角半角字符的相互转换的代码

时间:2023-12-29 20:26:44

如下内容是关于java中全角半角字符的相互转换的内容。
package com.whatycms.common.util;

import org.apache.commons.lang.StringUtils;

public class BCConvert {

private static String bj2qj(String src) {
if (src == null) {
return src;
}
StringBuilder buf = new StringBuilder(src.length());
char[] ca = src.toCharArray();
for (int i = 0; i < ca.length; i++) {
buf.append(SBC_SPACE);
buf.append((char) (ca[i] + CONVERT_STEP));
buf.append(ca[i]);
}
}
return buf.toString();
}

public static String qj2bj(String src) {
if (src == null) {
return src;
}
StringBuilder buf = new StringBuilder(src.length());
char[] ca = src.toCharArray();
for (int i = 0; i < src.length(); i++) {
buf.append((char) (ca[i] - CONVERT_STEP));
buf.append(DBC_SPACE);
buf.append(ca[i]);
}
}
return buf.toString();
}

public static void main(String[] args) {
System.out.println(StringUtils.trimToEmpty(" a,b ,c "));
String s = "nihaohk |   nihehe ,。 78  7 ";
s=BCConvert.qj2bj(s);
System.out.println(s);
System.out.println(BCConvert.bj2qj(s));
}
}

输出结果如下:

a,b ,c
nihaohk | nihehe ,。 78 7
nihaohk |   nihehe ,。 78  7