如何计算一个字之间有空格的字符串中的确切单词数?

时间:2022-09-13 11:41:48

Write a method called wordCount that accepts a String as its parameter and returns the number of words in the String. A word is a sequence of one or more nonspace characters (any character other than ' '). For example, the call wordCount("hello") should return 1, the call wordCount("how are you?") should return 3, the call wordCount(" this string has wide spaces ") should return 5, and the call wordCount(" ") should return 0.

编写一个名为wordCount的方法,该方法接受字符串作为参数,并返回字符串中的单词数。一个单词是一个或多个非空格字符的序列(任何字符除外)。例如,调用wordCount("hello")应该返回1,调用wordCount("how are you?")应该返回3,调用wordCount(" this string has wide spaces ")应该返回5,调用wordCount(" ")应该返回0。

I made a function:

我做了一个功能:

public static int wordCount(String s){

  int counter = 0;

  for(int i=0; i<=s.length()-1; i++) {

    if(Character.isLetter(s.charAt(i))){

      counter++;

      for(i<=s.length()-1; i++){

        if(s.charAt(i)==' '){

          counter++;
        }
      }                
    }
  }

  return counter;
}

But i know this has 1 limitation that it will also count the number of spaces after all the words in the string have finished nad it will also count 2 blank spaces as possibly being 2 words :( Is there a predefined function for word count? or can this code be corrected?

但是我知道这有一个限制,它也会在字符串中的所有单词完成nad之后计算空格的数量它也会计算2个空格,可能是2个单词:(有预定义的单词计数函数吗?)或者这个代码可以被修正吗?

9 个解决方案

#1


30  

If you want to ignore leading, trailing and duplicate spaces you can use

如果想要忽略前导、尾随和重复的空格,可以使用

String trimmed = text.trim();
int words = trimmed.isEmpty() ? 0 : trimmed.split("\\s+").length;

#2


6  

public static int wordCount(String s){
    if (s == null)
       return 0;
    return s.trim().split("\\s+").length;
}

Have fun with the function.

享受这个功能。

#3


2  

String str="I am a good boy";
String[] words=str.split("\\s+");
System.out.println(words.length);

#4


0  

http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String)

http://docs.oracle.com/javase/7/docs/api/java/lang/String.html分裂(以)

Splits this string around matches of the given regular expression.

将这个字符串分割为给定正则表达式的匹配项。

#5


0  

It should be easy with:

应该很容易做到:

String[] arr = "how are you sir".split("\\s");
System.out.printf("Count [%d]%n", arr.length);

#6


0  

Added some lines to your code:

在您的代码中添加了一些行:

public static int wordCount(String s){
    int counter=0;
    for(int i=0;i<=s.length()-1;i++){
            if(Character.isLetter(s.charAt(i))){
                    counter++;
                    for(;i<=s.length()-1;i++){
                            if(s.charAt(i)==' '){
                                    counter++;
                                    i++;
                                    while (s.charAt(i)==' ')
                                        i++;
                                    }
                            }

                    }


            }
            return counter;
   }

#7


0  

Simply use s.split(" ").length and for wide spaces...use s.trim().replaceAll("\\s+"," ").split(" ").length

简单地使用。(" ")。长度和宽阔的空间……使用s.trim()。replaceAll(" \ \ s + "," ")。(" "). length

#8


0  

public static int wordCount(String s){
    int counter=0;
    for(int i=0;i<=s.length()-1;i++){
        if(Character.isLetter(s.charAt(i))){
            counter++;
            for(;i<=s.length()-1;i++){
                if(s.charAt(i)==' '){
                    i++;
                    break;
                }
            }
        }
    }
    return counter;
}

This is what you need if don't use the predefined function. I have tested it by myself. Please let me know if there is any bugs!

如果不使用预定义函数,这就是您需要的。我自己测试过了。如果有虫子请告诉我!

#9


0  

My few solutions:

我的几个解决方案:

public static int wordcount1(String word) {

        if (word == null || word.trim().length() == 0) {
            return 0;
        }

        int counter = 1;

        for (char c : word.trim().toCharArray()) {
            if (c == ' ') {
                counter++;
            }
        }
        return counter;
    }

//

/ /

public static int wordcount2(String word) {
        if (word != null || word.length() > 0) {
            return word.trim().length()
                    - word.trim().replaceAll("[ ]", "").length() + 1;
        } else {
            return 0;
        }
    }

