字符串里寻找另一个串出现的次数和第一次出现的位置。

//字符串里寻找另一个串出现的次数和第一次出现的位置。
    @Test
    public void findSubString(){
        String s="IloveJavaloveC++lovePython";
        String temp="love";
        int index=s.indexOf(temp);
        int counts=0;
        if(index!=-1){
            System.out.println(index);
            s=s.substring(index+temp.length());
            counts++;
        }
        else return;
        while(true){
            index=s.indexOf(temp);
            if(index!=-1) {
                counts++;
                s=s.substring(index+temp.length());
            }
            else break;
        }
        System.out.println(counts);
    }

猜你喜欢

转载自blog.csdn.net/h2498864708/article/details/107433552