publicclassHelloWorld{publicstaticvoidmain(String[]args){System.out.println(halfWidth2FullWidth("Hello World!!!@"));System.out.println(fullWidth2halfWidth("Hello World!!!@"));System.out.println(halfWidth2FullWidth("Hello World!!!@中文測試"));System.out.println(fullWidth2halfWidth("中文測試Hello World!!!@"));}/**
* Full-angle string conversion half-corner string
* 1, half-width characters are starting from 33 to 126 end
* 2, the full-width character corresponding to the half-width character is from 65281 start to 65374 end
* 3, the half corner of the space is 32. The corresponding Full-width space is 12288
* The relationship between Half-width and Full-width is obvious, except that the character offset is 65248 (65281-33 = 65248).
*
* @param fullWidthStr Non-empty full-width string
* @return Half-angle string
*/publicstaticStringhalfWidth2FullWidth(StringfullWidthStr){if(null==fullWidthStr||fullWidthStr.length()<=0){return"";}char[]arr=fullWidthStr.toCharArray();for(inti=0;i<arr.length;++i){intcharValue=(int)arr[i];if(charValue>=33&&charValue<=126){arr[i]=(char)(charValue+65248);}elseif(charValue==32){arr[i]=(char)12288;}}returnnewString(arr);}privatestaticStringfullWidth2halfWidth(StringfullWidthStr){if(null==fullWidthStr||fullWidthStr.length()<=0){return"";}char[]charArray=fullWidthStr.toCharArray();//對全形字元轉換的char陣列遍歷for(inti=0;i<charArray.length;i++){intcharIntValue=(int)charArray[i];//如果符合轉換關係,將對應下標之間減掉偏移量65248;如果是空格的話,直接做轉換if(charIntValue>=65281&&charIntValue<=65374){charArray[i]=(char)(charIntValue-65248);}elseif(charIntValue==12288){charArray[i]=(char)32;}}returnnewString(charArray);}}