Searching a word in a String(s)

Searching for a word(s) in String(s) is a very common coding problem which we face day to day. 

A usual solution which comes to our mind is parsing the entire string. It becomes more time consuming if there are a bunch of strings. 

A simple solution to this is this : 

Given a word say “word” and we have a bunch of strings in a n array say “strings”, the just put a for loop on this array and check the following in that loop :

for(String Strings1 : strings) {

int index=Strings1.toLowercase().indexof(word.tolowerCase());

 if(index !=-1) {

return 1;

}

else return 0;

}

That is , just check whether word exists in string using indexOf operator. 

Its simple as that 🙂 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s