Find start/end of word contained in text.

I feel really silly asking this, but my brain doesn't seem to want to work at the moment and I can't find my old semester 1 examples.
While I can easily solve this in C++ trying to do it in Java is a bit of a mystery. size_t = found, str.find() string::npos -> etc
Basically I'm trying to implement a default highlighter. The words I need to search for AFAIK only appear once each in the Document or not at all.
SQL queries, SELECT, FROM, WHERE, etc.
I'm going off an example I found here: [http://www.java2s.com/Code/Java/Swing-JFC/Anexampleofhighlightingmultiplediscontiguousregionsofatextcomponent.htm]
Which searches for vowels within a document and highlights. So i figured it would be an easy switch to get it to do what I wanted, but I've been plugging away at it for a few hours now and can't solve it.
Would be a lot easier if String.contains(CharSequence cs) gave something back more useful than a boolean... integer would be nice :(
Anyway sample is below, to make it simple I have a simple query in the text pane, and only search for the 3 words, select from where.
Can anyone tell me how I would go about telling the addHighlight() method of the word found?
import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
public class MultiHighlight implements ActionListener
     private JTextComponent comp;
     private String[] wordsToHighlight;
     public MultiHighlight(JTextComponent c, String[] words)
          comp = c;
          wordsToHighlight = words;
     public void actionPerformed(ActionEvent e)
          // highlight
          Highlighter h = comp.getHighlighter();
          h.removeAllHighlights();
          String text = comp.getText().toUpperCase();
          for (int i=0;i<wordsToHighlight.length;i++)
               if (text.contains(wordsToHighlight))
                    // find start, find end... ?
                    h.addHighlight(start, end, DefaultHighlighter.DefaultPainter)
     public static void main(String args[])
          JFrame frame = new JFrame("MultiHighlight");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          JTextArea area = new JTextArea(5, 20);
          area.setText("Select *\nFrom stuff s, more m\nWhere s.otherstuff = m.otherstuff.");
          frame.getContentPane().add(new JScrollPane(area), BorderLayout.CENTER);
          JButton b = new JButton("Highlight All SQL words");
          String[] wordsToLookFor = { "SELECT", "FROM", "WHERE" };
          b.addActionListener(new MultiHighlight(area, wordsToLookFor));
          frame.getContentPane().add(b, BorderLayout.SOUTH);
          frame.pack();
          frame.setVisible(true);

int start = text.indexOf(wordsToHighlight);
                    int end = start + wordsToHighlight[i].length();
                    try {
                         h.addHighlight(start, end, DefaultHighlighter.DefaultPainter);
                    } catch (BadLocationException e1) {
                         e1.printStackTrace();

Similar Messages

  • How can I find an ID file that contains a text...?

    I've been working with InDesign for a long time. So, I have a lot of .indd files, from version 1 up to CS3.
    I need to find a file(s) that contains a specific text. E.g. the name of a person, that I'm sure is somewhere in my thousands of ID files.
    It's impossible to open all the files until I locate that text.
    In Windows Explorer, there's a way to search a word or phrase in a file, but it only returns doc, txt, rft, and other text files, even pdf. But not indd.
    Can somebody help me, please?
    Thanks,
    Hirao

    Have you tried changing the extension on your .indd files to .txt? It's
    a binary format, but there's a lot of plain text in there. The name
    you're looking for, or part of it, is probably in there somewhere.
    Search for the shortest possible text string. Some words are not continuous.
    Kenneth Benson
    Pegasus Type, Inc.
    www.pegtype.com

  • Replace Filename text by start/end position?

    I could do this in php, but I know nothing of applescript. The main function I need is PHP's substr_replace, to rename files in a passed array, by start/end string positions, with passed text.
    Basically I'm looking for an applescript function to add to an Automator workflow, that will replace filenames with text, by character position. The applescript function would have 4 parameters, (files:Array, startPosition:int, endPosition:int, replaceWith:String). The first is an array of the files passed by Automator's "Get Selected Files" func, then the starting and ending positions for what is to be replaced, finally followed by the string to replace with.
    So something like,
    function renameFilenamesByPosition(files, startPos, endPos, replaceWith)
    for(i=0;i<count(files);i++)
    file = files;
    oldName = basename(file);
    newName = substr_replace(oldName, replaceWith, startPos, endPos);
    rename(oldFilename, newName);
    Any help in coming up with an applescript that I can do this, and be inserted into an Automator workflow, is MUCH APPRECIATED!! I often have files with number tags, that I'd like to have removed.

    I had a substring handler that converted fairly readily, but I was looking for something more general-purpose to put into an Automator action. Using an action is good for making the interface not quite so ugly, but there is a bit more to do to make sure it is robust enough to take whatever the other actions can throw at it. I finally decided on an action that trimmed an adjustable number of characters from the beginning or end, with an option to add a text variable - something that the existing rename doesn't do.
    As for my replacement handler, the main difference is that it uses the normal AppleScript index ranges, and the index items can also be text strings. There is also a bit more code to take care of stuff like negative or swapped indexes (yours definitely doesn't like that). It does work differently than the PHP function, which is probably not what you were looking for.
    <pre style="
    font-family: Monaco, 'Courier New', Courier, monospace;
    font-size: 10px;
    margin: 0px;
    padding: 5px;
    border: 1px solid #000000;
    width: 720px; height: 340px;
    color: #000000;
    background-color: #FFEE80;
    overflow: auto;"
    title="this text can be pasted into the Script Editor">
    on run -- examples
    set TheText to "00012-myfile.jpeg"
    ReplaceTheText of TheText between 1 thru 6 by ""
    -- ReplaceTheText of TheText between "000" thru "-" by ""
    -- ReplaceTheText of TheText between 1 thru "-" by "xxxx"
    display dialog "\"" & the result & "\""
    end run
    to ReplaceTheText of SomeText between StartItem thru EndItem by ReplacingText
    replaces the text between the specified items with ReplacingText
    item searches are from left to right (beginning to end)
    if an item is not found, the replacement is from the beginning or end
    parameters - SomeText [text]: the text to modify
    StartItem [mixed]: the starting item or index (negative number is from the end)
    EndItem [mixed]: the ending item or index (negative number is from the end)
    ReplacingText [Text]: the replacement text
    returns [text]: the modified text, or the original text if error (index out of range, etc)
    set SomeText to SomeText as text
    set TextCount to (count SomeText)
    if class of StartItem is in {integer, real} then
    if StartItem is less than 0 then set StartItem to ((TextCount + 1) + StartItem) -- make positive index
    if (StartItem is greater than TextCount) or (StartItem is less than 1) then return SomeText -- out of range
    else
    set StartItem to offset of (StartItem as text) in SomeText
    if result is 0 then set StartItem to 1 -- the beginning
    end if
    if class of EndItem is in {integer, real} then
    if EndItem is less than 0 then set EndItem to ((TextCount + 1) + EndItem) -- make positive index
    if (EndItem is greater than TextCount) or (EndItem is less than 1) then return SomeText -- out of range
    else
    get (offset of (EndItem as text) in (text StartItem thru -1 of SomeText))
    if result is 0 then
    set EndItem to TextCount -- the end
    else
    set EndItem to StartItem + result + (count EndItem) - 2
    end if
    end if
    if StartItem is greater than EndItem then set {StartItem, EndItem} to {EndItem, StartItem} -- swap
    if StartItem is not 1 then
    set StartItem to text 1 thru (StartItem - 1) of SomeText
    else
    set StartItem to ""
    end if
    if EndItem is not TextCount then
    set EndItem to text (EndItem + 1) thru TextCount of SomeText
    else
    set EndItem to ""
    end if
    return StartItem & ReplacingText & EndItem
    end ReplaceTheText
    </pre>

  • Pages IPad: how to  'Find' occurrence of a word in a range starting from somewhere in the middle of document to the end. It seems that 'Find' feature always defaults to finding the word from the start of the document. Thanks

    Pages IPad: how to  'Find' occurrence of a word in a range starting from somewhere in the middle of document to the end. It seems that 'Find' feature always defaults to finding the word from the start of the document. Thanks

    Pages IPad: how to  'Find' occurrence of a word in a range starting from somewhere in the middle of document to the end. It seems that 'Find' feature always defaults to finding the word from the start of the document. Thanks

  • [AS] Find a string of words in text

    Hello - hopefully someone can help as I'm now stuck on this. I want to be able to find a specific string of words within a text frame, though I want to be able to do this without using the Find Text Preferences approach.
    I can get it to work if i am just finding one word, but if there is more than one word it doesn't work.
    For example this works (given a text frame with text in that contains the word Example):
    tell application "Adobe Indesign CS5"
    tell active document
    set mybox to selected
      tell mybox
       set myfind to object reference of every word whose contents = "Example"
      end tell
    end tell
    end tell
    However the same does not work (searching for "The Example", which is two words)
    tell application "Adobe Indesign CS5"
    tell active document
    set mybox to selected
      tell mybox
       set myfind to object reference of every word whose contents = "The Example"
      end tell
    end tell
    end tell
    What i can't figure out is the correct phrasing for finding a string of words, i guess i should not be looking for the object reference of WORD, but nothing else i've tried seems to work. Many thanks for any help

    I think what John has said is correct… I only had a quick look in the dictionary terms. Without find/change the closest you may get is line or paragraph after that you would need to manipulate the line/para contents further… Word by definition I would think can't contain space but is encapsulated in space bar first/last…
    tell application "Adobe InDesign CS5"
    tell active document
    tell story 1
    set myfind to object reference of every word whose contents = "Example"
    end tell
    show text first item of myfind
    end tell
    end tell
    This would show the line you can then break down further…
    tell application "Adobe InDesign CS5"
    tell active document
    tell story 1
    set myfind to object reference of every line whose contents contains "For Example"
    end tell
    show text first item of myfind
    end tell
    end tell

  • Find a word in long text

    Hi,
    I need to find all the routing cards that don't contain a certain word in their long text. any suggestions???
    Eti

    Hi
    Please check in following links.....you will find some solution.
    How to search for a word in a text editor
    Search in long text
    Word Search
    search word for '.'.
    Regards
    Sujit

  • Table which contains Start & End Destination details

    Hi All,
    I was trying to find out the table which contains the start and end destination details of a mileage claim in Travel Management.
    Can any one let me know the table names.
    Thanks and Regards
    Sri

    Hi Srinivas,
    Thanks for the reply!
    but what iam trying to find out is during mileage claim we usually enter the start destination and end destination . Now i wanted to know where will these details go and get stored as i need to amend some changes on the PDF form and i need to fetch these details to get them displayed.
    Thanks and Regards
    Sri

  • Find words within a text file

    Hey all.
    I am playing around with the idea of finding a line of text within a text file, by using scanner and some next methods.
    The way I am trying to get it to work is that one enters a string and the program then finds all the lines of text containing that
    user-entered string and then prints them.
    The text file in question contains names of University papers and their room numbers, quantity of students etc.
    Example:
    IBUS212     EALT006     1am     72     AL     LI     
    BMSC241     MCLT102     2     pm     8     AL     COOREY     
    My problem annoyingly enough seems to be that I can't think how one could compare the string entered to the lines of text being
    scanned in the text file.
    My latest go involved what you see in the code, scanning the examdata.txt file for a user-entered course number, using course.next();
    by going with the example above it would be IBUS212. The task was to then to find all of the lines containing that number and print them out using
    println (what I have tried with id and line) as well as the rest of that line eg: EALT006     1am     72     AL     LI .
       public void printCourse()
        try
            String details, input, id, line;
            int count;
            Scanner user = new Scanner(System.in);
            System.out.println();
            System.out.println();
            System.out.println("Please enter your course ID: ");
            input = user.nextLine();
            Scanner course = new Scanner(new File("examdata.txt"));
            course.next();
            course.nextLine();
            id = course.next();
            line = course.nextLine();
            if(input.equals(id))
                System.out.println("Your course times are: "  + id + "and" + line);
            else
              System.out.println("Your course does not exist."); 
            catch(IOException e)
                System.out.print("File failure");
      }Any advice/help/troubleshooting would be greatly appreciated.
    Edited by: AUAN on Aug 13, 2009 9:43 AM
    Edited by: AUAN on Aug 13, 2009 9:44 AM
    Edited by: AUAN on Aug 13, 2009 9:49 AM

    You'll want [to loop while|http://java.sun.com/docs/books/tutorial/java/nutsandbolts/while.html] the course Scanner has a next line and then print the line if it contains the entered text.
    For useful methods you can check the Javadoc of String and Scanner (use your browser search on keywords like 'next' or 'contains' to find them).

  • Acrobat Pro 8.1.2 crashes when displaying pdf made from Word doc containing url text

    Hello,
    I and a few others in my workplace have a similar issue.
    Acrobat crashes when they scroll through and view a pdf file that was created from Word.  The Word document contains url text in its header to the effect of "visit us at www.blah.com ." They're running Acrobat Pro 8.1.2 and Office 2K Pro SR-1 on XP Pro SP2.
    Even though the link is not an active link, (i.e. clicking on it does not open the web page), if I convert a document containing this header to PDF, Acrobat will not allow me to manipulate the file and will crash. When I take the same document and remove the web address, then convert from Word to PDF, I experience no problems with Acrobat (note that this is the identical file, but with the web address removed).This does not explain why Acrobat will work for awhile even with the web address in the header, then stop working. But it does appear to fix my problem with creating a PDF from Word, then manipulating it further.
    Does anyone know how to allow for this text to exist in the header and still have a stable pdf file/acrobat behavior?
    The pdf is attached.
    Thanks....

    To sum up, I've found that if a url beginning with www is in the Word document and then created to pdf Adobe will crash when viewing that pdf.  If it is deleted from the Word document before creating the pdf, Acrobat will not crash when viewing that pdf.

  • How do you use the "Item Containing Start/End Date" in a Calendar?

    Hi
    I want to do a calendar where the days shown will be conditional to a certain interval. Now I though I could achieve that using the Item Containing Start Date of a calendar. But it doesn't seem to work. In the help it says :
    Enter an item in the application which holds the start date of the calendar. The format of the date in this item must be YYYYMMDD.
    This is what I did here
    http://apex.oracle.com/pls/otn/f?p=34530:1::::::
    I have 3 fields. 2 for the Start Date and End Date and 1 for the "Date Item" parameter. Only the latter has an effect on the calendar display, the calendar only displays the month of the "Date Item" (in monthly mode).
    Wether or not I check "Begin at Start of Interval", it doesn't affect the way those fields are affecting the Calendar.
    So, how do we use that "Item Containing Start/End Date" parameter?

    Hello,
    does yout Script work when you begin with ?:
    Start-Process powershell -Verb runAs
    Best regards,
    Stefan
    German Orchestrator Portal ,
    My blog in English

  • Why does text I copy from a .pdf to a Word document, not copy correctly words containing the combinations 'tt' or 'ti'?

    When I copy text from a .pdf to a Word document, all words containing the combination 'tt' or 'ti' come up as blank boxes,or question marks.  How can I correct this?

    To comment a bit further, several letter combinations like tt and ti as well as many combinations with f are given as a single character in some languages and apparently you do not have that special character on your system.

  • Find start and end execution time of a sql statement?

    I am have databases with 10.2.0.3 and 9.2.0.8 on HP UNIX 11i and Windows 200x.
    I am not in a position to turn on sql tracing in production environment. Yet, I want to find when a sql statement started executing and when it ended. When I look at v$sql, it has information such FIRST_LOAD_TIME, LAST_LOAD_TIME etc. No where it has information last time statement began execution and when it ended execution.. It shows no of executions, elapsed time etc, but they are cumulative. Is there a way to find individual times (time information each time a sql statement was executed. – its start time, its end time ….)? If I were to write my own program how will I do it?
    Along the same line, when an AWR snapshot is shown, does it only include statements executed during that snapshot or it can have statements from the past if they have not been flushed from shared memory. If it only has statements executed in the snapshot period, how does it know when statement began execution?

    Hi,
    For oracle 10g you can use below query to find start and end time, you can see data for last seven days.
    select min(to_char(a.sample_time,'DD-MON-YY HH24:MI:SS')) "Start time", max(to_char(a.sample_time,'DD-MON-YY HH24:MI:SS')) "End Time", b.sql_text
    from dba_HIST_ACTIVE_SESS_HISTORY a,DBA_HIST_SQLTEXT b where
    a.sql_id=b.sql_id
    order by 1;
    Regards
    Jafar

  • Terminal - how to jump type to the start/end of a word or line?

    How do I jump to the start or end of a word or line in Terminal?
    In other OS X apps, Cmd or Opt + left/right arrows move to the start/end of a line or word respectively.

    Try these (note: the first two require the shift key, at least for me):
    ctrl-A beginning of line
    ctrl-E end of line
    (note: the next two require that you have the Terminal preferences, "settings" , "keyboard" opened, and the checkbox for "Use option as meta key" checked)
    opt-f forward word
    opt-b backward word
    ctrl-u clears the line before the cursor position. If you are at the end of the line, clears the entire line.
    ctrl-f forward character
    ctrl-b backward character
    ctrl-d delete character
    ctrl-l clear screen
    pageup page up in buffer
    pagedwn page down in buffer
    More here:
    http://osxdaily.com/2006/12/19/command-line-keyboard-shortcuts-for-mac-os-x/

  • TS1702 In pages, when I get to the end of a line, the text refuses to start again in the left, it continues on the right and goes down the page, anyone know a solution?

    In pages, when I get to the end of a line, the text refuses to start again in the left, it continues on the right and goes down the page, anyone know a solution?

    Does this occur in all of your Pages documents? Are you certain that you haven't formatted the type to run flush right? (Align right I mean - sorry old typographer lingo). Look at the ruler at the top and check the Align setting.
    Have you tried quitting Pages completely and the restart the iPad?
    Go to the home screen first by tapping the home button. Double tap the home button and the task bar will appear with all of your recent/open apps displayed at the bottom. Tap and hold down on any app icon until it begins to wiggle. Tap the minus sign in the upper left corner of the app that you want to close. Restart the iPad.
    Restart the iPad by holding down on the sleep button until the red slider appears and then slide to shut off. To power up hold the sleep button until the Apple logo appears and let go of the button.

  • When I start firefox browser an erroe message appear that contain this text "the procedure entry point_except_handler4_common could not be located in the dynamic link library msvcrt.dll" I am using windows XP

    when I start firefox browser an erroe message appear that contain this text "the procedure entry point_except_handler4_common could not be located in the dynamic link library msvcrt.dll" I am using windows XP in English
    == This happened ==
    Every time Firefox opened
    == start mozila firefox

    This issue can be caused by a problem with the file c:\windows\system32\dwmapi.dll
    The file dwmapi.dll is a Vista file and should not be present in Windows XP.
    See also [tiki-view_forum_thread.php?forumId=1&comments_parentId=417674]

Maybe you are looking for