java 不用循環統計一個字符串中某個字符出現的次數


參考原文地址:https://www.cnblogs.com/zwwhnly/p/12449818.html

public static void main(String[] args) {
    //被統計的字符串
    String str = "ABC123ABC";

    //查找的字符
    String searchChar = "B";

    int count = 0;

    //原來的字符串的長度
    int origialLength = str.length();

    //將字符串中的指定字符去掉
    str = str.replace(searchChar, "");

    //去掉指定字符的字符串長度
    int newLength = str.length();

    //指定字符出現的次數
    count = origialLength - newLength;

    System.out.println("字符" + searchChar + "出現的次數為:" + count);
}

  


免責聲明!

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



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