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

后端 未结 8 1595
感情败类
感情败类 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条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-13 03:34

    The reason you are getting an IndexOutOfBoundsException is probably because when i is 0 your inner loop will have string.charAt(i-1) which will throw an exception since 0-1 is -1. If you fix that your method might work, although you can use more efficient techniques.

提交回复
热议问题