Java full-width conversion

Source: Internet
Author: User

Solution 1:

 

/**

* Full-width to half-width symbols, numbers, uppercase letters:-1, low-width to 32 h less than half-width. Convert a-Z, A-Z, 0-9 to a half * angle. ** @ Param sbcstring * @ return halfwidth string * @ author gyb */public static string sbc2dbc (string sbcstring) {If (sbcstring = NULL) {return NULL ;} stringbuffer dbcstring = new stringbuffer (""); try {byte [] bytes = NULL; For (INT I = 0; I <sbcstring. length (); I ++) {string temp = sbcstring. substring (I, I + 1); Boolean iscontainletters = pattern. matches ("[0-9a-za-z]", temp); If (! Iscontainletters) {bytes = temp. getbytes ("Unicode"); If (Bytes [3] =-1) {bytes [2] = (byte) (Bytes [2] + 32 ); bytes [3] = 0;} dbcstring. append (new string (bytes, "Unicode");} else {dbcstring. append (temp) ;}} catch (unsupportedencodingexception e) {// todo auto-generated Catch Block E. printstacktrace ();} return dbcstring. tostring ();}/*** it refers to the half-width to the full-width. It is a number with a letter high of 0 and a half-width to 32 h more than the full-width. Convert the letter A-Z, A-Z, 0-9 out of the symbols into * for full angle, database storage. * @ Param dbcstring * @ return full-width string ** @ author gyb */public static string dbc2sbc (string dbcstring) {If (dbcstring = NULL) {return NULL ;} stringbuffer sbcstring = new stringbuffer (""); try {byte [] bytes = NULL; For (INT I = 0; I <dbcstring. length (); I ++) {string temp = dbcstring. substring (I, I + 1); Boolean iscontainletters = pattern. matches ("[0-9a-za-z]", temp); If (! Iscontainletters) {bytes = temp. getbytes ("Unicode"); If (Bytes [3] = 0) {bytes [2] = (byte) (Bytes [2]-32 ); bytes [3] =-1;} sbcstring. append (new string (bytes, "Unicode");} else {sbcstring. append (temp) ;}} catch (unsupportedencodingexception e) {// todo auto-generated Catch Block E. printstacktrace ();} return sbcstring. tostring ();} solution 2:

Package test;

Import java. util .*;

Public class multidimarray {
/**
* Halfwidth fullwidth conversion and replacement of halfwidth fullwidth white space
* @ Author maochangming
*/

Public static void main (string [] ARGs) throws exception {
Try {
// Remove spaces at the beginning and end, whether it is full-width half-width:
String S = "nihaohk nihehe ";
System. Out. println ("S. Length =" + S. Length ());
// S = S. replaceall ("^ // s * | // s * $ ","");
S = S. replaceall ("(^ [|] * | [|] * $ )","");
S = S. replaceall ("","");
System. Out. println ("S. Length =" + S. Length ());
System. Out. println ("s =" + S );

String qjstr = "hello ";
String qjstr1 = "hello ";

String result = bqchange (qjstr );
String result1 = qbchange (qjstr1 );

System. Out. println (qjstr + "/N" + result );
System. Out. println (qjstr1 + "/N" + result1 );
} Catch (exception ex ){
Throw new Exception ("ERROR:" + ex. getMessage ());
}
}

/**
* Halfwidth to fullwidth
* @ Param QJstr
* @ Return
*/
Public static final String BQchange (String QJstr ){
String outStr = "";
String Tstr = "";
Byte [] B = NULL;

For (INT I = 0; I <qjstr. Length (); I ++ ){
Try {
Tstr = qjstr. substring (I, I + 1 );
B = tstr. getbytes ("Unicode ");
} Catch (Java. Io. unsupportedencodingexception e ){
E. printStackTrace ();
}

If (B [3]! =-1 ){
B [2] = (byte) (B [2]-32 );
B [3] =-1;
Try {
OutStr = outStr + new String (B, "unicode ");
} Catch (java. io. UnsupportedEncodingException e ){
E. printStackTrace ();
}
} Else
OutStr = outStr + Tstr;
}

Return outStr;
}

/**
* Full-width to half-width
* @ Param QJstr
* @ Return
*/
Public static final String QBchange (String QJstr ){
String outStr = "";
String Tstr = "";
Byte [] B = NULL;

For (INT I = 0; I <qjstr. Length (); I ++ ){
Try {
Tstr = qjstr. substring (I, I + 1 );
B = tstr. getbytes ("Unicode ");
} Catch (Java. Io. unsupportedencodingexception e ){
E. printstacktrace ();
}

If (B [3] =-1 ){
B [2] = (byte) (B [2] + 32 );
B [3] = 0;
Try {
Outstr = outstr + new string (B, "Unicode ");
} Catch (Java. Io. unsupportedencodingexception e ){
E. printstacktrace ();
}
} Else
Outstr = outstr + tstr;
}

Return outstr;
}

}

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.