The first approach does count hyphens while the second doesn’t. Here, we have created stringTokenizer1 with passing a string in the constructor, it takes default delimiter whereas for stringTokenizer2 custom delimiter to separate tokens is passed. How many words in my text? With Notepad++ word count tool Word count without delimiter: 6 ("Word count: "+String.valueOf(tokensCount2)) ("Word count without delimiter: "+String.valueOf(tokensCount1)) StringTokenizer stringTokenizer2 = new StringTokenizer(myString,mydelim) StringTokenizer stringTokenizer1 = new StringTokenizer(myString) The StringTokenizer class does not count the whitespace and tabs automatically hence it is handled on its own.
The characters that separate tokens (delimiters) may be specified either at the time of creation or on a per-token basis.
The methods in this class do not differentiate among quoted strings, identifiers, and numbers, nor do they recognize or skip comments. The StringTokenizer class in Java allows us to break a string into multiple tokens. Use StringTokenizer to Count Words in a String in Java
#Word counter java how to
In this article, we are going to shed some light on how to count the number of words in a string in Java and different ways to achieve this. Get Number of Times a Word Is Repeated in a String in Java.Use split() and Regular Expression to Count Words in a String in Java.Use StringTokenizer to Count Words in a String in Java.