Copy codeThe Code is as follows: package com. whatycms. common. util;
Import org. apache. commons. lang. StringUtils;
/**
* <PRE>
* Returns the string's fullwidth, halfwidth, and fullwidth.
* </PRE>
*/
Public class BCConvert {
/**
* Visible characters in the ASCII table are from! Start with the offset value 33 (Decimal)
*/
Static final char DBC_CHAR_START = 33; // halfwidth!
/**
* Visible characters in the ASCII table ~ End. The offset value is 126 (Decimal)
*/
Static final char DBC_CHAR_END = 126; // halfwidth ~
/**
* The full angle corresponds to the visible characters in the ASCII table from! Start with the offset of 65281
*/
Static final char SBC_CHAR_START = 65281; // full width!
/**
* The full angle corresponds to the visible characters in the ASCII table ~ End, offset 65374
*/
Static final char SBC_CHAR_END = 65374; // fullwidth ~
/**
* Relative offset between visible characters except spaces in the ASCII table and corresponding full-angle characters
*/
Static final int CONVERT_STEP = 65248; // full-width half-width conversion Interval
/**
* The Space Value of the fullwidth. It does not comply with the relative offset of ASCII and must be processed separately.
*/
Static final char SBC_SPACE = 12288; // full-width space 12288
/**
* The value of halfwidth space. The value is 32 (Decimal) in ASCII)
*/
Static final char DBC_SPACE = ''; // halfwidth Space
/**
* <PRE>
* Halfwidth character-> fullwidth character conversion
* Only spaces are processed ,! To the delimiter, ignore other
* </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 halfwidth space, replace it with a fullwidth space.
Buf. append (SBC_SPACE );
} Else if (ca [I]> = DBC_CHAR_START) & (ca [I] <= DBC_CHAR_END) {// The character is! To ~ Between visible characters
Buf. append (char) (ca [I] + CONVERT_STEP ));
} Else {// do not process any characters other than spaces or other visible characters in the ascii table
Buf. append (ca [I]);
}
}
Return buf. toString ();
}
/**
* <PRE>
* Fullwidth character-> halfwidth character conversion
* Only spaces with full corners are processed! To fullwidth ~ And ignore other
* </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 it is in the full corner! To fullwidth ~ Within the interval
Buf. append (char) (ca [I]-CONVERT_STEP ));
} Else if (ca [I] = SBC_SPACE) {// if it is a fullwidth Space
Buf. append (DBC_SPACE );
} Else {// do not process space at all angles! To fullwidth ~ Characters out of the specified 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 | nihehe ,. 78 7 ";
S = BCConvert. qj2bj (s );
System. out. println (s );
System. out. println (BCConvert. bj2qj (s ));
}
}
The console output is as follows:Copy codeThe Code is as follows: a, B, c
Nihaohk | nihehe ,. 78 7
Nihaohk | nihehe ,. 78 7