/**
* 查找字符串出现的次数
*
* @author ZX
*
*/
public class Test {
public static void main(String[] args) {
String str = "zpfaszpfaszzzzpfas";
String key = "zpf";
// 先查找str在strs中是否存在
// 找到第一个字符串以后 如何查找第二个那 无需从头开始 只需要第一次出现的位置加字符串的长度
int countkey = getkeycount(str, key);
System.out.println(countkey);
}
/**
* 查找字符串在另一个字符串中出现的次数
*
* @param str
* @param key
* @return
*/
public static int getkeycount(String str, String key) {
// 查找的位置
int index = 0;
// 出现的次数
int count = 0;
while ((index = str.indexOf(key, index)) != -1) {
index = index + key.length();
count++;
}
return count;
}
}