class ttest1{
private static final String s = "itheimaoaitheimacastitheimama";
private static final String subStr = "itheima";
private void find() {
int i = 0;
int p;
String temp = s;
while((p=temp.indexOf(subStr))!=-1)
{
temp = temp.substring(p+subStr.length());
++i;
}
System.out.println("字符串"+s+"中字符串"+subStr+"出现了"+i+"次");
}
private void find1() {
int i = 0;
String temp = s.replaceAll(subStr, "!");
for(char c:temp.toCharArray())
{
if(c=='!')
++i;
}
System.out.println("字符串"+s+"中字符串"+subStr+"出现了"+i+"次");
}
public ttest1(){
find();
find1();
}
} |