Java illegal character filtering, half-width/full-width substitution

Source: Internet
Author: User

Java illegal character filtering, half-width/full-width substitution

 PackageMjorcen.netty.test1;Importjava.io.UnsupportedEncodingException; Public classCharbyteconverter {/*** Full-width turning half-width * *@paramSTR *@return     *      * @authorMjorcen * @email [email protected] * @dateTime SEP, 2:51:50 PM *@version1*/@Deprecated Public Static Finalstring Tosinglebyte (String str) {StringBuffer outstrbuf=NewStringBuffer (""); String Tstr= ""; byte[] B =NULL;  for(inti = 0; I < str.length (); i++) {Tstr= Str.substring (I, i + 1); //Full-width spaces converted into half-width spaces            if(Tstr.equals ("") {outstrbuf.append (" "); Continue; }            Try{b= Tstr.getbytes ("Unicode"); //get Unicode byte data                if(b[2] = =-1) {                    //indicates full-widthB[3] = (byte) (B[3] + 32); b[2] = 0; Outstrbuf.append (NewString (b, "Unicode")); } Else{outstrbuf.append (TSTR); }            } Catch(unsupportedencodingexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }        } //end for.        returnoutstrbuf.tostring (); }    /*** Half Corner turn full angle * *@paramSTR *@return     *      * @authorMjorcen * @email [email protected] * @dateTime SEP, 2:52:06 PM *@version1*/@Deprecated Public Static Finalstring Todoublebyte (String str) {StringBuffer outstrbuf=NewStringBuffer (""); String Tstr= ""; byte[] B =NULL;  for(inti = 0; I < str.length (); i++) {Tstr= Str.substring (I, i + 1); if(Tstr.equals ("")) {                //Half-width spaceoutstrbuf.append (TSTR); Continue; }            Try{b= Tstr.getbytes ("Unicode"); if(b[2] = = 0) {                    //Half-widthB[3] = (byte) (B[3]-32); b[2] = 1; Outstrbuf.append (NewString (b, "Unicode")); } Else{outstrbuf.append (TSTR); }                returnoutstrbuf.tostring (); } Catch(unsupportedencodingexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }        }        returnoutstrbuf.tostring (); }    /*** Half Corner turn full angle * *@paramSTR *@return     *      * @authorMjorcen * @email [email protected] * @dateTime SEP, 2:52:31 PM *@version1*/     Public Staticstring TOSBC (String str) {CharC[] =Str.tochararray ();  for(inti = 0; i < c.length; i++) {            if(C[i] = = ") {C[i]= ' \u3000 '; } Else if(C[i] < ' \177 ') {C[i]= (Char) (C[i] + 65248); }        }        return NewString (c); }    /*** Full-width turning half-width * *@paramSTR *@return     *      * @authorMjorcen * @email [email protected] * @dateTime SEP, 2:52:50 PM *@version1*/     Public Staticstring Todbc (String str) {CharC[] =Str.tochararray ();  for(inti = 0; i < c.length; i++) {            if(C[i] = = ' \u3000 ') {C[i]= ' '; } Else if(C[i] > ' \uff00 ' && c[i] < ' \uff5f ') {C[i]= (Char) (C[i]-65248); }} String returnstring=NewString (c); returnreturnstring; }     Public Static voidMain (string[] args) {LongL =System.currenttimemillis ();  for(inti = 0; I < 100000; i++) {String str= "814 Xiang Dao Asidifen 123/. 12,412 See 2 1 Fast 2 looked at 2, looked at 2 called looks + looks like home 12 consider 23; 3 Interface 2, 2 will 2. Water and electricity 2 125123123121209-021~! of bitter time @#$%^&* () _ "; String result=Todbc (str);        TOSBC (result); } System.out.println (System.currenttimemillis ()-l); }}

Java illegal character filtering, half-width/full-width substitution

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.