- package text.no_9_13;
- public class Test6 {
- public static void main(String[] args){
- String s = "javascriptjavasejavaeejavame";
- int count = getSubCount_1(s,"java");
- System.out.println(count);
- }
-
- // 第一种方式:
- public static int getSubCount_1(String string,String key){
- int count = 0;
- int index = 0;
- while((index=string.indexOf(key,index))!=-1){
- index = index+key.length();
- count++;
- }
- return count;
- }
-
- //第二种方式:
-
- public static int getSubCount_2(String string,String key){
- int count = 0;
- int index = 0;
- while ((index=string.indexOf(key,index))!=-1){
- string = string.substring(index+key.length());
- count++;
- }
- return count;
- }
- }
复制代码
|
|