close

本篇是利用indexOf()回傳字串位置的函式,但是只會從指定位置後查找一次,

加以改良升級成「統計字串中所有出現 某字串 的次數」

package p005;

public class c00102 {
    public static int FindAndCount(String FromWhat,String FindWhat)
    {
        int k=0,cnt=0;
        do
        {
            k = FromWhat.indexOf(FindWhat,k); /*FromWhat.indexOf(FindWhat,k) 是回傳"FromWhat字串"中遇到FindWhat字串的第一個位置,k表示從"FromWhat字串"的第k個位子開始找*/
            if(k>=0){cnt++;k=k+FindWhat.length();}
        }while(k>=0);
        
        return cnt;
    }
    
    
    public static void main(String[] a)
    {
        System.out.println(FindAndCount("sdgfsgsg","sg"));
    }
}

arrow
arrow
    文章標籤
    indexOf Java 自製函式 fun
    全站熱搜
    創作者介紹
    創作者 Kuihao 的頭像
    Kuihao

    溫暖午後的金針田__孕育有趣的創新

    Kuihao 發表在 痞客邦 留言(0) 人氣()