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 閱讀(1472) 評論(0)  編輯  收藏


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


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

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 青神县| 水富县| 偃师市| 新乡县| 军事| 峨眉山市| 广南县| 农安县| 德钦县| 株洲市| 商南县| 休宁县| 淮滨县| 巴楚县| 乌兰浩特市| 泸水县| 康保县| 灵石县| 正阳县| 贞丰县| 钦州市| 元谋县| 甘孜县| 商丘市| 额尔古纳市| 什邡市| 永德县| 庆元县| 鄂托克旗| 湘阴县| 克山县| 呼图壁县| 梅州市| 金山区| 甘孜| 舟山市| 天峻县| 武乡县| 冷水江市| 临澧县| 淳化县|