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

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

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

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


          網站導航:
           
          主站蜘蛛池模板: 亚东县| 卫辉市| 阳江市| 三穗县| 临湘市| 乌兰浩特市| 邮箱| 永春县| 武邑县| 靖江市| 昭觉县| 同德县| 襄汾县| 南宫市| 洪江市| 瑞安市| 浑源县| 三江| 通渭县| 东乡| 赫章县| 商城县| 化德县| 花莲县| 平武县| 东台市| 成都市| 沛县| 夹江县| 库伦旗| 龙门县| 阿勒泰市| 绥江县| 永清县| 汨罗市| 团风县| 临潭县| 昌平区| 嘉定区| 成都市| 岳阳县|