程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> java統計字符串單詞的個數,java字符串個數

java統計字符串單詞的個數,java字符串個數

編輯:JAVA綜合教程

java統計字符串單詞的個數,java字符串個數


在一些項目中可能需要對一段字符串中的單詞進行統計,我在這裡寫了一個簡單的demo,有需要的同學可以拿去看一下。

本人沒怎麼寫個播客,如果有啥說的不對的地方,你來打我啊大笑大笑

不說廢話了直接貼代碼:

實現代碼:

        /** 
         * 統計各個單詞出現的次數 
         * @param text 
         */  
        public static void findEnglishNum(String text){  
            //找出所有的單詞  
            String[] array = {".", " ", "?", "!"};  
            for (int i = 0; i < array.length; i++) {  
                text = text.replace(array[i],",");  
            }  
            String[] textArray = text.split(",");  
              
            //遍歷 記錄  
            Map<String, Integer> map = new HashMap<String, Integer>();  
            for (int i = 0; i < textArray.length; i++) {  
                String key = textArray[i];  
                //轉為小寫  
                String key_l = key.toLowerCase();  
                if(!"".equals(key_l)){  
                    Integer num = map.get(key_l);  
                    if(num == null || num == 0){  
                        map.put(key_l, 1);  
                    }else if(num > 0){  
                        map.put(key_l, num+1);  
                    }  
                }  
            }  
            //輸出到控制台  
            System.out.println("各個單詞出現的頻率為:");  
            Iterator<String> iter = map.keySet().iterator();  
            while(iter.hasNext()){  
                String key = iter.next();  
                Integer num = map.get(key);  
                System.out.println(key + "\n\t\t" + num + "次\n-------------------");  
            }  
        }  

測試代碼:

    public static void main(String[] args) {  
            String text = "Welcome welcome to ADempiere, a commons-based peer-production of Open Source ERP Applications. This Wiki is for the global community to contribute and share know-how and domain expertise. We hope you can find as much open information and participate in making it most usable for everyone. This project has a bazaar of Citizens with a Community Council Team which work in theFunctional Team and Technical Team along the Software Development Procedure supported and funded by the foundation ADempiere";  
              
            findEnglishNum(text);          }  

運行結果:

後面還有一些沒有全部截下來


本人代碼功力不夠深厚,如果代碼中有什麼不對或不好的地方,歡迎各位大神指點!可憐

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved