Is the best way to check for 2 words of same type in a string to make a stringtokenizer and story every word in an array then compare it in a for loop?
seems like a hassle for comparing for same words i was wondering if theres a better way?
String has a split function that's more efficient than StringTokenizer.
But for your problem take a look at regular expressions, they were invented for things like that.
There's a set of functions "indexOf" in String which you can use to look for the starting index of a substring in a string.
i have a similar problem where i have to find the number of occurences of a particular word from a sentence, i tried the code but it even counts the words which have those alphabets partly. can anyone help me with this
public class StringCount2
public static void main(String args)
String searchFor = "is";
String base = "This is object oriented programming language ";
int len = searchFor.length();
//System.out.println( "length of object =" +len);
int result = 0;
if (len > 0)
int start = base.indexOf(searchFor);
// System.out.println("index of object =" + start);
while (start != -1)
start = base.indexOf(searchFor, start+len);