qileilove

          blog已經轉移至github,大家請訪問 http://qaseven.github.io/

          Java利用正則表達式統計某個字符串出現的次數

          //統計某個字符出現的次數
          private void countSubString(){
          String string1="香蕉、玉米、面粉";
          String string2="香蕉、玉米、面粉";
          String string3="牛奶、雞蛋";
          StringBuffer stringBuffer=new StringBuffer();
          stringBuffer.append(string1).append("、").append(string2).append("、").append(string3).append("、");
          String totalString=stringBuffer.toString();
          System.out.println("組拼后的字符串為:"+totalString);
          while (totalString.length()>0) {
          //得到第一個字符串比如"香蕉、"
          int index=totalString.indexOf("、");
          String foodName=totalString.substring(0,index+1);
          Pattern pattern = Pattern.compile(foodName);
          Matcher matcher = pattern.matcher(totalString);
          int count=0;
          while(matcher.find()){
          count++;
          }
          totalString= totalString.replaceAll(foodName, "");
          System.out.println("食品名字為:"+foodName+",出現次數為:"+count);
          System.out.println("統計刪除后字符串為:totalString="+totalString);
          System.out.println("===============================");
          }

          posted on 2014-01-13 10:56 順其自然EVO 閱讀(1471) 評論(0)  編輯  收藏


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


          網站導航:
           
          <2014年1月>
          2930311234
          567891011
          12131415161718
          19202122232425
          2627282930311
          2345678

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 巴林右旗| 通榆县| 焉耆| 通道| 靖江市| 西充县| 喀喇沁旗| 绥宁县| 阜平县| 湖州市| 荣昌县| 平湖市| 万源市| 个旧市| 绥江县| 九寨沟县| 荃湾区| 长阳| 道孚县| 苍山县| 嵊泗县| 缙云县| 临夏市| 鹿邑县| 黄梅县| 阿拉善盟| 新巴尔虎右旗| 马边| 蓬莱市| 龙胜| 静安区| 古蔺县| 江阴市| 射洪县| 镇沅| 鸡西市| 涟源市| 裕民县| 全南县| 新邵县| 五华县|