Results 1 to 7 of 7
Thread: Finding a word in a sentence?
- 07-22-2010, 06:49 AM #1
Member
- Join Date
- Jul 2010
- Posts
- 1
- Rep Power
- 0
Finding a word in a sentence?
Hi, I'm very new to java and am a bit lost really.
I'm working with a String called 'sentence', which equals "the quick brown fox jumped over the lazy dog."
How can I query this, asking if the sentence includes the word "fox", printing the result as "Yes", or "No if "fox isn't part of the sentence? Would this have to be done in an array, or am I way off the mark?
Thanks in advance!
- 07-22-2010, 07:24 AM #2
You could do it in an array; I believe Java supports tokenizing a String (splitting it up into "tokens" based on a specific character as a separator). You could also do it by looping through each character in the string.
I'd suggest looking at these:
StringTokenizer (Java 2 Platform SE v1.4.2)
Parsing Strings with split
loop through a string in java - Java answers
Good luck!
- 07-22-2010, 09:52 AM #3
Member
- Join Date
- Jul 2010
- Posts
- 1
- Rep Power
- 0
You can use the indexOf method of the String.
String string = "the quick brown fox jumped over the lazy dog.";
Boolean b = string.indexOf("fox") > 0;
This is case-sensitive. If you want it to ignore the case just use regex.
Hope this helps.
- 07-22-2010, 07:34 PM #4
That would also find "abcfoxdef" in the sentence though. He'd have to then check if the string started with "fox ", if " fox " was in the sentence, or if it ended with " fox".
- 07-22-2010, 07:46 PM #5
OP needs to consider what the definition of a word is. What are its delimiters?
white spaces, punctuation, or ()s or ???
Could have a method the took a char and returned true if the char was a valid word delimiter. Then with indexOf() finding a substring, testing for valid delimiters.
- 07-22-2010, 10:38 PM #6
Java Code:String sentence = "the quick brown fox jumped over the lazy dog."; System.out.println(sentence.contains("fox") ? "yes" : "no");
- 07-22-2010, 11:07 PM #7
Similar Threads
-
Finding and storing a word from a string builder
By ao241 in forum Advanced JavaReplies: 3Last Post: 06-28-2010, 12:46 PM -
please help with counting specific word in a sentence
By noobinoo in forum New To JavaReplies: 4Last Post: 05-07-2010, 02:06 PM -
enter a string sentence
By amorosa19 in forum New To JavaReplies: 11Last Post: 01-28-2009, 04:05 AM -
String/sentence to unicode convertion
By sandeepvreddy in forum New To JavaReplies: 5Last Post: 11-20-2008, 03:33 PM -
How to extract info from a sentence
By luisarca in forum XMLReplies: 1Last Post: 06-07-2007, 05:43 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks