Java如何獲取字符串單詞個數(shù)
Java獲取字符串單詞個數(shù)
public static int getWordCount(String content){ int count = 0; String cn_words = content.replaceAll("[^(\\u4e00-\\u9fa5,?!丁??;'‘:“”【】、)(……¥!·)]", ""); int cn_words_count = cn_words.length(); String non_cn_words = content.replaceAll("[^(a-zA-Z0-9`\\-=\';.,/~!@#$%^&*()_+|}{\":><?\\[\\])]", " "); int non_cn_words_count = 0; String[] temp = non_cn_words.split(" "); for(String ch:temp){ if(ch.trim().length() != 0) non_cn_words_count++; } count = cn_words_count + non_cn_words_count; return count; } public static void main(String[] args) { System.out.println(getWordCount("我愛你 zhanglulu _")); // 輸出5,單詞是以空格分開,所以這里我愛你三個字加一個單詞zhanglulu和一個下劃線,空格不算。 }
統(tǒng)計String單詞數(shù)的三種方法
統(tǒng)計字符串里包含有多少個單詞,這是Java代碼常用的場景。介紹三種簡單的方法來對其進行統(tǒng)計。這里所謂的單詞,是指連續(xù)的非空字符串。如“Hello”則為一個詞,“I love Guangzhou”則為三個詞。
方法一:使用split
在類String中,有split()這個方法,可以將字符進行分割??梢酝ㄟ^對字符串以空白字符進行分割,則可以得到結(jié)果。
public int countWithSplit(String str) { ? ? if (Strings.isNullOrEmpty(str)) { ? ? ? ? return 0; ? ? } ? ? return str.split("\\s+").length; }
代碼中"\\s+"為正則表達式,表示所有的空白字符。
方法二:使用StringTokenizer
public int countWithStringTokenizer(String str) { ? ? if (Strings.isNullOrEmpty(str)) { ? ? ? ? return 0; ? ? } ? ? StringTokenizer tokenizer = new StringTokenizer(str); ? ? return tokenizer.countTokens(); }
StringTokenizer是一個很有用的類,構(gòu)造函數(shù)有三個:
- 1. StringTokenizer(String str) :構(gòu)造一個用來解析 str 的 StringTokenizer 對象。java 默認的分隔符是空格("")、制表符(\t)、換行符(\n)、回車符(\r)。
- 2. StringTokenizer(String str, String delim) :構(gòu)造一個用來解析 str 的 StringTokenizer 對象,并提供一個指定的分隔符。
- 3. StringTokenizer(String str, String delim, boolean returnDelims) :構(gòu)造一個用來解析 str 的 StringTokenizer 對象,并提供一個指定的分隔符,同時,指定是否返回分隔符。
方法三:使用原始的char判斷
public int countWithChar(String str) { ? ? if (Strings.isNullOrEmpty(str)) { ? ? ? ? return 0; ? ? } ? ? int wordCount = 0; ? ? boolean isWord = false; ? ? int endOfLine = str.length() - 1; ? ? char[] chars = str.toCharArray(); ? ? ? for (int i = 0; i < chars.length; i++) { ? ? ? ? // 如果是非空字符, word = true. ? ? ? ? if (isWord(chars[i]) && i != endOfLine) { ? ? ? ? ? ? isWord = true; ? ? ? ? ? ? ? // 非空字符后遇到空字符,則數(shù)量加1 ? ? ? ? } else if (!isWord(chars[i]) && isWord) { ? ? ? ? ? ? wordCount++; ? ? ? ? ? ? isWord = false; ? ? ? ? ? ? // 非空字符后遇到行尾 ? ? ? ? } else if (isWord(chars[i]) && i == endOfLine) { ? ? ? ? ? ? wordCount++; ? ? ? ? } ? ? } ? ? return wordCount; } ? private boolean isWord(char c) { ? ? return c != ' ' ? ? ? ? ? ? && c != '\t' ? ? ? ? ? ? && c != '\n' ? ? ? ? ? ? && c != '\r' ? ? ? ? ? ? && c != '\f'; }
測試代碼
簡單寫了幾個測試用例,測試通過。
public class CountWordTest { ? ? private CountWord countWord = new CountWord(); ? ? ? @Test ? ? public void test() { ? ? ? ? testStringCount(null, 0); ? ? ? ? testStringCount("", 0); ? ? ? ? testStringCount(" ", 0); ? ? ? ? testStringCount(" \t\r\n\f", 0); ? ? ? ? testStringCount("0", 1); ? ? ? ? testStringCount("abcdef", 1); ? ? ? ? testStringCount("a b c", 3); ? ? ? ? testStringCount("a,b,c", 1); ? ? ? ? testStringCount("a\rb\nc", 3); ? ? ? ? testStringCount("a,b\t\nc", 2); ? ? } ? ? ? private void testStringCount(String str, int expectedCount) { ? ? ? ? assertEquals(expectedCount, countWord.countWithSplit(str)); ? ? ? ? assertEquals(expectedCount, countWord.countWithStringTokenizer(str)); ? ? ? ? assertEquals(expectedCount, countWord.countWithChar(str)); ? ? } }
這三種方法都非常簡單,沒有什么技術(shù)難點,用到了String、StringTokenizer、正則、Guava、JUnit等,非?;A(chǔ)。
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringCloud Alibaba項目實戰(zhàn)之nacos-server服務(wù)搭建過程
Nacos 是阿里巴巴推出來的一個新開源項目,這是一個更易于構(gòu)建云原生應(yīng)用的動態(tài)服務(wù)發(fā)現(xiàn)、配置管理和服務(wù)管理平臺。本章節(jié)重點給大家介紹SpringCloud Alibaba項目實戰(zhàn)之nacos-server服務(wù)搭建過程,感興趣的朋友一起看看吧2021-06-06Maven腳手架如何基于jeecg實現(xiàn)快速開發(fā)
這篇文章主要介紹了Maven腳手架如何基于jeecg實現(xiàn)快速開發(fā),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-10-10Java并發(fā)編程之阻塞隊列(BlockingQueue)詳解
這篇文章主要介紹了詳解Java阻塞隊列(BlockingQueue)的實現(xiàn)原理,阻塞隊列是Java util.concurrent包下重要的數(shù)據(jù)結(jié)構(gòu),有興趣的可以了解一下2021-09-09Java Swing實現(xiàn)JTable檢測單元格數(shù)據(jù)變更事件的方法示例
這篇文章主要介紹了Java Swing實現(xiàn)JTable檢測單元格數(shù)據(jù)變更事件的方法,結(jié)合完整實例形式分析了Swing實現(xiàn)JTable檢測單元格數(shù)據(jù)變更事件過程中出現(xiàn)的問題與相關(guān)解決方法,需要的朋友可以參考下2017-11-11Mybatis-plus配置多數(shù)據(jù)源,連接多數(shù)據(jù)庫方式
這篇文章主要介紹了Mybatis-plus配置多數(shù)據(jù)源,連接多數(shù)據(jù)庫方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06