Java怎么获取字符串单词个数

其他教程   发布日期:2023年07月21日   浏览次数:547

今天小编给大家分享一下Java怎么获取字符串单词个数的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。

    Java获取字符串单词个数

    1. public static int getWordCount(String content){
    2. int count = 0;
    3. String cn_words = content.replaceAll("[^(u4e00-u9fa5,。《》?;'‘:“”【】、)(……¥!・)]", "");
    4. int cn_words_count = cn_words.length();
    5. String non_cn_words = content.replaceAll("[^(a-zA-Z0-9`-=';.,/~!@#$%^&*()_+|}{":><?[])]", " ");
    6. int non_cn_words_count = 0;
    7. String[] temp = non_cn_words.split(" ");
    8. for(String ch:temp){
    9. if(ch.trim().length() != 0) non_cn_words_count++;
    10. }
    11. count = cn_words_count + non_cn_words_count;
    12. return count;
    13. }
    14. public static void main(String[] args) {
    15. System.out.println(getWordCount("我爱你 zhanglulu _")); // 输出5,单词是以空格分开,所以这里我爱你三个字加一个单词zhanglulu和一个下划线,空格不算。
    16. }

    统计String单词数的三种方法

    统计字符串里包含有多少个单词,这是Java代码常用的场景。介绍三种简单的方法来对其进行统计。这里所谓的单词,是指连续的非空字符串。如“Hello”则为一个词,“I love Guangzhou”则为三个词。

    方法一:使用split

    在类String中,有split()这个方法,可以将字符进行分割。可以通过对字符串以空白字符进行分割,则可以得到结果。

    1. public int countWithSplit(String str) {
    2. if (Strings.isNullOrEmpty(str)) {
    3. return 0;
    4. }
    5. return str.split("s+").length;
    6. }

    代码中"s+"为正则表达式,表示所有的空白字符。

    方法二:使用StringTokenizer

    1. public int countWithStringTokenizer(String str) {
    2. if (Strings.isNullOrEmpty(str)) {
    3. return 0;
    4. }
    5. StringTokenizer tokenizer = new StringTokenizer(str);
    6. return tokenizer.countTokens();
    7. }

    StringTokenizer是一个很有用的类,构造函数有三个:

    • 1. StringTokenizer(String str) :构造一个用来解析 str 的 StringTokenizer 对象。java 默认的分隔符是空格("")、制表符( )、换行符( )、回车符( )。

    • 2. StringTokenizer(String str, String delim) :构造一个用来解析 str 的 StringTokenizer 对象,并提供一个指定的分隔符。

    • 3. StringTokenizer(String str, String delim, boolean returnDelims) :构造一个用来解析 str 的 StringTokenizer 对象,并提供一个指定的分隔符,同时,指定是否返回分隔符。

    方法三:使用原始的char判断

    1. public int countWithChar(String str) {
    2. if (Strings.isNullOrEmpty(str)) {
    3. return 0;
    4. }
    5. int wordCount = 0;
    6. boolean isWord = false;
    7. int endOfLine = str.length() - 1;
    8. char[] chars = str.toCharArray();
    9. for (int i = 0; i < chars.length; i++) {
    10. // 如果是非空字符, word = true.
    11. if (isWord(chars[i]) && i != endOfLine) {
    12. isWord = true;
    13. // 非空字符后遇到空字符,则数量加1
    14. } else if (!isWord(chars[i]) && isWord) {
    15. wordCount++;
    16. isWord = false;
    17. // 非空字符后遇到行尾
    18. } else if (isWord(chars[i]) && i == endOfLine) {
    19. wordCount++;
    20. }
    21. }
    22. return wordCount;
    23. }
    24. private boolean isWord(char c) {
    25. return c != ' '
    26. && c != ' '
    27. && c != '
    28. '
    29. && c != '
    30. '
    31. && c != 'f';
    32. }

    测试代码

    简单写了几个测试用例,测试通过。

    1. public class CountWordTest {
    2. private CountWord countWord = new CountWord();
    3. @Test
    4. public void test() {
    5. testStringCount(null, 0);
    6. testStringCount("", 0);
    7. testStringCount(" ", 0);
    8. testStringCount("
    9. f", 0);
    10. testStringCount("0", 1);
    11. testStringCount("abcdef", 1);
    12. testStringCount("a b c", 3);
    13. testStringCount("a,b,c", 1);
    14. testStringCount("a
    15. b
    16. c", 3);
    17. testStringCount("a,b
    18. c", 2);
    19. }
    20. private void testStringCount(String str, int expectedCount) {
    21. assertEquals(expectedCount, countWord.countWithSplit(str));
    22. assertEquals(expectedCount, countWord.countWithStringTokenizer(str));
    23. assertEquals(expectedCount, countWord.countWithChar(str));
    24. }
    25. }

    这三种方法都非常简单,没有什么技术难点,用到了String、StringTokenizer、正则、Guava、JUnit等,非常基础。

    以上就是Java怎么获取字符串单词个数的详细内容,更多关于Java怎么获取字符串单词个数的资料请关注九品源码其它相关文章!