Java - Đếm số lần xuất hiện của một ký tự trong văn bảng(How do I count the number of occurrences of a char in a String?).






Bài viết này sẽ hướng dẫn một số phương pháp và thủ thuật để đếm số lần xuất hiện(occurences) của một ký tự trong một đoạn văn bảng.







Như thường lệ ta sẽ có một đề bài để thể hiện các phương pháp này, đầu tiên ta sẽ có một đoạn văn bảng(text) sau.
String text = "A.B.C.D.E.F.G.H.I.J.K.L.M.N.O.P.Q.R.S.T.U.V.W.X.Y.Z"

Đề bài yêu cầu đếm tổng số lần xuất hiện của dấu chấm(.) trong đoạn văn bảng trên. Dưới đây tôi xin giới thiệu một số phương pháp để giải quyết vấn đề trên.


Dùng vòng lập For:
     public static int countOccurrences(String text, char charcount)
    {
        int count = 0;
        for (int i=0; i < text.length(); i++)
        {
            if (text.charAt(i) == charcount)
            {
                 count++;
            }
        }
        return count;
    } 


Sửa dụng StringUtil của Apache:
Lưu ý với cách này bạn phải download bộ thư viện Apache Common Lang về sử dung.
        //--- Use StringUtils Apache Commons Lang ------------
        int count_01 = StringUtils.countMatches(text, ".");
        System.out.println("Use StringUtils: " + count_01);


Sửa dụng Replace:
        //--- Use Replace for String Class ------------
        int count_02 = text.length() - text.replace(".", "").length();
        System.out.println("Use Replace: " + count_02);


Sửa dụng ReplaceAll và Pattern:
        //--- Use Replace Pattern --------------------
        int count_03 = text.replaceAll("[^.]", "").length();
        System.out.println("Use Replace Pattern: " + count_03);


Sửa dụng Split:
        //--- Use Split ------------------------------
        int count_04 = text.split("\\.",-1).length-1;
        System.out.println("Use Split: " + count_04);


Dưới đây là toàn bộ mã nguồn của bài viết.
/**
 * @(#)Demo_Count_Occurrences.java
 *
 * Demo_Count_Occurrences application
 *
 * @author VNLIVES.NET
 * @version 1.00 2013/9/26
 */
import org.apache.commons.lang3.StringUtils;

public class Demo_Count_Occurrences {
   
    public static void main(String[] args) {
       
        // TODO, add your application code
        String text = "A.B.C.D.E.F.G.H.I.J.K.L.M.N.O.P.Q.R.S.T.U.V.W.X.Y.Z";
       
        //--- Use For Loop to count occurrences char in string ------------
        char charcount = '.';
        System.out.println("Use For Loop: " + countOccurrences(text, charcount));
       
        //--- Use StringUtils Apache Commons Lang ------------
        int count_01 = StringUtils.countMatches(text, ".");
        System.out.println("Use StringUtils: " + count_01);
       
        //--- Use Replace for String Class ------------
        int count_02 = text.length() - text.replace(".", "").length();
        System.out.println("Use Replace: " + count_02);
       
        //--- Use Replace Pattern --------------------
        int count_03 = text.replaceAll("[^.]", "").length();
        System.out.println("Use Replace Pattern: " + count_03);
       
        //--- Use Split ------------------------------
        int count_04 = text.split("\\.",-1).length-1;
        System.out.println("Use Split: " + count_04);
       
    }
   
    public static int countOccurrences(String text, char charcount)
    {
        int count = 0;
        for (int i=0; i < text.length(); i++)
        {
            if (text.charAt(i) == charcount)
            {
                 count++;
            }
        }
        return count;
    }   
   
   
   
}



Rất mong sự đóng góp ý kiển của các bạn.













No comments:

Post a Comment