1. 程式人生 > >java判斷一個字串包含多少其他字串

java判斷一個字串包含多少其他字串

public static int getSubCount_2(String str, String key) {
        int count = 0;
        int index = 0;
        while ((index = str.indexOf(key, index)) != -1) {
            index = index + key.length();

            count++;
        }
        return count;
    }

    public static void main(String[] args) {
        String str
= "kkabkkcdkkefkks"; //控制檯輸出4 System.out.println(getSubCount_2(str, "kk")); }