A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 蒲公英在飞 中级黑马   /  2014-7-19 07:15  /  1027 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

求一个子串在整串出现的次数?

4 个回复

倒序浏览
public class Test2{
public static void main(String[] args){
          String a = "abcabc23423abc342334abc3453423423";
          String b = "abc";
          System.out.println(getSubNum(a, b));
         }

         public static int getSubNum(String a, String b) {
          int num = 0;
          String str = a;
          //返回子字符串b在a中第一次出现的索引,没有找到b返回-1
          int index = a.indexOf(b);
          while (index != -1) {
           num++;
           str = str.substring(index + b.length() - 1);//返回第一处索引的后面的子字符串
           index = str.indexOf(b);
          }
          return num;
         }
}
回复 使用道具 举报
使用正则表达式会好一点,
  1. package copy;

  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;

  4. public class Demo {
  5.         public static void main(String[] args) {
  6.                 String a = "abcabc23423abc342334abc3453423423";
  7.                 String b = "abc";
  8.                 System.out.println(strCount(a, b));
  9.         }

  10.         private static int strCount(String str, String sub) {

  11.                 int num = 0;
  12.        
  13.                 //Pattern表示一个已编译的正则表达式
  14.                 //Matcher是一个靠着输入的字符串来解析这个模式和完成匹配操作的对象
  15.                 Pattern pattern = Pattern.compile(sub);
  16.                 Matcher matcher = pattern.matcher(str);
  17.                
  18.                 //是否找到与之匹配的字串
  19.                 while(matcher.find()){
  20.                         num++;
  21.                 }
  22.                
  23.                 return num;

  24.         }

  25. }
复制代码

评分

参与人数 1黑马币 +5 收起 理由
人心如水 + 5 赞一个!

查看全部评分

回复 使用道具 举报
我也来凑凑热闹
  1. public class Sunday{
  2.         public static void main(String[] args){
  3.                 String str="ttabcttabcttabctt";
  4.                 String subStr="tt";
  5.                 System.out.println("子串在整串中出现的次数:"+gettimes(str,subStr));
  6.         }
  7.         public static int gettimes(String str,String subStr){
  8.                 int count=0;
  9.                 int index=0;
  10.                 while((index=str.indexOf(subStr,index))!=-1){
  11.                         index=index+subStr.length();
  12.                         count++;
  13.                 }
  14.                 return count;
  15.         }
  16. }
复制代码

这个问题在毕老师的视频第13天-08中详细讲解过。
回复 使用道具 举报
package day_7_19;
//求一个子串在整串出现的次数?
public class SubStringDemo {

        public static void main(String[] args) {
                String str = "abcddbcksaddwaddaddaddddsdasdaddavavaaaddaaaaddsvdd";
                String str2 = "dd";
                getNum(str,str2);
        }

        private static void getNum(String str, String str2) {
                int count=0;
                int len = str2.length();
                int n =0;
                int i=0;
                while(i<str.length()-len){
                         i= str.indexOf(str2, n);
                        n=i+len;
                        count++;
                       
                /*        int i=str.indexOf(str2, 0);
                        int j =str.indexOf(str2, i+len);
                        int m = str.indexOf(str2, j+len);*/
                }
                System.out.println(count);
        }
}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马