Simplifying Java Software Development: How to Count the Number of Words in a String using Java (one-liner)
By Angsuman Chakraborty, Gaea News NetworkSaturday, June 18, 2005
Java is a language of choice for millions of developers worldwide. In a series of articles I will show simple tips and techniques which make Java extremely powerful and yet simple to use. Today’s article is about using regex, a pattern matcher incorporated in Java (from 1.4 I believe).
Here is a sample code (line in bold) to count the number of words in any amount of text. The sample program counts the number of words in the argument to the program. The argument must be quoted to ensure separate words are clubbed together in a single sentence by the operating system.
public class WordCount { public static void main(String args[]) { System.out.println(java.util.regex.Pattern.compile("[\\w]+").split(args[0].trim()).length); } }
Sample usage:
java WordCount “This is a sample phrase with 8 words”
All its does is compile the regular expression “\w” which matches with words and splits the phrase in an array of individual words. Then the total length of the array is printed.
March 20, 2010: 9:51 am
what will i do if the program ask was getting Background: N! (N factorial) can be quite irritating and difficult to compute for large values |
![]() sally |
March 20, 2010: 2:33 am
how to write a java program that identifies how many “int”and “char”declaration are there in a given java program? |
![]() sally |
March 20, 2010: 2:19 am
how to write a java program that identify how many “int” and “char”declaration are there in a given java program? |
![]() rajnish |
![]() Masila |
![]() mamduh |
![]() mamduh |
![]() MuthamizhSelvan |
December 8, 2005: 2:56 am
Dear Sir, |
June 19, 2005: 10:43 am
The regex is fine when you just deal with English, or other alphabetic languages, but when you’re dealing with Chinese or other Asian languages, your solution won’t work. Instead, you’d rather use Java’s locale specific String collators, in java.text.*. It’s much cleaner to use this approach, than looking for the simplest one-liner solution for English. |
June 19, 2005: 5:14 am
Even shorter: or int count = args[0].trim().split(”\\s”).length; |
June 19, 2005: 12:52 am
Thanks. Can you please try it again and let me know if the problem is fixed on your end. You will have to refresh the feed. You can use any feed url. I have tested here on two offline feed aggregators. Thanks again for the catch. |
June 19, 2005: 12:33 am
I think it started with this one: https://blog.taragana.com/index.php/archive/what-is-your-favorite-programming-language/ |
June 19, 2005: 12:29 am
Can you please tell me from when it started happening? I saw the problem with the main feed and some other feeds like technology and web feeds. Looks like a problem with some characters in some posts. I am trying to find it. Debugging PHP is such a royal pain! |
June 19, 2005: 12:22 am
Here is what I got: Sponsor(s): You are on the phone with your client, going through an RFP that you emailed them. You hear them flip through the pages. Suddenly, they ask: “How much will this cost?” This happens every day, right? Not with GoToMeeting, the easiest way to hold online meetings. You can present, share, and field questions - right from your desk. So you stop the “skip ahead syndrome” before it starts. Try it FREE now. |
June 19, 2005: 12:16 am
That’s is very surprising. Thanks for pointing it out. It was working well before. I tested it several times. |
June 19, 2005: 12:02 am
Come on! There was absolutely no content in the feed. Not even a teaser. Just ads. Can you point me to one of your feeds with some content (and preferably no ads), or would you rather me unsubscribe? |
lei