zoukankan      html  css  js  c++  java
  • JavaSE-统计字符在字符串中出现的次数

    /**
     * 方法一:使用indexOf和subString方法,循环判断并截取
     */
    // zhangsan
    // 查找n出现的次数
    public static void getStrCount1(String str, String con) {
        int count = 0;
        while (str.indexOf(con) >= 0) {
            // 3 + 1
            str = str.substring(str.indexOf(con) + con.length());
            // 第一次截取后:gsan
            count++;
        }
        System.out.println("指定字符串出现的次数:" + count);
    }
    
    /**
     * 方法二:使用replace方法将字符串替换为空,然后求长度
     */
    public static void getStrCount2(String str, String con) {
        int count = (str.length() - str.replace(con, "").length()) / con.length();
        System.out.println(count);
    }
    

      

  • 相关阅读:
    python标准库
    python常用标准库
    django-restframework-serializers
    Resources.UnloadUnusedAssets
    Shader 的 Blend
    C++STL queue
    C++STL stack
    C++STL deque
    C++STL容器重点
    C++STL 迭代器
  • 原文地址:https://www.cnblogs.com/zhangzhixi/p/14402480.html
Copyright © 2011-2022 走看看