我的Java路上那些事兒

          快樂(lè)成長(zhǎng)
          posts - 110, comments - 101, trackbacks - 0, articles - 7
            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理
              /**
               * 取得指定子串在字符串中出現(xiàn)的次數(shù)。
               * <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 子串在字符串中出現(xiàn)的次數(shù),如果字符串為<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;
              }

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 宝清县| 遵义市| 石台县| 化隆| 社会| 扬中市| 宣恩县| 瑞丽市| 民勤县| 宣武区| 山西省| 德兴市| 桐城市| 五原县| 西丰县| 神池县| 伊春市| 云龙县| 武陟县| 高淳县| 无极县| 崇信县| 皮山县| 西昌市| 绥化市| 观塘区| 从化市| 巴林右旗| 榆中县| 吉安市| 沁水县| 望城县| 墨竹工卡县| 仲巴县| 桐乡市| 芦溪县| 泸水县| 彰化市| 富川| 呈贡县| 江华|