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