posts - 110, comments - 101, trackbacks - 0, articles - 7
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          取得指定子串在字符串中出現的次數

          Posted on 2011-05-26 13:51 云云 閱讀(1759) 評論(0)  編輯  收藏
              /**
               * 取得指定子串在字符串中出現的次數。
               * <p/>
               * <p>
               * 如果字符串為<code>null</code>或空,則返回<code>0</code>。
               * <pre>
               * StringUtil.countMatches(null, *)       = 0
               * StringUtil.countMatches("", *)         = 0
               * StringUtil.countMatches("abba", null)  = 0
               * StringUtil.countMatches("abba", "")    = 0
               * StringUtil.countMatches("abba", "a")   = 2
               * StringUtil.countMatches("abba", "ab")  = 1
               * StringUtil.countMatches("abba", "xxx") = 0
               * </pre>
               * </p>
               * 
          @param str    要掃描的字符串
               * 
          @param subStr 子字符串
               * 
          @return 子串在字符串中出現的次數,如果字符串為<code>null</code>或空,則返回<code>0</code>
               
          */

              
          public static int countMatches(String str, String subStr) {
                  
          if ((str == null|| (str.length() == 0|| (subStr == null|| (subStr.length() == 0)) {
                      
          return 0;
                  }


                  
          int count = 0;
                  
          int index = 0;

                  
          while ((index = str.indexOf(subStr, index)) != -1{
                      count
          ++;
                      index 
          += subStr.length();
                  }


                  
          return count;
              }

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


          網站導航:
           
          主站蜘蛛池模板: 海原县| 镇坪县| 岱山县| 比如县| 安宁市| 南部县| 扬中市| 汉中市| 油尖旺区| 六安市| 荔波县| 南郑县| 西昌市| 三台县| 嘉峪关市| 普兰店市| 临汾市| 高雄市| 资中县| 随州市| 东明县| 齐齐哈尔市| 宁明县| 长丰县| 大洼县| 麻江县| 郯城县| 庆安县| 富民县| 松溪县| 吉隆县| 漯河市| 永泰县| 沐川县| 黄骅市| 涟水县| 偃师市| 衡阳县| 辰溪县| 屯昌县| 平南县|