计算字符串中的指定的子串出现的次数

public class Test_String2 {
	public static void main(String[] args) {
		String s="abddabsscabdablab";
		int count=0;
		int index=-1;   //用于标识字符的下标位置
		
		String sToFind="ab";  //需要寻找的字符串
		while((index=s.indexOf(sToFind))!=-1) {
			count++;
			s=s.substring(index+sToFind.length());
		}
		System.out.println("ab出现的次数为:"+count);
	}
}

猜你喜欢

转载自blog.csdn.net/maidu_xbd/article/details/85847480