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();
    }

 

Tags: