The Artima Developer Community
Sponsored Link

Web Buzz Forum
Simplifying Java: How to Count the Number of Words...

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Angsuman Chakraborty

Posts: 19816
Nickname: angsuman
Registered: Dec, 2003

Angsuman Chakraborty is the CEO and Chief Architect at Taragana
Simplifying Java: How to Count the Number of Words... Posted: Jun 22, 2005 6:04 PM
Reply to this message Reply

This post originated from an RSS feed registered with Web Buzz by Angsuman Chakraborty.
Original Post: Simplifying Java: How to Count the Number of Words...
Feed Title: LinkBlog - Java, Bioinformatics, Outsourcing, Web
Feed URL: http://feeds.feedburner.com/Linkblog-JavaBioinformaticsOutsourcingWeb
Feed Description: A LinkBlog of my web travels. It includes links of my interests which are Java, Bioinformatics, Software Outsourcing, Web Technologies, Diabetes, Global Politics etc.
Latest Web Buzz Posts
Latest Web Buzz Posts by Angsuman Chakraborty
Latest Posts From LinkBlog - Java, Bioinformatics, Outsourcing, Web

Advertisement
Simplifying Java: How to Count the Number of Words in a String using Java (one-liner)

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);
}
}

Read: Simplifying Java: How to Count the Number of Words...

Topic: AjaxPatterns Previous Topic   Next Topic Topic: German translation of searchhi

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use