Count number of words in string Java example shows how to count number of words in string in Java using the charAt method and regular expression.
How to count number of words in Sting in Java?
1) Count the number of words in a string using the charAt method
We can use the charAt
method of the String class to count the number of words as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
package com.javacodeexamples.stringexamples; public class CountNumberOfWordsExample { public static void main(String[] args) { String[] sentences = { "Count number of word in String", "Hello World", "Java Example. Hello", " String count words " }; for(String sentence : sentences) System.out.println(sentence + ": " + countWords(sentence)); } private static int countWords(String str){ int wordCount = 0; //if string is null, word count is 0 if(str == null) return wordCount; //remove leading and trailing white spaces first str = str.trim(); //if string contained all the spaces, word count is 0 if(str.equals("")) return wordCount; for(int i = 0; i < str.length(); i++){ //if character is a space, increase the word count if( str.charAt(i) == ' ' ){ wordCount++; } } return wordCount; } } |
Output
1 2 3 4 |
Count number of word in String: 5 Hello World: 1 Java Example. Hello: 2 String count words : 4 |
Basically we looped through the characters of each of the string values and when we found a space character, we increased the word count by 1. By looking at the output, it seems that we did not take care of the last word since there is no space after the last word in the string. Also, if the string contains more than one consecutive spaces, our code ended up counting it as a separate word hence we got 4 word count in the sentence ” String count words “.
Let’s fix the problem. Have a look at the below given modified code for the countWords
method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
private static int countWords(String str){ int wordCount = 0; //if string is null, word count is 0 if(str == null) return wordCount; //remove leading and trailing white spaces first str = str.trim(); //if string contained all the spaces, word count is 0 if(str.equals("")) return wordCount; for(int i = 0; i < str.length(); i++){ //if character is a space, increase the word count if( str.charAt(i) == ' ' ){ wordCount++; //skip all the consecutive white spaces while(str.charAt(i) == ' ' && i < str.length() - 1) i++; }else{ /* * If we have reached at end of the string, * we need to count the last word */ if(i == str.length() - 1) wordCount++; } } return wordCount; } |
Output
1 2 3 4 |
Count number of word in String: 6 Hello World: 2 Java Example. Hello: 3 String count words : 3 |
We made two changes, first, we added a while loop which skips through all the consecutive white spaces from the string. Second, we added else block which checks whether we have reached at end of the string and if we have, it increments the number of word by one.
Note: This approach does not work for word separators other than space (such as dot, comma or quotes).
2) Using Java regular expression
We can count the number of words using a regular expression pattern “\\s+” along with the split method. The pattern “\\s” means space while the “\\s+” pattern means one or more spaces in a regular expression.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
private static int countWords(String str){ int wordCount = 0; //if string is null, word count is 0 if(str == null) return wordCount; //remove leading and trailing white spaces first str = str.trim(); //if string contained all the spaces, word count is 0 if(str.equals("")) return wordCount; //split the string by one or more space String[] temp = str.split("\\s+"); //number of array elements is equal to the words return temp.length; } |
Output
1 2 3 4 |
Count number of word in String: 6 Hello World: 2 Java Example. Hello: 3 String count words : 3 |
Note: This approach also does not work for word separators other than the space character.
Instead of the “\\s+” pattern, the “\\w+” pattern should be used. The “\\w” pattern matches a word in a regular expression. For example, “Java Example.Hello” string will return word count as 2 using the “\\s+” pattern because “Example” and “Hello” are not separated by a space character but the dot. While the “\\w+” pattern will return word count as 3 since it matches words as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
private static int countWords(String str){ int wordCount = 0; //if string is null, word count is 0 if(str == null) return wordCount; //remove leading and trailing white spaces first str = str.trim(); //if string contained all the spaces, word count is 0 if(str.equals("")) return wordCount; //split the string by one or more space String[] temp = str.split("\\w+"); //number of array elements is equal to words return temp.length; } |
Output
1 2 3 4 |
Count number of word in String: 6 Hello World: 2 Java Example.Hello: 3 String count words : 3 |
This example is a part of the Java String tutorial.
Please let me know your views in the comments section below.