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.
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).
Re: find index of duplicate words inside string
I'm trying to get the index though.
Re: find index of duplicate words inside string
Quote:
Originally Posted by
onions
i wouldn't be able to find the index with indexOf because it just takes the index of the first "the".
There are four overloads of indexOf(...) -- choose the one that can do what you need.
db
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.
Re: find index of duplicate words inside string
Quote:
Originally Posted by
DarrylBurke
There are four overloads of indexOf(...) -- choose the one that can do what you need.
db
Darryl suggestion is much better.
Re: find index of duplicate words inside string
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;
}
This is one of the codes that will search the text.
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.
Re: find index of duplicate words inside string
Try something like this:
Code:
....
while ( (startIndex=text.toString().indexOf(num,startIndex)) > 0 )
{
endIndex=startIndex+num.length();
textPane=highlight(textPane,startIndex,endIndex);
startIndex = endIndex;
}
....
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.
Re: find index of duplicate words inside string
Quote:
Originally Posted by
shall
Try something like this:
Code:
....
while ( (startIndex=text.toString().indexOf(num,startIndex)) > 0 )
{
endIndex=startIndex+num.length();
textPane=highlight(textPane,startIndex,endIndex);
startIndex = endIndex;
}
....
This worked perfectly. Thank you!