Java统计大串中小串出现的次数

    public static int queryStr(String str , String regex) {
        //计数
        int count;
        //判断是否包含目标字符串
        if (!str.contains(regex)) return 0;
        //以regex分割成为数组,以测试开头能正常,结尾不正常计数
        count = str.split(regex).length-1;
        //以目标字符串结尾,多计数一次
        if (str.endsWith(regex)) count++;
        return count;
    }

 

posted @ 2021-01-15 22:00  多米_dmxq#top  阅读(209)  评论(0)    收藏  举报