Java How To Count Words
Count Number of Words in a String
You can easily count the number of words in a string with the following example:
ExampleGet your own Java Server
String words = "One Two Three Four";
int countWords = words.split("\\s").length;
System.out.println(countWords);
Try it Yourself »