您的位置:首页 > 其它

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

2012-04-22 15:13 169 查看
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;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