java实现在一个字符串中查找某个子字符串出现的次数


  public static void main(String[] args) {
        String a = "我爱我的祖国!!!";
        String b = "爱";

        System.out.println(strCount(a, b));

    }

    /**
     *
     * @param str 源字符串
     * @param findByStr 被查询的字符串
     * @return 返回findByStr在str中出现的次数
     */
    public static int strCount(String str,String findByStr){
        String[] split = str.split("");
        return Arrays.asList(split).stream().filter(s -> s.equals(findByStr)).collect(Collectors.toList()).size();
    }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM