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

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

          Posted on 2011-05-26 13:51 云云 閱讀(1756) 評論(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;
              }

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


          網站導航:
           
          主站蜘蛛池模板: 荔波县| 郴州市| 闽清县| 华池县| 秦安县| 广元市| 海丰县| 峨边| 嵩明县| 昌邑市| 额济纳旗| 南澳县| 大同县| 克东县| 宁安市| 新郑市| 枞阳县| 阿尔山市| 赤峰市| 富阳市| 南靖县| 扎兰屯市| 芮城县| 油尖旺区| 禹城市| 崇信县| 红桥区| 平利县| 玉山县| 正宁县| 屏山县| 长丰县| 报价| 景泰县| 石屏县| 珲春市| 榕江县| 彭阳县| 香港 | 延庆县| 万州区|