String temp =str;
int count=0;
for(int index=temp.indexOf("to");index=-1;){
  count++;
  temp=temp.substring(index+1);
  index=temp.indexOf("to");
}

解决方案 »

  1.   

    /*
     * 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);
        }
    }
      

  2.   

    用indexOf()方法,再用个int变量发现一次就加一
      

  3.   

    思想就是那样了,主要方法是indexOf(),用法可参考API!
      

  4.   

    都有现成的API,不用自己写算法了
    否则要写个高效的方法,还真不容易。
      

  5.   

    也差不多了,偶们网站论坛发帖子,还用indexOf()来判断敏感字那,也没感觉速度慢