How To Find Occurrence of A Word in a String in Java

Share and Enjoy !

Most of the time in several discussion I have been asked this question that how to find a word inside a string.

Today I thought to write an article on how to find out a word in string.

Let us assume I have a string which is given as “I am a good boy. I am a bad boy” and I need to find the occurrence of “am”

Let me create a method that will provide me the count of “am”. To make this function reusable I have taken two parameters
1. The string to be searched
2. The particular word to be searched

My algorithm is very straight
I will get the string and then split with space. This will give the words in an array. Now loop through the array to find out the particular word.if the word is found just increment the counter.



private int getCount(String str, String srchstr) {

String Ori=str;
String srt=srchstr;
int count=0;
String[] splits = Ori.split(” “);
for(int i=0 ;i<splits.length;i ) {

if(splits[i].equals(srt)) {
count ;
}
}

return count;
}


Now let me create the main function-

Ads code goes here
public static void main(String[] args) {
String str=”I am a good boy. I am a bad boy”;
String srchstr=”am”;
int val=0;
Search search = new Search();
val=search.getCount(str, srchstr);
System.out.println(“The occurance of ” srchstr ” in the given string ” val ” times”);
}


The main method will call the method “getCount” with specified parameters and the getCount method will provide me the results.

Now let us more customize it..
Let us take an user input here…You can check out this tutorial how to get user input
here.

String str = null;
str = JOptionPane.showInputDialog(null, “What is your String?”, “Enter your String”, JOptionPane.QUESTION_MESSAGE);

and What need to be searched..

String srchstr = null;
srchstr = JOptionPane.showInputDialog(null, “What is your Search word?”, “Enter your word to be searched?”, JOptionPane.QUESTION_MESSAGE);


This will give more power to the user to do custom search..
The full code goes like-

import javax.swing.JOptionPane;


public class Search {


public static void main(String[] args) {
String str = null;
str = JOptionPane.showInputDialog(null, “What is your String?”, “Enter your String?”, JOptionPane.QUESTION_MESSAGE);
//String str=”I am a good boy. I am a bad boy”;
String srchstr = null;
srchstr = JOptionPane.showInputDialog(null, “What is your Search word?”, “Enter your word to be searched?”, JOptionPane.QUESTION_MESSAGE);
//String srchstr=”am”;
int val=0;
Search search = new Search();
val=search.getCount(str, srchstr);
System.out.println(“The occurance of ” srchstr ” in the given string ” val ” times”);
}

private int getCount(String str, String srchstr) {

String Ori=str;
String srt=srchstr;
int count=0;
String[] splits = Ori.split(” “);
for(int i=0 ;i<splits.length;i ) {

if(splits[i].equals(srt)) {
count ;
}
}

return count;
}
}

You are done!!!

READ  Class InetAddress in Java

Share and Enjoy !

38 thoughts on “How To Find Occurrence of A Word in a String in Java”

  1. Spot on with this write-up, I truly assume this website needs far more consideration. I’ll most likely be once more to read rather more, thanks for that info.

  2. You ought to take part in a contest for one of the
    most useful sites on the internet. I will highly recommend
    this website!

  3. mckenziedavenport

    This is very interesting, You are a very skilled blogger. I have joined your feed and look forward to seeking more of your excellent post.
    Also, I’ve shared your site in my social networks!

  4. Nice post. I was checking continuously this weblog and I
    am inspired! Extremely helpful information particularly the remaining
    part 🙂 I maintain such info a lot. I used to be looking for this
    certain info for a long time. Thank you and best of
    luck.

  5. obviously like your web-site but you have to test the spelling on quite a few of
    your posts. Several of them are rife with spelling issues and I find it very bothersome
    to inform the truth however I’ll certainly come back again.

  6. Can I just say what a comfort to find someone who really understands what they are discussing online.

    You definitely know how to bring a problem to light and make it important.
    A lot more people ought to look at this and understand
    this side of the story. I was surprised you’re not more popular since
    you certainly possess the gift.

  7. That is a good tip particularly to those fresh to the blogosphere. Simple but very precise info… Many thanks for sharing this one. A must read article!

  8. Thanks on your marvelous posting! I certainly enjoyed reading it, you’re a great author.I will always bookmark your blog
    and will eventually come back in the future. I want
    to encourage continue your great writing, have a nice holiday weekend!

  9. Hi there, this weekend is fastidious in favor of me, because this moment i am reading this enormous informative post here at my residence.

  10. Everyone loves what you guys tend to be up too.
    This type of clever work and exposure! Keep up the superb works guys
    I’ve added you guys to blogroll.

  11. I’m not sure exactly why but this website is loading extremely
    slow for me. Is anyone else having this problem or is it a problem on my end?
    I’ll check back later and see if the problem still exists.

  12. Pretty part of content. I just stumbled upon your blog and in accession capital to claim that I get actually loved
    account your weblog posts. Anyway I’ll be subscribing on your augment and
    even I success you get right of entry to consistently fast.

  13. Asking questions are actually nice thing if you are not understanding something completely, but this post presents good understanding even.

  14. Great blog! Is your theme custom made or
    did you download it from somewhere? A theme
    like yours with a few simple tweeks would really make my
    blog stand out. Please let me know where you got your design. Appreciate it

  15. What i do not understood is in truth how you are no longer really a lot more neatly-favored than you might be
    now. You’re so intelligent. You understand therefore significantly in terms of this matter, produced me individually
    consider it from numerous various angles. Its like women and men aren’t interested until it’s something
    to accomplish with Girl gaga! Your individual stuffs outstanding.
    At all times care for it up!

  16. Hello there! I simply would like to give you
    a huge thumbs up for the excellent info you have got right here on this post.
    I am coming back to your web site for more soon.

  17. Pingback: Google

  18. Pingback: Google

  19. Pingback: Google

  20. Pingback: Google

  21. Hello it’s me, I am also visiting this website daily, this
    site is in fact fastidious and the users are actually sharing pleasant thoughts.

  22. Hi to all, it’s truly a fastidious for me to pay a visit this site, it includes important Information.

  23. Just want to say your article is as astounding. The clearness on your
    publish is just cool and i can think you are knowledgeable
    on this subject. Fine with your permission let me to grab your feed to
    keep updated with imminent post. Thanks 1,000,000 and please carry
    on the rewarding work. adreamoftrains content hosting

  24. Pingback: Google

  25. Pingback: Google

  26. Pingback: Google

  27. Pingback: Google

  28. Pingback: Google

  29. Pingback: Google

  30. Pingback: Google

  31. Pingback: Google

  32. Pingback: Google

Leave a Comment

Your email address will not be published. Required fields are marked *