JAVA判斷字符串中某個字符存在的個數


 

 

/**
     * 判斷字符串中某個字符存在的個數
     * @param str1  完整字符串
     * @param str2  要統計匹配個數的字符
     * @return
     */
    public static int countStr(String str1, String str2) {
        int count=0;
        if (str1.indexOf(str2) == -1) {
            return 0;
        }
        while(str1.indexOf(str2)!=-1){
            count++;
            str1=str1.substring(str1.indexOf(str2)+str2.length());
        }
        return count;
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM