Java实现获取指定字符串中某个子字符串出现的个数

public class findStrNum {

    public static void main(String[] args) {

        String str = "a abc fo abc gsdfsdsbcsgsod abcsgs abc";

        int fromIndex = 0;
        int count = 0;
        while(true){
            int index = str.indexOf("abc", fromIndex);
            if(-1 != index){
                fromIndex = index + 1;
                count++;
            }else{
                break;
            }
        }
        System.out.println(count);
    }
}
阅读更多

没有更多推荐了,返回首页