wonderer's program

          everything will be better
          posts - 19, comments - 6, trackbacks - 0, articles - 0
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          Java中的全角和半角

          Posted on 2007-12-23 16:46 wonderer 閱讀(1950) 評論(3)  編輯  收藏 所屬分類: java
          OYM中的任務中,有一項對文件內容的檢查挺有意思的,就是要檢查字符是否是全角的,例如“GY”(not“GY”),并且把這些字符改為半角的。
          想起了在研發中心的一個朋友的抱怨:“昨天寫了一整天的程序,發到廣大教務處那邊居然說不能用,然后親自跑了一躺,發現不是我的程序有問題,是那邊的人輸入個全角字符,搜半角的字符,當然不行了”
          恩,Betty寫的需求真有意思,考慮的問題很周全,是一個很厲害的項目經理。如果從輸入這里解決了字符是否是半角的,那么,以后的情況就容易解決很多了。恩,網上搜了一下資料,查了一下書,得出了以下代碼:
          public void testChar() {
            String s1 
          = "123";
            String s2 
          = "abc";
            String s3 
          = "123abc";
            System.out.println(s1);
            System.out.println(s2);
            System.out.println(s3);
            
          for (int i = 0; i < s1.length(); i++) {
             
          int j = s1.charAt(i);
             
          if (j > 256) {
              
          int temp = j - 65248;
              
          if (temp >= 0) {
               System.out.print((
          char)j+"-->:" + (char) temp);
              } 
          else {
                System.out.print((
          char) j);
              }
             } 
          else {
              System.out.print((
          char) j);
             }
            }
            System.out.println();
            
            
          for (int i = 0; i < s2.length(); i++) {
             
          int j = s2.charAt(i);
             
          if (j > 256) {
              
          int temp = j - 65248;
              
          if (temp >= 0) {
               System.out.print((
          char)j+"-->:" + (char) temp);
              } 
          else {
               System.out.print((
          char) j);
              }
             } 
          else {
              System.out.print ((
          char) j);
             }
            }
            System.out.println();
            
            
          for (int i = 0; i < s3.length(); i++) {
             
          int j = s3.charAt(i);
             
          if (j > 256) {
              
          int temp = j - 65248;
              
          if (temp >= 0) {
                System.out.print((
          char)j+"-->:" + (char) temp);
              } 
          else {
               System.out.print((
          char) j);
              }
             } 
          else {
              System.out.print((
          char) j);
             }
            }
            System.out.println();
           
           }
          輸出的結果如下:
          123
          -->ab-->bc--c
          123a
          -->ab-->bc--c



          評論

          # re: Java中的全角和半角  回復  更多評論   

          2007-12-23 17:12 by 黑蝙蝠
          樓主注釋下吧

          # re: Java中的全角和半角  回復  更多評論   

          2007-12-23 17:46 by beyond
          全角空格為12288,半角空格為32
          其他字符半角(33-126)與全角(65281-65374)的對應關系是:均相差65248

          # re: Java中的全角和半角  回復  更多評論   

          2007-12-27 23:23 by wonderer
          @黑蝙蝠
          只是覺得代碼比較簡單,所以沒注釋,呵呵,不過寫代碼還是養成好習慣的比較好。
          主站蜘蛛池模板: 永福县| 河南省| 丹东市| 石棉县| 铜山县| 绥中县| 迁西县| 镇雄县| 石河子市| 石台县| 云阳县| 社会| 敖汉旗| 北海市| 正镶白旗| 临夏市| 隆尧县| 扎兰屯市| 讷河市| 霍林郭勒市| 霍山县| 尼勒克县| 泾阳县| 南召县| 朔州市| 全州县| 长岛县| 东丽区| 仁化县| 尚志市| 革吉县| 金寨县| 江门市| 台中县| 呼伦贝尔市| 宜昌市| 黄石市| 新乡县| 红原县| 崇阳县| 禹州市|