欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

判断String大串中包含多少个小串

程序员文章站 2022-06-17 14:54:52
...
String maxString = "Write2Db.jarWrite2Db.jarWrite2Db.jar;
String minString="Write2Db.jar";
int count=getCount(maxString, minString);


public static int getCount(String maxString, String minString) {
        // 定义一个统计变量,初始化值是0
        int count = 0;
        int index;
        //先查,赋值,判断
        while((index=maxString.indexOf(minString))!=-1){
            count++;
            maxString = maxString.substring(index + minString.length());
        }

        return count;
    }