国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

java統計字符串單詞個數的方法解析

2019-11-26 13:20:02
字體:
來源:轉載
供稿:網友

在一些項目中可能需要對一段字符串中的單詞進行統計,我在這里寫了一個簡單的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);   }

運行結果:

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

以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持武林網!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 大田县| 邓州市| 田东县| 苍溪县| 商南县| 南丰县| 德清县| 福海县| 时尚| 连云港市| 巴马| 额尔古纳市| 都江堰市| 富蕴县| 开江县| 尼玛县| 凤翔县| 镇康县| 庆阳市| 普宁市| 阿拉尔市| 湖口县| 崇义县| 璧山县| 崇文区| 临武县| 巴马| 佛教| 花莲市| 虎林市| 万载县| 扬中市| 武城县| 湘阴县| 柳州市| 莒南县| 大埔县| 龙口市| 阿勒泰市| 巨野县| 扶沟县|