Index of word in a String

How can I find the index of a word in a String?
E.g.
String a = "This is a book";
In this the index of word "is" is 5 not 2.
A word is a sequence of char separated from other char sequences by a one of " \t\n\r\f", i.e. the default StringTokenized delimiters.
Regards,
Lokesh

Yes something like this.
This is how I solved it
  private static final String WORD_DELIM = " \t\n\r\f,.;:";
  private int getIndexOfWord(String str, String text) {
    StringTokenizer tokenizer     = new StringTokenizer(str, WORD_DELIM, true);
    String token;
    StringBuffer previousContents = new StringBuffer();
    while(tokenizer.hasMoreTokens()) {
      token = tokenizer.nextToken();
      if(startsWithWord(text, token)) {
        break;
      previousContents.append(token);
    if(previousContents.length() == 0) {
      return -1;
    } else {
      return previousContents.length();
  private boolean startsWithWord(String word, String token) {
    //The token must start with "word"
    if(!token.startsWith(word)) {
      return false;
    //Now rplace all delimiters by blanks
    int delimCount = WORD_DELIM.length();
    char c;
    for(int i = delimCount - 1; i >= 0; i--) {
      c     = WORD_DELIM.charAt(i);
      token = token.replace(c, ' ');
    //Finally get rid of the blanks.
    token = token.trim();
    if(token.equals(word)) {
      return true;
    return false;

Similar Messages

  • PDF indexing of Word.doc Keywords: kind of disappointing

    If we could embed document properties like Keywords in Word, and then convert Word.docs to PDFs , and then index the PDFs using Acrobat Pro, theoretically it would allow for lightning-fast keyword search and review, through a zillion PDFs.
    Except, there are unexpected glitches that are either undocumented... or, if the documentation exists, it's either hard to find or too scantily worded.
    Here's a few things I've observed, using Windows XP, Office 2007 and Acrobat 8 Pro:
    1. Word document properties only transfer over to PDF if you use the Acrobat tab in Word's ribbon to generate the PDF via PDFMaker, which apparently invokes some 'more robust' implementation of Distiller, than occurs if you simply use the print dialog to print to PDF.  THIS IS PROBABLY A BIG SOURCE OF USER CONFUSION THAT DISCOURAGES MANY USERS FROM GOING ANY FURTHER WITH EXPERIMENTING WITH PDF INDEXING OF WORD DOCUMENT PROPERTIES.  IT CAN LEAD YOU TO CONCLUDE THAT NONE OF THE WORD DOCUMENT PROPERTIES EXCEPT FOR TITLE, CAN SURVIVE A PDF CONVERSION.
    2. When you invoke PDFMaker, when the "Save Adobe PDF File As" dialog appears, you must click on the button at the bottom that is labeled "Adobe PDF conversion Options' and verify that the "Convert document information" check box is checked. (This may be able to be set as a permanent user preference somewhere, but I'm not quite sure where.)
    3. The Properties fields in Word that will come over, include Title, Author, Subject, and Keywords.  (The Comments field is ignored, as far as I can tell.)
    4. You can now index the PDFs, and these Properties fields will also be indexed.....well, Sort Of.
    5. "Sort Of", because if you then search for any of the text in your Properties fields, (like for example you search for a word or phrase that you've embedded in their Keywords fields), the advanced search result won't be displayed quite the way 'found hits' normally display in a PDF index search results screen.   You may expect to see the contents of those Keywords fields, show up in the search results in a long list of 'found' file icons with ALL (or a generous selection) of their surrounding Keywords also displayed, and with the specified found keyword highlighted in BOLD.
    But, that's not what happens. What you really get is an icon showing the contents of the Title field (which you didn't search for.) It basically means that Acrobat has found a document with something you searched for, in it....but Acrobat is not going to show it to you as easily as you are accustomed to seeing it. You only have two choices: (1) either hover your mouse over each found file's Title icon, one by one, until its screentip-type popup window appears, showing you all the contents of all four of that document's Properties fields; or (2) click on the icon, display the PDF, go to File Properties, and observe that file's properties dialog box.
    This is disappointing: the fast, easy, contextual lookup advantages you've enjoyed with regular PDF index searches appear to be unavailable when it comes to viewing search results on indexed document properties. I can understand the logic; (why show other keywords surrounding the searched-for keyword? If they're not in a sentence, there's really no contextual relationship, and therefore no reason to show them.)
    However, what if users wanted to store logically related keywords in a deliberate organized pattern..ie,
    Texas, Car, 1999, Ford, Mustang, Green
    Texas, Car, 2000, Ford, Mustang, White
    Texas, Car, 2000, Ford, Mustang, Yellow
    Texas, Car, 2000, Chevrolet, Corvette, Blue
    Ohio, Car, 2006, Honda, Civic, Silver
    ...etc.
    In this context, all keywords are logically related; it could be a big advantage to be able to use PDF Index search to instantly find and view a list of all 5,328 White 2000 Ford Mustangs located in Texas....then pop up their insurance.doc PDFs for further details.
    Allowing the user to set a preference to 'show all stored propery values in the search results' instead of an arbitrary length string of surrounding values, could also be very helpful, so that the full information depicted in the above example could actually be fully displayed, not arbitrarily truncated.
    I guess the only workaround is to forget Word's Document Properties, and just embed keywords within the document itself, such as maybe at the end of the document, maybe colored white (so they can't be easily seen).  Formatting them as hidden text doesn't work; Acrobat ignores hidden text when you convert from Word to PDF.

    I seem to get hard returns in all cases. That is why I had the short answer. I do not remember if saving as a DOC got rid of the hard returns. Of course the simplest way to find out is to try it. I went to another machine and the Save As to a DOC file did not put in the hard returns (cut and paste did). I should note that the PDF was produced from a totally different word processor and was not a WORD native document. That would suggest it is not dependent on the tags that can be included by WORD and PDF Maker.

  • Match Certain word in a String

    Hi all,
    I'm actually doing a project regrading e-learning. And i'm suppose to create a set of questions for each topics using labview. So Is there any way that i can match certain word in the string to make sure that answer is correct? Cause i'm sure that every user that input answer will be different. Thus, I want to pick out main point as an answer. Is there anyway i can do it?? 
    Really appreciate your help!!! 
    Thank you!! 
    Solved!
    Go to Solution.
    Attachments:
    Match Strings.vi ‏8 KB
    Match Strings.vi ‏8 KB

    Here's another option (building on Jeff's code).  Turn on the Conditional Terminal on the FOR loop and change it to "Continue if TRUE".  This way, the loop will exit as soon as a failure is found.  Just pass the result straight out of the loop.  If none fail, then the FOR loop will exit on its own (from the auto-indexing) and a pass is passed out.
    There are only two ways to tell somebody thanks: Kudos and Marked Solutions
    Unofficial Forum Rules and Guidelines
    Attachments:
    Match String.png ‏19 KB

  • To find a word in a string.

    Please, is there a way to find a word in a string?
    For exemple:
    The string contains... "Hello World Java Sun"
    I need to find the word "Java", and I'd like to do not compair char by char... I'd like to do it faster than one by one...
    Is there a way? How should I do? I was thinking to use something like substring... but even it... I need to compair char by char... (or I didn't?)
    Thanks a lot

    go to search engine and learn the String class it will help u in the future.
         indexOf
    public int indexOf(String str)
        Returns the index within this string of the first occurrence of the specified substring. The integer returned is the smallest value k such that:
             this.startsWith(str, k)
        is true.
        Parameters:
            str - any string.
        Returns:
            if the string argument occurs as a substring within this object, then the index of the first character of the first such substring is returned; if it does not occur as a substring, -1 is returned

  • How to obtain the table index in word use LabVIEW Report Generation Toolkit for Microsoft Office

    I created a word templete and it had several tables. When I use the "Word Edit Cell" function in LabVIEW Report Generation Toolkit for Microsoft Office, the function need "table index", and I didn't find any function to get or set the table index in word document. How can I achieve my attention to write value to specified table cell using the "Word Edit Cell" function?
    Thanks for reply!
    YangAfreet

    Hi yangafreet
    You do not need to get the table index for the word edit cell.vi from anywhere. LabVIEW will automatically index all the tables in the document. See the attatched vi for an example.
    Rich
    Attachments:
    Table Edit.vi ‏23 KB

  • Here's how to find the right word in a string

    I needed to find the rightmost word in a string. I didn't find a simple formula in these forums, but I now have one, so I wanted to share it. Hope you find it useful.
    Assuming that the string is in cell A1, the following will return the rightmost word in the string:
    RIGHT(A1,LEN(A1)-FIND("*",SUBSTITUTE(A1," ","*",LEN(A1)-LEN(SUBSTITUTE(A1," ","")))))

    I found the problem. Whatever character was being used in the substitution was parsed out by the forum parser. I replaced it with "œ" (option q on my keyboard).
    =RIGHT(A1,LEN(A1)-FIND("œ",SUBSTITUTE(A1," ","œ",LEN(A1)-LEN(SUBSTITUTE(A1," ","")))))
    Still needs an error check for a single-word "sentence" and to remove the trailing period but it does seem to work. Pretty slick.
    Message was edited by: Badunit
    I see below that the problem was fixed by the OP.
    Message was edited by: Badunit

  • How to read each and every word from a string.

    Hi all,
       I have a string which is having many label numbers. if the string is lv_str, its value is like, 11111111111111##22222222222222##3333333333333.
    I need to move the values alone into internal table. each value should be updated as a single row into one internal table. How to read each and every word of the string and move to an internal table.
    the internal table should be like this.
    11111111111111
    22222222222222
    3333333333333
    Can any one give me a suggestion in this regard.
    POINTS PROMISED.
    Regards,
    Buvana

    Hi,
    If you know the format and length of the data
    Use split at '#' so that you will get the individual values.
    Thean append it to internal table.
    Reward iof helpful.

  • How to search for a particular word in a string?

    How to search for a particular word in a string?
    thanks for your help....

    This works fine.
    public class Foo {
        public static void main(String[] args) {
        String s = "Now is the time for all good...";
        String s2 = "the time";
        System.out.println(s.contains(s2));
        System.out.println(s.contains("for al"));
        System.out.println(s.contains("not here"));
    }output:true
    true
    falseYou must have something else wrong in your code.
    JJ

  • How i can count the number of words in a string?

    hi, i want to know how to count the number of words in a string
    e.g. java is a very powerful computer language.
    i will get 7 words.
    thanks in advance..

    Jverd, this has actually been answered, but due to an
    attack of goldie-itis, all the answers were hosed.
    The OP did get an answer, though.Yeah, I know. I just didn't know if he saw the answer before it went away.

  • How to get the second and third.... words in a string

    Hi all,
    is there any way to get the words in a string seperate columns in a row.
    "xxx yyyy zzzz aaaa bbbb"
    required answer is
    a b c d e
    xxx yyyy zzzz aaaa bbbb
    a b c d e are the column names
    Thanks

    WITH t as (
      SELECT 'xxx yyyy zzzz aaaa bbbb' as t FROM dual
    -- end of sample data.
    SELECT t,
           regexp_substr(t, '[^ ]+', 1, 1) as a,
           regexp_substr(t, '[^ ]+', 1, 2) as b,
           regexp_substr(t, '[^ ]+', 1, 3) as c,
           regexp_substr(t, '[^ ]+', 1, 4) as d,
           regexp_substr(t, '[^ ]+', 1, 5) as e
      FROM t;
    T                       A                       B                       C                       D                       E                    
    xxx yyyy zzzz aaaa bbbb xxx                     yyyy                    zzzz                    aaaa                    bbbb                   

  • Replacing first Character of every word in a string with an Uppercase

    Hi,
    Is it possible to replace all first character of every word in a string with UpperCase?
    $="autocad lite"
    Should look "Autocad Lite" .
    Thanks,
    François

    Hi FRacine,
    Please refer to the script below:
    $givenname="autocad lite"
    $givenname
    $givenname = $givenname.substring(0,1).toupper()+$givenname.substring(1)
    $givenname
    Edit: to change first character in every word, please refer to this script, this may not be the best way, but it can work:
    $givenname="autocad lite"
    $givenname
    $words=$givenname.split(" ")
    $givenname=""
    foreach ($word in $words){
    $givenname+=$word.substring(0,1).toupper()+$word.substring(1)+" "}
    $givenname=$givenname.trim()
    $givenname
    If there is anything else regarding this issue, please feel free to post back.
    Best Regards,
    Anna Wang

  • To get Length of each word in a String.

    Hi,
    I wanted to know, how to calculate the length of each word in a string.Suppose for example if the input is like this:
    i/p:'Tech on the Net'
    o/p:4 2 3 3
    ie Tech-4 on-2 the-3 Net-3
    another Ex:i am a good boy
    o/p:1 2 1 4 3
    Kindly let me know any PL/SQL function or SQL query which would help to achieve this.
    Thanks&Regards
    Zaheer

    sql>select substr('tech on the net',decode(n,0,1,instr('tech on the net',' ',1,n)+1),
                  decode(instr('tech on the net',' ',1,n+1),0,length('tech on the net')-instr('tech on the net',' ',1,n),
                        instr('tech on the net',' ',1,n+1)- decode(n,0,0,instr('tech on the net',' ',1,n))-1)) wrd,
          length(substr('tech on the net',decode(n,0,1,instr('tech on the net',' ',1,n)+1),
                  decode(instr('tech on the net',' ',1,n+1),0,length('tech on the net')-instr('tech on the net',' ',1,n),
                        instr('tech on the net',' ',1,n+1)- decode(n,0,0,instr('tech on the net',' ',1,n))-1))) cnt
    from(select rownum-1 n
         from dual
         connect by level <= length('tech on the net') - length(replace('tech on the net',' ',''))+1)
    WRD CNT
    tech  4 
    on  2 
    the  3 
    net  3                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • How to create index in word document?

    Hello,
    Is it possible to create a index in word document from abap code (ole)?
    Thank you for response..
    Alfonso

    1.Goto the transparent table  KNA1
    2. select the button Indexes (which is next to the techincal settings button)
    3.list of alredy existing indexes are displayed
    4. in the dialog displayed select the icon create.
    5. specify the name for the index to be created ( should start with Z)
    6. screen for specify the index fields will appear, specify the details based on your requirement.
    Note:
    1.creating an index will created a sorted  copy of the DB table with data  with limited fields
    2. Try using already created indexes , only if necessary create new index
    3. Here table KNA1 is used as an example

  • Delete the first word in a string?

    Hi, i have a code in which i can extract the first three words in a string. after it's found the three first words, i would like it to remove the first one, what could i add?
    this is what the code looks like:
    import java.util.regex.*;
    class Test187D {
         public static void main(String[] args) {
              String word1;
              String word2;
              String word3;
              String partDesc = "Hi my name is SandraPandra";
              Pattern pattern = Pattern.compile("^(\\w+)\\s+(\\w+)\\s+(\\w+)");
              Matcher matcher = pattern.matcher(partDesc);
              //Find the first word of the part desc
              if (matcher.find()) {
                   word1 = matcher.group(1);
                   word2 = matcher.group(2);
                   word3 = matcher.group(3);
                   System.out.println (word1 + " " + word2 + " " + word3);
    } Thank you in advance

    Take the length of the first word, plus one for the length of the space, that's how many characters to remove. Take the substring of the original string from the end of the removed part to the end of the whole string. Look up the String methods length(), and substring().

  • I need in more lines of the Index some words in Bold text and some others in Kursiv text. How can i get it? It seems to me that either i can have all the Style in Bold Text or in Kursiv Text :(

    I need in more lines of the Index some words in Bold text and some others in Kursiv text. How can i get it? It seems to me that either I can edit a Style only in Bold Text or in Kursiv Text
    I make you an example to clear what I really need:
    Index
    Introduction
    I. Leonardo's Monnalisa
    II. Leonardo's Battaglia
    Bibliography
    Please HELP HELP HELP

    What version of Pages are you referring to?
    Basically if you are talking about the Table of Contents in Pages and want to have different character styles within paragraphs in the T.O.C. you will have to export the T.O.C. and bring it back in as text and change that.
    Peter

Maybe you are looking for

  • Songs no longer appear?

    songs no longer appear.? the files are there under albums and so on, just not "songs", which doesn't appear on any of the  display prefernces. neither does "name". But both of those columns appear in itunes and the ipod without anything listed. In th

  • Crystal Reports 2008 - Changing Table Locations

    Hello, we have several reports which we use by different customers. The customers use different name spaces (table locations) for the same data. To use the same reports with different table locations, we wrote a piece of software replacing the connec

  • Newline character within double quotes in csv file

    I have an external table defined as follows: CREATE TABLE EXT_MASTER    (     "SOME_CODE" VARCHAR2(255 BYTE),      "FAX_NUMBER" VARCHAR2(255 BYTE),      "WEB_ADDRESS" VARCHAR2(255 BYTE),    ORGANIZATION EXTERNAL     ( TYPE ORACLE_LOADER       DEFAULT

  • Sidebar in 10.4.11

    Can someone explain why aliases that I drag to my sidebar will not disappear when I drag them off. My programs still see them and when I restart they are back. I have always used this as a shortcut to folders that I use weekly. I just update the fold

  • LG 22-inch monitor won't function properly after last Mac OS X Update

    My new LG 22-inch monitor was working great until I received the last Mac OS X Update (10.4.9). I received this update after installing Tiger (better late than never...ha ha) and after I restarted my system, I saw multiples of the same files on my de