java統(tǒng)計(jì)字符串單詞個(gè)數(shù)的方法解析
在一些項(xiàng)目中可能需要對(duì)一段字符串中的單詞進(jìn)行統(tǒng)計(jì),我在這里寫了一個(gè)簡單的demo,有需要的同學(xué)可以拿去看一下。
不說廢話了直接貼代碼:
實(shí)現(xiàn)代碼:
/** * 統(tǒng)計(jì)各個(gè)單詞出現(xiàn)的次數(shù) * @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]; //轉(zhuǎn)為小寫 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); } } } //輸出到控制臺(tái) System.out.println("各個(gè)單詞出現(xiàn)的頻率為:"); 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); }
運(yùn)行結(jié)果:
后面還有一些沒有全部截下來
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持腳本之家!
相關(guān)文章
java8 List<Object>去掉重復(fù)對(duì)象的幾種方法
本文主要介紹了java8 List<Object>去掉重復(fù)對(duì)象的幾種方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04Spring中的監(jiān)聽器SpringApplicationRunListener詳解
這篇文章主要介紹了Spring中的監(jiān)聽器SpringApplicationRunListener詳解,命名我們就可以知道它是一個(gè)監(jiān)聽者,分析springboot啟動(dòng)流程我們會(huì)發(fā)現(xiàn),它其實(shí)是用來在整個(gè)啟動(dòng)流程中接收不同執(zhí)行點(diǎn)事件通知的監(jiān)聽者,需要的朋友可以參考下2023-11-11java微信小程序步數(shù)encryptedData和開放數(shù)據(jù)解密的實(shí)現(xiàn)
這篇文章主要介紹了java微信小程序步數(shù)encryptedData和開放數(shù)據(jù)解密的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09SpringMVC + servlet3.0 文件上傳的配置和實(shí)現(xiàn)代碼
本篇文章主要介紹了SpringMVC + servlet3.0 文件上傳的配置和實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-04-04java 動(dòng)態(tài)代理的方法總結(jié)
這篇文章主要介紹了java 動(dòng)態(tài)代理的方法總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-04-04java 中設(shè)計(jì)模式(裝飾設(shè)計(jì)模式)的實(shí)例詳解
這篇文章主要介紹了java 中設(shè)計(jì)模式(裝飾設(shè)計(jì)模式)的實(shí)例詳解的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-09-09