How to implement _java for Java full-width half-width character conversion

Source: Internet
Author: User
Copy Code code as follows:

Package com.whatycms.common.util;
Import Org.apache.commons.lang.StringUtils;
/**
* <PRE>
* Provides full angle->, half-width-> full angle conversion for strings
* </PRE>
*/
public class Bcconvert {
/**
* The visible characters from the ASCII table start with the offset shift value of (Decimal)
*/
Static final char Dbc_char_start = 33; Half a corner!
/**
* The visible character in the ASCII table ends with the offset value of 126 (Decimal)
*/
Static final char dbc_char_end = 126; Half a Corner ~
/**
* Full angle corresponds to the ASCII table visible characters from! Start with an offset value of 65281
*/
Static final char Sbc_char_start = 65281; Full horn!
/**
* The full angle corresponds to the ASCII table's visible character to the end, the offset value is 65374
*/
Static final char sbc_char_end = 65374; All Corners ~
/**
* The relative offset of the extra visible characters in the ASCII table and the corresponding full-width characters
*/
static final int convert_step = 65248; Full-angle half-angle conversion interval
/**
* The value of a full-width space that does not conform to the relative offset of ASCII and must be handled separately
*/
Static final char sbc_space = 12288; Full-width Space 12288
/**
* The value of a half-width space, in ASCII (Decimal)
*/
Static final char dbc_space = '; Half-corner space
/**
* <PRE>
* Half-width character-> full-angle character conversion
* Only handle spaces,! To ˜ between the characters, ignoring the 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 half-width space, use a full-width space instead
Buf.append (Sbc_space);
else if ((Ca[i] >= dbc_char_start) && (ca[i) <= dbc_char_end)) {//character is! To ~ between the visible characters
Buf.append ((char) (Ca[i] + convert_step));
else {//do not treat spaces and characters other than other visible characters in the ASCII table
Buf.append (Ca[i]);
}
}
return buf.tostring ();
}
/**
* <PRE>
* Full-angle character-> half-angle character conversion
* Deal with all corners of the space, all corners! to the Full-width ~ between the characters, ignoring the 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 located in full corner! to the full angle ~ 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, all corners! To the full corner of the character 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 | nihehe,". 78 7 ";
S=BCCONVERT.QJ2BJ (s);
System.out.println (s);
System.out.println (BCCONVERT.BJ2QJ (s));
}
}

The console output is as follows:
Copy Code code as follows:

A,b, C
Nihaohk | Nihehe,. 78 7
Nihaohk | Nihehe,. 78 7
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.