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?

12 Years
Discussion Span
Last Post by newtechie

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.


No, that compares two Strings. Here the problem is to detect whether a single word is repeated several times inside a single 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

import java.lang.String;

   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);

this thread is two years old, why not post in another topic? many people also frown upon thread hijacking

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.