首页 > 代码库 > 关于全角半角转换(转)

关于全角半角转换(转)

见谅未能注明出处:

  1     第一种方法:  2     /**  3      * 特殊字符串转换  4      * @Class Name FullCharConverter  5      * @Author v-jiangwei  6      * @Create In 2012-8-24  7      */  8     public class FullCharConverter {  9  10     /** 11     * 全角转半角的 转换函数 12     * @Methods Name full2HalfChange 13     * @Create In 2012-8-24 By v-jiangwei 14     * @param QJstr 15     * @return String 16     */ 17     public static final String full2HalfChange(String QJstr) 18     { 19     StringBuffer outStrBuf = new StringBuffer(""); 20     String Tstr = ""; 21     byte[] b = null; 22     for (int i = 0; i < QJstr.length(); i++) { 23     Tstr = QJstr.substring(i, i + 1); 24     // 全角空格转换成半角空格 25     if (Tstr.equals(" ")) { 26     outStrBuf.append(" "); 27     continue; 28     } 29     try { 30     b = Tstr.getBytes("unicode"); 31     // 得到 unicode 字节数据 32     if (b[2] == -1) { 33     // 表示全角 34     b[3] = (byte) (b[3] + 32); 35     b[2] = 0; 36     outStrBuf.append(new String(b, "unicode")); 37     } else { 38     outStrBuf.append(Tstr); 39     } 40     } catch (UnsupportedEncodingException e) { 41     // TODO Auto-generated catch block 42     e.printStackTrace(); 43     } 44  45     } // end for. 46     return outStrBuf.toString(); 47  48  49     } 50  51       52  53     /** 54     * 半角转全角 55     * @Methods Name half2Fullchange 56     * @Create In 2012-8-24 By v-jiangwei 57     * @param QJstr 58     * @return String 59     */ 60     public static final String half2Fullchange(String QJstr) 61     { 62     StringBuffer outStrBuf = new StringBuffer(""); 63     String Tstr = ""; 64     byte[] b = null; 65     for (int i = 0; i < QJstr.length(); i++) { 66     Tstr = QJstr.substring(i, i + 1); 67     if (Tstr.equals(" ")) { 68     // 半角空格 69     outStrBuf.append(Tstr); 70     continue; 71     } 72     try { 73     b = Tstr.getBytes("unicode"); 74     if (b[2] == 0) { 75     // 半角 76     b[3] = (byte) (b[3] - 32); 77     b[2] = -1; 78     outStrBuf.append(new String(b, "unicode")); 79     } else { 80     outStrBuf.append(Tstr); 81     } 82     return outStrBuf.toString(); 83     } catch (UnsupportedEncodingException e) { 84     // TODO Auto-generated catch block 85     e.printStackTrace(); 86     } 87  88     } 89     return outStrBuf.toString(); 90     } 91  92  93     /** 94     * @Methods Name main 95     * @Create In 2012-8-24 By v-jiangwei 96     * @param args 97     *            void 98     */ 99     public static void main(String[] args) {100     // TODO Auto-generated method stub101     String QJstr = "814乡道";102     String result = FullCharConverter.full2HalfChange(QJstr);103     System.out.println(QJstr);104     System.out.println(result);105     System.out.println("------------------------------------");106     // 半角转全角107     String str = "G45大广高速";108     System.out.println(str);109     System.out.println(FullCharConverter.half2Fullchange(str));110     }111 112 113     }114     第二种方法:115     /**116          * 半角转全角117          * @param input String.118          * @return 全角字符串.119          */120     public static String ToSBC(String input) {121              char c[] = input.toCharArray();122              for (int i = 0; i < c.length; i++) {123                if (c[i] == ‘ ‘) {124                  c[i] = ‘\u3000‘;125                } else if (c[i] < ‘\177‘) {126                  c[i] = (char) (c[i] + 65248);127 128 129                }130              }131              return new String(c);132     }133      134          /**135          * 全角转半角136          * @param input String.137          * @return 半角字符串138          */139      public static String ToDBC(String input) {140          char c[] = input.toCharArray();141               for (int i = 0; i < c.length; i++) {142                 if (c[i] == ‘\u3000‘) {143                   c[i] = ‘ ‘;144                 } else if (c[i] > ‘\uFF00‘ && c[i] < ‘\uFF5F‘) {145                   c[i] = (char) (c[i] - 65248);146 147 148                 }149               }150               String returnString = new String(c);151               return returnString;152      }153         public static void main(String[] args) {154         String QJstr = "G45大广高速";155     String result = StringUtil.ToDBC(QJstr);156     System.out.println(result);157 158     }

这里面对第一种说明下,对于一个字符,当转换为unicode编码时候,所对应的byte数组,第三个值对应0或者1,0代表半角,1代表全角

关于全角半角转换(转)