how to count the exact number of words in a string that has empty spaces between words?

前端 未结 9 1790
猫巷女王i
猫巷女王i 2020-12-03 14:35

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

9条回答
  •  佛祖请我去吃肉
    2020-12-03 15:23

    It should be easy with:

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

提交回复
热议问题