Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML


 
  
 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 

 
Facing Programming Problem?
Ask Questions?, Browse Latest Questions, Question-Answer Guidelines
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

Word Count

                         

This example counts the number of occurrences of  a specific word in a string. Here we are counting the occurrences of word "you" in a string. To count it we are using countMatches() method.

The org.apache.commons.lang.StringUtils class extends the Object class and defines certain words related to String handling such as null for null,"" for a zero-length string, ' ' for space characters, Charecter.isWhitespace(char) for whitespace and String.trim() for trim. The StringUtils class handles null input strings.

The method used:

countMatches(String str,String sub): This method counts how many times the string sub appears in the String str. This method returns zero if  StringUtils.countMatches(null, *), StringUtils.countMatches("", *) ,StringUtils.countMatches("abba", null),StringUtils.countMatches("abba", "") , and StringUtils.countMatches("abba", "x"). The parameters used  as "str" is String to be checked and "sub" is substring to be count.


 The code of the program is given below:

import org.apache.commons.lang.StringUtils;
 
public class WordCountExample 
{    
    public static void main(String[] args)
    {
    String string = "How r you?R you fine?Where are you going?";    
    System.out.println(StringUtils.countMatches(string,"you")
" occurrences of the word 'you' in '" + string +
                "' is found.");
    }
}

The output of the program is given below:

C:\rajesh\kodejava>javac WordCountExample.java
C:\rajesh\kodejava>java WordCountExample
3 occurrences of the word 'you' in 'How r you?R you fine?
Where are you going?' is found.

Download this example

                         

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (
post your own) View All Comments Latest 10 Comments:

Is this possbile?
If so can you help me?

Also I have problem importing org.apache.commons.lang.StringUtils .. there's nothing like that in the libary, how do I add it?

Thanks

Posted by Chirs on Wednesday, 03.5.08 @ 04:19am | #51347

Latest Searches:
connection pooling
Summary - Expressions
Jframe Tutorials
example of one dimensi
Get first day of week
Junit
print values from tabl
jstl list size
submit
MurachаÑ?°а?аÑ????
connect SQL server in
Ñ?Ð?Ð???Ñ?Ð?Ð?Ñ?Ð?Ð??Ñ
?ั?ะ?ะ?ั?ะ??
bitsets
get Numeric Value
velocity
upload file in java st
delete ajax php
jsp Insert insert data
createbargraphinjava
Murachதà¯?à®?à®?தà
Jigloo SWT/Swing GUI B
static commands
file i/o exeptions
diplay tag in struts
wap to print table 1-5
dispatch action for th
struts2 tag
different Font
mysql-internationaliza
Diff bet MVC Model1 an
read excel file and di
dyna validator form
compress file uploadin
getQueryString
deployment java progra
Unicast AND multicast
iReport
Initialization
how we will use the ma
get windows directory
StringTokenizer specif
parsing
open close session
bean
simpleintrest calculat
online
files read
json lib/componen...]=
json lib/componen...]=
c:out in var of c:fore
Unicast object
call ?�в????�в??
login form using mysql
??????Ñ?Ð?Ð?Ñ?Ð???????
fileupload jsf
populate drop down fro
input in java
java packages
how to retrieve data f
difference between jdk
Combattons la programm
integrating jsp and XM
how to pass data from
create xml using java
MurachÑ?Ð?Ð?Ñ??Ñ?Ð????
File Stream
static Scanner console
ejb 2 tutorial
Matrix Example in Java
JFreechart3D:Line3D
validation of username
Checked exceptions
how to create a dialog
How to insert multiple
JFreechart:Line3D
code for inserting val
jtree
architecture
Aதà¯??தàÂ
Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.