In  this tutorial, we will see “How to count number of words in a String using Java 8?”

Count number of words in a String in Java 8

Java 8 – How to Count the number of Words in a String? Share on X

 

Output:

check if text or string present in a file using java 8

In the above example, It first uses the split method to split the provided string into an array of strings using a regular expression that matches the whitespace as the delimiter. Then, the Arrays.stream() method is used to create a stream of the final array of strings. After that,  count() method is used to get the number of elements in the stream. And, at last, it uses the sysout method to print the count of the words in the string.

Count number of words in a String in Java 8

skip method java 8 stream

Java 8 – How to Count the number of Words in a String? Share on X

Do you like this Post? – then check my other helpful posts:

Other Useful References:

Author

  • Deepak Verma

    Deepak Verma is a Test Automation Consultant and Software development Engineer for more than 10 years. His mission is to help you become an In-demand full stack automation tester. He is also the founder of Techndeck, a blog and online coaching platform dedicated to helping you succeed with all the automation basics to advanced testing automation tricks.

    http://Techndeck.com deepak.rkgit@gmail.com Verma Deepak