统计某字符串中某子串出现的次数

class StringDemo1
{
public static void main(String[] args) {
int n = getString("abcdnfgabchedabckkfdnabct","abc");
System.out.println("字符串出现的次数为:"+n);
}
public static int getString(String str,String str1){
   int count=0;
int index=0;
while(true){
index = str.indexOf(str1,index);
if(index==-1){
break;
}
index = index + str1.length();
count++;
}
return count;
}
}

posted on 2012-04-22 15:13  java课程设计  阅读(328)  评论(0编辑  收藏  举报

导航