String temp =str;
int count=0;
for(int index=temp.indexOf("to");index=-1;){
count++;
temp=temp.substring(index+1);
index=temp.indexOf("to");
}
int count=0;
for(int index=temp.indexOf("to");index=-1;){
count++;
temp=temp.substring(index+1);
index=temp.indexOf("to");
}
* Created on 2004-7-5
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*//**
* @author wx
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class Temp {
public static void main(String[] aaa) {
String str1 = "bc";
String str2 = "abcdabcdabcd";
int i = 0;
int j = 0;
while (str2.indexOf(str1, i) != -1) {
i = str2.indexOf(str1, i) + 1;
j++;
} System.out.println("次数=="+j);
}
}
否则要写个高效的方法,还真不容易。