Results 1 to 10 of 10
Like Tree1Likes
  • 1 Post By shall

Thread: find index of duplicate words inside string

  1. #1
    onions is offline Member
    Join Date
    Apr 2012
    Posts
    9
    Rep Power
    0

    Default 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.

  2. #2
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default 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).

  3. #3
    onions is offline Member
    Join Date
    Apr 2012
    Posts
    9
    Rep Power
    0

    Default Re: find index of duplicate words inside string

    I'm trying to get the index though.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: find index of duplicate words inside string

    Quote Originally Posted by onions View Post
    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
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default 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.

  6. #6
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: find index of duplicate words inside string

    Quote Originally Posted by DarrylBurke View Post
    There are four overloads of indexOf(...) -- choose the one that can do what you need.

    db
    Darryl suggestion is much better.

  7. #7
    onions is offline Member
    Join Date
    Apr 2012
    Posts
    9
    Rep Power
    0

    Default Re: find index of duplicate words inside string

    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;
        }
    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.

  8. #8
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default 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;
    }
    ....
    onions likes this.

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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.

  10. #10
    onions is offline Member
    Join Date
    Apr 2012
    Posts
    9
    Rep Power
    0

    Default Re: find index of duplicate words inside string

    Quote Originally Posted by shall View Post
    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;
    }
    ....
    This worked perfectly. Thank you!

Similar Threads

  1. Accessing index inside WAR file
    By Rollmops in forum Lucene
    Replies: 0
    Last Post: 07-28-2011, 02:49 PM
  2. Replies: 2
    Last Post: 01-05-2011, 10:30 PM
  3. find index of string in another string
    By Sdannenberg3 in forum New To Java
    Replies: 4
    Last Post: 03-04-2010, 10:14 AM
  4. Find index position of every word in a String
    By pentace in forum New To Java
    Replies: 6
    Last Post: 06-28-2009, 08:26 PM
  5. using loop to find duplicate
    By gwithey in forum New To Java
    Replies: 7
    Last Post: 03-06-2009, 01:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •