How do I count the number of words in a string?

后端 未结 8 1645
感情败类
感情败类 2021-01-13 02:54

I need to count the number of words and I am assuming the correct way to do it is by calculating the number of times that the previous character in a string is not a letter

8条回答
  •  萌比男神i
    2021-01-13 03:16

    You can use String.split() to convert the string into an array, with one word in each element. The number of words is given by the length of the array:

    int words = myString.split("\s+").length;
    

提交回复
热议问题