JAVA海洋

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            1 隨筆 :: 0 文章 :: 4 評論 :: 0 Trackbacks

          ???????? 最近碰到了一面試題:一個字符串中如何得到漢字的個數?比如"adf我jk愛sdf莫能助lkka",要求寫一個方法得出其中漢字的個數,并取得這些漢字。
          ??????? 自己想了半天沒做出來,在CSDN發了個貼子得到了答案,在此感謝CSDN的網友,huyc_fly() ,希望他不會介意我把他的方法發出來與大家,也許大家還有別的解決方法,非常期望跟大家討論.
          ??????? 實現代碼如下:

          ??public class TestChineseChar {


          ??public static void main(String[] args) {
          ??TestChineseChar t = new TestChineseChar();
          ??t.getChineseChar();
          ?}

          ?public void getChineseChar() {
          ??String str = "adf我jk愛sdf莫能助lkka";
          ??StringBuffer sb = new StringBuffer();
          ??String tempStr;
          ??int count = 0;
          ??for (int i = 0; i < str.length(); i++) {
          ???tempStr = String.valueOf(str.charAt(i));
          ???if (tempStr.getBytes().length == 2) {
          ????sb.append(tempStr);
          ????count++;
          ???}
          ??}

          ??System.out.println("the count of chinese characters in the String is :"
          ????+ count);
          ??System.out.println(sb.toString());
          ?}
          }

          posted on 2006-11-07 23:51 JIEVA 閱讀(1517) 評論(4)  編輯  收藏

          評論

          # re: 如何得到一個字符串中漢字的個數? 2006-11-07 23:53 JIEVA
          bradwoo8621() :
          str.getBytes().length - str.length();
          如果只有漢字和字符的話

          這個好像也有點意思  回復  更多評論
            

          # re: 如何得到一個字符串中漢字的個數? 2006-11-08 10:36 zeyuphoenix
          the count of chinese characters in the String is :4
          我莫能助

          奇怪 愛怎么不當漢字處理???  回復  更多評論
            

          # re: 如何得到一個字符串中漢字的個數? 2006-11-08 18:04 JIEVA
          不是吧,這是我這里的運行結果:
          the count of chinese characters in the String is : 5
          我愛莫能助  回復  更多評論
            

          # re: 如何得到一個字符串中漢字的個數? 2008-09-11 10:00 xiaodong——wang
          非常感謝~~~  回復  更多評論
            


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 都匀市| 元朗区| 金阳县| 江津市| 阿勒泰市| 哈巴河县| 北川| 五莲县| 巴林左旗| 石泉县| 林西县| 长兴县| 四川省| 香格里拉县| 潜江市| 台湾省| 彰化市| 琼结县| 宝鸡市| 乐都县| 清水县| 银川市| 库车县| 宜良县| 汤阴县| 盖州市| 塘沽区| 宣汉县| 内江市| 论坛| 长顺县| 淮北市| 大埔区| 上虞市| 通渭县| 凉山| 东光县| 青阳县| 阜阳市| 新郑市| 南华县|