Java-J2SE学习笔记-查找一个String中,subString的出现次数
1.查找一个String中,subString的出现次数
2.代码
package Test; public class TestStringContain { public static void main(String[] args) { String str = "javakkjava--java99javahajava98098098098java"; String subStr = "java"; int count = 0; int fromIndex = 0; int index = -1; while((index = str.indexOf(subStr,fromIndex)) != -1){ //每次找到subStr,都把fromIndex下移subStr.length()个长度 fromIndex = index + subStr.length(); count++; } System.out.println(count); } }
3.运行结果
6
You can do anything you set your mind to, man!