java 全角 半角 转换

   1: public class Change {
   2:  
   3:    public String DBCString;
   4:  
   5:    public String SBCString;
   6:  
   7:    public String getDBCString() {
   8:       return DBCString;
   9:    }
  10:  
  11:    public void setDBCString(String string) {
  12:       DBCString = string;
  13:    }
  14:  
  15:    public Change(String inStr) {
  16:       this.setSBCString(this.ToSBC(inStr));
  17:       this.setDBCString(this.ToDBC(inStr));
  18:    }
  19:  
  20:    public String getSBCString() {
  21:       return SBCString;
  22:    }
  23:  
  24:    public void setSBCString(String string) {
  25:       SBCString = string;
  26:    }
  27:  
  28:    public static String ToSBC(String input) {
  29:       // 半角转全角
  30:       char[] c = input.toCharArray();
  31:       for (int i = 0; i < c.length; i++) {
  32:          if (c[i] == 32) {
  33:             c[i] = (char) 12288;
  34:             continue;
  35:          }
  36:          if (c[i] < 127)
  37:             c[i] = (char) (c[i] + 65248);
  38:       }
  39:       return new String(c);
  40:    }
  41:  
  42:    public static String ToDBC(String input) {
  43:       // 全角转半角
  44:       char[] c = input.toCharArray();
  45:       for (int i = 0; i < c.length; i++) {
  46:          if (c[i] == 12288) {
  47:             c[i] = (char) 32;
  48:             continue;
  49:          }
  50:          if (c[i] > 65280 && c[i] < 65375)
  51:             c[i] = (char) (c[i] - 65248);
  52:       }
  53:       return new String(c);
  54:    }
  55:  
  56: }
posted @ 2012-03-19 16:32  MindMac  阅读(404)  评论(0编辑  收藏  举报