Ad Home

Theme images by kelvinjay. Powered by Blogger.

Travel

Header Ads

Java

Selenium

UFT

Framework

General QA Concept

BDD

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

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-
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!!!



«
Next
Newer Post
»
Previous
Older Post

2 comments:

  1. Nice post dude...Good for beginner like me...

    ReplyDelete
  2. Thanks Mainak..I am writing few more articles..pls check out ..I hope these will be very helpful for you.

    ReplyDelete