【问题标题】:how to count the exact number of words in a string that has empty spaces between words?如何计算单词之间有空格的字符串中的确切单词数?
【发布时间】:2012-01-19 10:37:25
【问题描述】:

编写一个名为 wordCount 的方法,该方法接受一个字符串作为其参数并返回字符串中的单词数。单词是一个或多个非空格字符的序列(' ' 以外的任何字符)。例如,调用 wordCount("hello") 应该返回 1,调用 wordCount("你好吗?") 应该返回 3,调用 wordCount("this string has wide spaces") 应该返回 5,调用 wordCount (" ") 应该返回 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)==' '){

          counter++;
        }
      }                
    }
  }

  return counter;
}

但我知道这有 1 个限制,它还会在字符串中的所有单词完成后计算空格数,并且它还会将 2 个空格算作可能是 2 个单词 :( 是否有预定义的字数统计功能?或者这个代码可以更正吗?

【问题讨论】:

  • 你不能只做String#split("\\s"); 并获取结果数组的长度。
  • 您是否缺少[homework] 标签?

标签: java


【解决方案1】:

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

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

【讨论】:

  • +1 表示第一个答案,考虑到连续的多个空格。
  • 和前导空格。 split() 默认会忽略尾随分隔符。
  • @PeterLawrey- text=" ";
  • 失败是什么意思?你的意思是它返回 1?
  • @PeterLawrey-yA 它返回 1,它应该返回 0
【解决方案2】:
public static int wordCount(String s){
    if (s == null)
       return 0;
    return s.trim().split("\\s+").length;
}

玩得开心。

【讨论】:

    【解决方案3】:
    String str="I am a good boy";
    String[] words=str.split("\\s+");
    System.out.println(words.length);
    

    【讨论】:

    • 失败,因为“这个字符串有很大的空格”
    【解决方案4】:
    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;
    }
    

    如果不使用预定义的功能,这就是您所需要的。我自己测试过。如果有任何错误请告诉我!

    【讨论】:

      【解决方案5】:

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

      围绕给定正则表达式的匹配拆分此字符串。

      【讨论】:

      • 我会使用 Java 7 javadoc。 ;)
      【解决方案6】:

      应该很容易:

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

      【讨论】:

        【解决方案7】:

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

        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;
           }
        

        【讨论】:

          【解决方案8】:

          只需使用s.split(" ").length,对于宽阔的空间...使用s.trim().replaceAll("\\s+"," ").split(" ").length

          【讨论】:

          • 失败,因为“这个字符串有很大的空格”
          • 这适用于多个空格 s.replaceAll("\\s+"," ").split(" ").length
          【解决方案9】:

          我的几个解决方案:

          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;
                  }
              }
          

          // 递归

          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 个连续空格,则解决方案将失败。
          猜你喜欢
          • 1970-01-01
          • 2016-03-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-07-22
          • 1970-01-01
          • 2018-03-01
          • 1970-01-01
          相关资源
          最近更新 更多