在字符串:
woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun
统计java一共出现了多少?
- public class Demo {
- public static void main(String[] args) {
- String str = "woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun";
-
- int count = 0;
- int index = str.indexOf("java");
- while(index != -1){
- count++;//因为先找了一次,如果进入循环就意味着已经找到一次了,所以先加1
- //继续找
- index = str.indexOf("java",index + 1);
- }
- System.out.println("java出现了:" + count + " 次");
- }
- }
复制代码
|
|