Best Way to Check for same Word in string?

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2005
Posts: 46
Reputation: Gink is an unknown quantity at this point 
Solved Threads: 0
Gink Gink is offline Offline
Light Poster

Best Way to Check for same Word in string?

 
0
  #1
Feb 6th, 2005
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?
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Best Way to Check for same Word in string?

 
0
  #2
Feb 6th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: Best Way to Check for same Word in string?

 
0
  #3
Feb 6th, 2005
you could always use this:

boolean equalsIgnoreCase(String anotherString);
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Best Way to Check for same Word in string?

 
0
  #4
Feb 6th, 2005
No, that compares two Strings. Here the problem is to detect whether a single word is repeated several times inside a single String.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 28
Reputation: newtechie is an unknown quantity at this point 
Solved Threads: 0
newtechie newtechie is offline Offline
Light Poster

Re: Best Way to Check for same Word in string?

 
0
  #5
Jul 30th, 2008
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







  1. import java.lang.String;
  2.  
  3. public class StringCount2
  4. {
  5. public static void main(String args[])
  6. {
  7. String searchFor = "is";
  8. String base = "This is object oriented programming language ";
  9. int len = searchFor.length();
  10. //System.out.println( "length of object =" +len);
  11. int result = 0;
  12. if (len > 0)
  13. {
  14. int start = base.indexOf(searchFor);
  15. // System.out.println("index of object =" + start);
  16. while (start != -1)
  17. {
  18. result++;
  19. start = base.indexOf(searchFor, start+len);
  20.  
  21. }
  22. }
  23. System.out.println(result);
  24. }
  25. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 413
Reputation: sciwizeh is on a distinguished road 
Solved Threads: 22
sciwizeh's Avatar
sciwizeh sciwizeh is offline Offline
Posting Pro in Training

Re: Best Way to Check for same Word in string?

 
0
  #6
Jul 30th, 2008
this thread is two years old, why not post in another topic? many people also frown upon thread hijacking
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "
-Albert Einstein
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 28
Reputation: newtechie is an unknown quantity at this point 
Solved Threads: 0
newtechie newtechie is offline Offline
Light Poster

Re: Best Way to Check for same Word in string?

 
0
  #7
Jul 31st, 2008
ok i will do that
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 5695 | Replies: 6
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC