Copy the code code as follows:
package com.whatycms.common.util;
import org.apache.commons.lang.StringUtils;
/**
* <PRE>
* Provide full-width -> half-width, half-width -> full-width conversion for strings
* </PRE>
*/
public class BCConvert {
/**
* Visible characters in the ASCII table start from !, and the offset value is 33 (Decimal)
*/
static final char DBC_CHAR_START = 33; // Half-width!
/**
* The visible characters in the ASCII table end with ~, and the offset value is 126 (Decimal)
*/
static final char DBC_CHAR_END = 126; // half-width~
/**
* Full-width corresponds to the visible characters of the ASCII table from! Start with offset value 65281
*/
static final char SBC_CHAR_START = 65281; // Full width!
/**
* Full-width corresponds to the visible characters of the ASCII table to the end of ~, the offset value is 65374
*/
static final char SBC_CHAR_END = 65374; // Full width ~
/**
* The relative offset between visible characters in the ASCII table except spaces and the corresponding full-width characters
*/
static final int CONVERT_STEP = 65248; // Full-width and half-width conversion interval
/**
* The value of full-width spaces, which does not comply with the relative offset from ASCII, must be processed separately
*/
static final char SBC_SPACE = 12288; // Full-width space 12288
/**
* The value of half-width space, which is 32 (Decimal) in ASCII
*/
static final char DBC_SPACE = ' '; // half-width space
/**
* <PRE>
* Half-width character->full-width character conversion
* Only processes spaces, characters between ! and ˜, ignores others
* </PRE>
*/
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++) {
if (ca[i] == DBC_SPACE) { // If it is a half-width space, directly replace it with a full-width space
buf.append(SBC_SPACE);
} else if ((ca[i] >= DBC_CHAR_START) && (ca[i] <= DBC_CHAR_END)) { // Characters are visible characters between ! to ~
buf.append((char) (ca[i] + CONVERT_STEP));
} else { // Do not do any processing for characters other than spaces and other visible characters in the ascii table
buf.append(ca[i]);
}
}
return buf.toString();
}
/**
* <PRE>
* Full-width character->half-width character conversion
* Only handle full-width spaces, full-width! to characters between full-width ~, and other characters are ignored
* </PRE>
*/
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++) {
if (ca[i] >= SBC_CHAR_START && ca[i] <= SBC_CHAR_END) { // If in full width! to full width ~ range
buf.append((char) (ca[i] - CONVERT_STEP));
} else if (ca[i] == SBC_SPACE) { // If it is a full-width space
buf.append(DBC_SPACE);
} else { //Do not handle full-width spaces, full-width! to full-width ~ characters outside the range
buf.append(ca[i]);
}
}
return buf.toString();
}
public static void main(String[] args) {
System.out.println(StringUtils.trimToEmpty(" a,b ,c "));
String s = "nihaohk | nihehehe ,. 78 7 ";
s=BCConvert.qj2bj(s);
System.out.println(s);
System.out.println(BCConvert.bj2qj(s));
}
}
The console output is as follows:
Copy the code code as follows:
a,b,c
nihaohk | nihehe,. 78 7
nhhahk|| nihehe,. 78 7