用java实现在一个字符串中查找某个子字符串出现的次数
public class TestSubString { public static void main(String[] args) { String str = "ilovejavajavacdejavailha123java34javali3java@#@R#FDSFAFDAjavai23o2fjai23javii2o3java"; String sToFind = "java"; int num = new TestSubString().countStr(str,sToFind); System.out.println("共找到" + num + "个" + sToFind); } /** * @param str 原字符串 * @param sToFind 需要查找的字符串 * @return 返回在原字符串中sToFind出现的次数 */ private int countStr(String str,String sToFind) { int num = 0; while (str.contains(sToFind)) { str = str.substring(str.indexOf(sToFind) + sToFind.length()); num ++; } return num; } }