954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Best Way to Check for same Word in string?

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?

Gink
Light Poster
46 posts since Feb 2005
Reputation Points: 10
Solved Threads: 0
 

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.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

you could always use this:

boolean equalsIgnoreCase(String anotherString);

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

No, that compares two Strings. Here the problem is to detect whether a single word is repeated several times inside a single String.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

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) 
    {
     result++;
     start = base.indexOf(searchFor, start+len);
     
    }
    }
     System.out.println(result);
    }
 }
newtechie
Light Poster
33 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

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

sciwizeh
Posting Pro in Training
457 posts since Jun 2008
Reputation Points: 77
Solved Threads: 23
 

ok i will do that

newtechie
Light Poster
33 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You