// Recursive

/ /递归

public static int wordcount3(String word) {
        if (word == null || word.length() == 0) {
            return 0;
        }
        if (word.charAt(0) == ' ') {
            return 1 + wordcount3(word.substring(1));
        }
        return wordcount3(word.substring(1));
    }

//

/ /

public static int wordcount4(String word) {
        if (word == null || word.length() == 0) {
            return 0;
        }

        String check = word.trim();
        int counter = 1;
        for (int i = 0; i < check.length(); i++) {
            if (i > 0 && Character.isSpaceChar(check.charAt(i))
                    && !Character.isSpaceChar(check.charAt(i - 1))) {
                counter++;
            }
        }
        return counter;
    }

#1


30  

If you want to ignore leading, trailing and duplicate spaces you can use

如果想要忽略前导、尾随和重复的空格,可以使用

String trimmed = text.trim();
int words = trimmed.isEmpty() ? 0 : trimmed.split("\\s+").length;

#2


6  

public static int wordCount(String s){
    if (s == null)
       return 0;
    return s.trim().split("\\s+").length;
}

Have fun with the function.

享受这个功能。

#3


2  

String str="I am a good boy";
String[] words=str.split("\\s+");
System.out.println(words.length);

#4


0  

http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String)

http://docs.oracle.com/javase/7/docs/api/java/lang/String.html分裂(以)

Splits this string around matches of the given regular expression.

将这个字符串分割为给定正则表达式的匹配项。

#5


0  

It should be easy with:

应该很容易做到:

String[] arr = "how are you sir".split("\\s");
System.out.printf("Count [%d]%n", arr.length);

#6


0  

Added some lines to your code:

在您的代码中添加了一些行:

public static int wordCount(String s){
    int counter=0;
    for(int i=0;i<=s.length()-1;i++){
            if(Character.isLetter(s.charAt(i))){
                    counter++;
                    for(;i<=s.length()-1;i++){
                            if(s.charAt(i)==' '){
                                    counter++;
                                    i++;
                                    while (s.charAt(i)==' ')
                                        i++;
                                    }
                            }

                    }


            }
            return counter;
   }

#7


0  

Simply use s.split(" ").length and for wide spaces...use s.trim().replaceAll("\\s+"," ").split(" ").length

简单地使用。(" ")。长度和宽阔的空间……使用s.trim()。replaceAll(" \ \ s + "," ")。(" "). length

#8


0  

public static int wordCount(String s){
    int counter=0;
    for(int i=0;i<=s.length()-1;i++){
        if(Character.isLetter(s.charAt(i))){
            counter++;
            for(;i<=s.length()-1;i++){
                if(s.charAt(i)==' '){
                    i++;
                    break;
                }
            }
        }
    }
    return counter;
}

This is what you need if don't use the predefined function. I have tested it by myself. Please let me know if there is any bugs!

如果不使用预定义函数,这就是您需要的。我自己测试过了。如果有虫子请告诉我!

#9


0  

My few solutions:

我的几个解决方案:

public static int wordcount1(String word) {

        if (word == null || word.trim().length() == 0) {
            return 0;
        }

        int counter = 1;

        for (char c : word.trim().toCharArray()) {
            if (c == ' ') {
                counter++;
            }
        }
        return counter;
    }

//

/ /

public static int wordcount2(String word) {
        if (word != null || word.length() > 0) {
            return word.trim().length()
                    - word.trim().replaceAll("[ ]", "").length() + 1;
        } else {
            return 0;
        }
    }

// Recursive

/ /递归

public static int wordcount3(String word) {
        if (word == null || word.length() == 0) {
            return 0;
        }
        if (word.charAt(0) == ' ') {
            return 1 + wordcount3(word.substring(1));
        }
        return wordcount3(word.substring(1));
    }

//

/ /

public static int wordcount4(String word) {
        if (word == null || word.length() == 0) {
            return 0;
        }

        String check = word.trim();
        int counter = 1;
        for (int i = 0; i < check.length(); i++) {
            if (i > 0 && Character.isSpaceChar(check.charAt(i))
                    && !Character.isSpaceChar(check.charAt(i - 1))) {
                counter++;
            }
        }
        return counter;
    }