Results 1 to 10 of 10
- 05-09-2012, 05:23 AM #1
Member
- Join Date
- Apr 2012
- Posts
- 9
- Rep Power
- 0
find index of duplicate words inside string
Hello,
I need to find the index of a word with in a text.
I've been using the text.indexOf(word) function but i have problems when there is more than one of the word.
Like "With the power of the sun."
If the word i needed was the second "the" in the text, i wouldn't be able to find the index with indexOf because it just takes the index of the first "the".
I can't check for the nth occurrence of the word i'm searching for because in the program I don't know what the occurrence of the word is in the text or where it is in the text. (I'm using a scanner to traverse). Any advice would be appreciated.
- 05-09-2012, 05:44 AM #2
Senior Member
- Join Date
- Apr 2012
- Posts
- 199
- Rep Power
- 0
Re: find index of duplicate words inside string
It looks like you're using Strings. Java Strings has a lot of member functions. Take a look at the member function substring(int beginIndex).
- 05-09-2012, 05:48 AM #3
Member
- Join Date
- Apr 2012
- Posts
- 9
- Rep Power
- 0
Re: find index of duplicate words inside string
I'm trying to get the index though.
- 05-09-2012, 05:55 AM #4
- 05-09-2012, 06:00 AM #5
Senior Member
- Join Date
- Apr 2012
- Posts
- 199
- Rep Power
- 0
Re: find index of duplicate words inside string
Once you find the index then use substring(index+3) to get a new substring 0f the old string, i.e. " power of the sun". Then, you can use indexOf(word) again.
Also, you might want to try lastIndexOf. I haven't use it though.
- 05-09-2012, 06:02 AM #6
Senior Member
- Join Date
- Apr 2012
- Posts
- 199
- Rep Power
- 0
- 05-09-2012, 06:23 AM #7
Member
- Join Date
- Apr 2012
- Posts
- 9
- Rep Power
- 0
Re: find index of duplicate words inside string
This is one of the codes that will search the text.Java Code:public JTextPane findPercent(String text,JTextPane textPane) throws BadLocationException{ String num; Integer startIndex=0,endIndex=0; Pattern p = Pattern.compile("[- +]?\\d+(\\.\\d+)?[\\s]?%"); Matcher m = p.matcher(text); while (m.find()) { num=m.group().trim(); startIndex=text.toString().indexOf(num); endIndex=startIndex+num.length(); textPane=highlight(textPane,startIndex,endIndex); } textPane=highlight(textPane,startIndex,endIndex); return textPane; }
In the string "This is 50% and this is 35% and 50% is a 50% number with 50%. Why is 50% 1/2 but 35% is not?"
It will only find the ones i colored and bolded.
The other overloads of indexOf require an index to start looking which I dont see how I can obtain this way.
- 05-09-2012, 09:56 AM #8
Senior Member
- Join Date
- Apr 2012
- Posts
- 199
- Rep Power
- 0
Re: find index of duplicate words inside string
Try something like this:
Java Code:.... while ( (startIndex=text.toString().indexOf(num,startIndex)) > 0 ) { endIndex=startIndex+num.length(); textPane=highlight(textPane,startIndex,endIndex); startIndex = endIndex; } ....
- 05-09-2012, 10:25 AM #9
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: find index of duplicate words inside string
Searching for words within text is a little problematic because of punctuation and things. You might want to investigate BreakIterator.
- 05-09-2012, 03:48 PM #10
Member
- Join Date
- Apr 2012
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
Accessing index inside WAR file
By Rollmops in forum LuceneReplies: 0Last Post: 07-28-2011, 02:49 PM -
creating autocomplete index for multiple words (phrases)
By lrichardson in forum LuceneReplies: 2Last Post: 01-05-2011, 10:30 PM -
find index of string in another string
By Sdannenberg3 in forum New To JavaReplies: 4Last Post: 03-04-2010, 10:14 AM -
Find index position of every word in a String
By pentace in forum New To JavaReplies: 6Last Post: 06-28-2009, 08:26 PM -
using loop to find duplicate
By gwithey in forum New To JavaReplies: 7Last Post: 03-06-2009, 01:46 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks