String 类中的几个练习--获取指定字符串中,大写字母、小写字母、数字的个数||获取一个字符串中,另一个字符串出现的次数

package cn.homework.demo1;

public class GetCount {
     /*
     *  获取一个字符串中,另一个字符串出现的次数
     *  思想:
     *    1. indexOf到字符串中到第一次出现的索引  2
     *    2. 找到的索引+被找字符串长度,截取字符串   lll
     *    3. 计数器++
     */

    public static void main(String[] args) {
        
         String str1 = "hellollw";
            String str2 = "l";
            int count=0;
            while (true) {
                int index = str1.indexOf(str2);//0
                if (index != -1) {
                    count++;
                    str1 = str1.substring(index+str2.length()); //每次截取不包含出现过的字符
     
                } else {
                    break;
                }
               // System.out.println(index);
            }
            
            System.out.println(count);
     
     
        }
    
}
package cn.homework.demo1;

public class GetCount {
/*
* 获取一个字符串中,另一个字符串出现的次数
* 思想:
* 1. indexOf到字符串中到第一次出现的索引 2
* 2. 找到的索引+被找字符串长度,截取字符串 lll
* 3. 计数器++
*/

public static void main(String[] args) {

String str1 = "hellollw";
String str2 = "l";
int count=0;
while (true) {
int index = str1.indexOf(str2);//0
if (index != -1) {
count++;
str1 = str1.substring(index+str2.length()); //每次截取不包含出现过的字符

} else {
break;
}
// System.out.println(index);
}

System.out.println(count);


}

}




 
原文地址:https://www.cnblogs.com/qurui1998/p/10555303.html