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)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 益阳市| 恩施市| 富裕县| 仁怀市| 曲阜市| 芜湖市| 嘉荫县| 舞阳县| 响水县| 宣恩县| 紫阳县| 新晃| 温州市| 怀宁县| 小金县| 绥宁县| 闽清县| 武邑县| 乌兰察布市| 云梦县| 万源市| 辰溪县| 磐石市| 印江| 嘉禾县| 铁力市| 弥勒县| 德化县| 大理市| 通化县| 修文县| 专栏| 娄底市| 巍山| 舒城县| 武穴市| 怀化市| 万宁市| 亚东县| 天柱县| 都匀市|