Need an efficient String searching algo

Hi !
i'm in desperate need of the algorithm i hope some of you may already have it.
actually i'm developing a chat application and i need to implement the functionality of inserting images on finding special tokens in the text..i'm using JTextPane and i want to insert secified image against tokens that i can insert image at the index where i find that token in the text.
lets assume hotmail and yahoo messenger. in those applications we can send images by appending tokens like " :) " , " :( " , etc, .. does any of you have a snippet of code that does the work for me.
i hope u will help me , i need it urgently

try this : the strange word if the > sign.
import java.awt.*;
import java.net.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.util.*;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.*;
public class Fast extends JFrame
     JTextPane   pan = new JTextPane();
     JScrollPane sp  = new JScrollPane(pan);
     Document    doc = pan.getDocument();
     Vector      vs  = new Vector();
     Vector      vi  = new Vector();
public Fast() 
     addWindowListener(new WindowAdapter()
    {     public void windowClosing(WindowEvent ev)
          {     dispose();
               System.exit(0);}});
     setBounds(10,10,757,440);
     getContentPane().setLayout(null);
     sp.setBounds(10,10,660,400);
     getContentPane().add(sp);
     icons();
     buttons();
     setVisible(true);
private void icons()
     ImageIcon   i1  = new ImageIcon("g1.gif");
     ImageIcon   i2  = new ImageIcon("g2.gif");
     vs.add(":)");
     for (int i=0; i < 40; i++) vs.add("xx");
     vs.add("-)");
     vi.add(i1);
     for (int i=0; i < 40; i++) vi.add("yy");
     vi.add(i2);     
private void buttons()
     JButton j1 = new JButton("big");
     j1.setBounds(676,20,66,24);
     getContentPane().add(j1);
     j1.addActionListener(new ActionListener()
        {     public void actionPerformed(ActionEvent be) 
               add_big(); 
private void add_big()
     int ts = 0;
     int c;    
     pan = new JTextPane();      
     doc = pan.getDocument();
     SimpleAttributeSet sa = new SimpleAttributeSet();
     for (int i=100; i < 200; i++)
          try
               String s = "word number "+i+":) and one more picture -), ";
               doc.insertString(doc.getLength(),s,sa);
          catch(BadLocationException e){}     
     long now = System.currentTimeMillis();
     String       s   = pan.getText();
     StringBuffer sb = new StringBuffer(s);
     for (int i=s.length()-2; i > -1; i--)
          if (vs.contains(s.substring(i,i+2))) sb.delete(i,i+2);
     pan.setText(sb.toString());
     for (int i=0; i < s.length()-2; i++)
          c = vs.indexOf(s.substring(i,i+2));
          if (c > -1)
               pan.setCaretPosition(i-ts);
               ts++;
               pan.insertIcon((ImageIcon)vi.get(c));
     System.out.println("Took: " + (System.currentTimeMillis() - now));
     L.p(""+s.length()+"       "+sb.length());
     sp.setViewportView(pan);     
     sp.invalidate();
public class myPan extends JPanel
public myPan()
public void paint(Graphics g)
public static void main (String[] args) 
     new Fast();

Similar Messages

  • Efficient string search into files

    Hello, i wrote an application that search a string into several files. At the moment i perform the search looping each files, opening them with new BufferedReader(new FileReader), using .readLine() method to get each line and .indexOf >= 0 to check if the line contains the string to search.
    Is there a more efficient way to do it? Maybe using Scanner class or what else? HasMap? thanks in advance for the replies

    To search for a string in a file you do not have much choice but to read the file and search the characters for the given string.
    If you only need to find the first occurrence of the string then obviously you want to stop reading the file when you have found it (unless you need to do something with the rest of the file). Using a large buffer size might help the reading efficiency of the process. You might check out java.nio for some more efficient ways of reading streams in general.
    I don't see how a HashMap would help with this process unless you need do something along the lines of counting occurrences of multiple strings.
    If all you want to do is search for a string then I am not sure what you would gain using the Scanner class.

  • Multiple string search

    I'm having problems trying to search a file for various strings. Once the first search string has been found,
    I need to search for another string (within the first search) , and then process the input lines. The code below works for the first search, but my efforts to incorpate a second search string fail. Can anyone help please?
    Rob
    StringBuffer buf = new StringBuffer();
    buf.append("//");
    buf.append(jobStep);
    search = buf.toString();
    buf.setLength(0);
    String infile = "//INFILE";
    String line = null;
    BufferedReader reader = new BufferedReader(new InputStreamReader(inS));
    int pos = -1;
    int pos1 = -1;
    int count = 0;
    String inputLine;
    while ((inputLine = reader.readLine()) != null) {
    for (; true; ) {
    pos = inputLine.indexOf(search, pos + 1);
    if (pos >= 0) {
    out.println( "step = " + inputLine + "<br>" ); *** code works to here , now I've found jobStep, need to find string 'infile'
    count++;
    else {
    break;

    You could put your search strings in a collection, such as an array or Vector, and then get each search string, as needed.

  • Problem in String Search using OKAPI in 9i Lite

    We are creating an Symbian EPOC application using Oracle 9iLite in stand-alone mode. We have managed to create a C++ program that creates an Oracle database on the Nokia Communicator 9210.
    The problem is that a string search returns either null or all of the searched records, ie. it does not work.
    The code below should do the string search and return the strings that match the given keyword. Any idea why this is not working? We are using OKAPI.
    TempCond[0].Operator = OK_GT;
    TempCond[0].AttrPos = KEYWORDREFERENCE_INDEX_FOR_KEYWORD;
    TempCond[0].AttrVal.BufPtr = (unsigned char*) szGivenKeyword;
    TempCond[0].AttrVal.BufSize = OK_NEW_ARRAY | strlen szGivenKeyword);
    TempCond[0].AttrVal.Indicator=1;
    TempCond[0].Collate=0;
    TempCond[0].Connective=OK_AND;
    // Iterate through the entire group, extracting the key data
    //(whatever the key may be) and the object reference, and
    // append to the end of the array.
    e = okCreateIterator( iEnv,
    iEbmgKeywordReferenceGroupRef,
    iEbmgKeywordReferenceClassRef,
    FALSE,
    1,
    TempCond,
    20,
    0,
    NULL,
    &iter
    Thanks for any help you may provide.

    I am sorry, this is not an answer to your question but a little help from you is well appeciated.
    I have just started exploring oracle 9i Lite to develop apps. on Palm and WinCE using OKAPI but the documentation does not seem enough. Is there some book or sample programs available that demonstrate OKAPI to develop form based apps ? CodeWarrior and Satellite Forms are not good when someone wants to try writing demp app.
    Thanks,
    Ashish

  • What kind of mac would I need for Nexflix, basic search and email? Also, sounds silly, but can I connect an HDMI from Mac to tv?

    What kind of mac would I need for Nexflix, basic search and email? Also, sounds silly, but can I connect an HDMI from Mac to tv?

    Just an FYI.  The Macs named PowerMac date from early 2006 and before.  Use this tip to discover where to post once you get your Mac:
    Site map of Communities and Categories
    The cheapest Mac available new is a Mac Mini.   It is no bigger than many an external hard drive.  But the screen and keyboard and mouse must be purchased separately.  They support USB and Bluetooth keyboards and mice, and pretty much any DVI, HDMI, or displayport display.   Although the iMac is certainly an attractive option if all you want to deal with is deciding on a printer.   I still recommend getting an Apple USB keyboard for the occasional times you may need to troubleshoot startup issues.  Normally not a problem, but as it gets older some issues can be fixed at startup or diagnosed at startup.

  • Accessing a JSTL variable in a JSP Scriptlet (need to replace string )

    I have
    <c:set var="myVar" value="..." />
    I need to replace single quotes and double quotes to be escaped because I pass them into javascript functions and set them as ID for div sections
    ... onclick ="func('${myVar}')" ..
    <div id="${myVar}">
    but if the string contains ' single quotes it messes up the javascript or double quotes messes up the ID portion of the HTML tag
    I know there is the JSTL function fn but I can't figure out how to do it properly in JSTL
    <c:set var="myVar"
    value="${fn:replace(myVar, "'", "\"")"/>
    But that gets tricky since the value portion is enclosed in quotes
    So I was thinking of using a Scriptlet part instead.

A: Accessing a JSTL variable in a JSP Scriptlet (need to replace string )

escaping quotes within quotes within quotes.... ARGH!
Recipe for a headache if ever there was one.
However you must be strong and resist the temptations of the dark side (Scriptlet code)
My suggestion for cleaning this up - write your own static function for escaping javascript strings.
public static String escapeJavascriptString(String s){
  return .......
}Then define the function in a tld:
<function>
    <description>
      Escapes a string for javascript purposes
    </description>
    <name>escapeJavascript</name>
    <function-class>com.mypackage.ELFunctions</function-class>
    <function-signature>java.lang.String escapeJavascript(java.lang.String)</function-signature>
    <example>
      <c:out value="${myfunc:escapeJavascript(name)}">
    </example>
  </function>Cheers,
evnafets

escaping quotes within quotes within quotes.... ARGH!
Recipe for a headache if ever there was one.
However you must be strong and resist the temptations of the dark side (Scriptlet code)
My suggestion for cleaning this up - write your own static function for escaping javascript strings.
public static String escapeJavascriptString(String s){
  return .......
}Then define the function in a tld:
<function>
    <description>
      Escapes a string for javascript purposes
    </description>
    <name>escapeJavascript</name>
    <function-class>com.mypackage.ELFunctions</function-class>
    <function-signature>java.lang.String escapeJavascript(java.lang.String)</function-signature>
    <example>
      <c:out value="${myfunc:escapeJavascript(name)}">
    </example>
  </function>Cheers,
evnafets

  • HT201365 How would i use someone else's iPhone to find my iPhone? what App to I need to run to search for my iPhone

    How would i use someone else's iPhone to find my iPhone? what App to I need to run to search for my iPhone

    Settings>general>resets>erase all content and settings.
    That will put it back to its out of the box state. Set it up with your own apple Id.

  • Need advice on web search engine

    I may need to install a search engine for a asp.x web site I am working on. I know nothing about ASP.NET and the these are secure web pages. Also, this is for just a portion of the web site.I have no experience with search engines.
    First question: Can search engines limit their search to just a section of a web site?
    Second question: What would recommend for my circumstances?
    Thanks!

    Answer to your first question: No, I don't want to provide a search engine to the entire site. There is a section you go in with several divisions. One division is the directory of services. The way that it's set up now is they have about 9 PDF files that are link to 9 headings. You click on a heading and it opens the PDF for that topic. The problem is that it takes a long time for the PDF to open and then the person may have to search it for the information they are looking for.
    I think you may have answered my question already. If robot files are required to limit a search, then a search engine may not be the practical choice in my case.
    By the way, to answer your last question, I am not coding in ASP.NET. I have made it clear to these people that I don't know a line of it. But I also think the solution to the problem is to convert the Directory to web pages, set up links of topics and maybe a JavaScript jump menu for linking to other bookmarked information. That way the searched is limited to only certain topics we decide beforehand but also limited to the directory.
    What do you think of this solution? Do you foresee any problems?
    Michael

  • String search

    i hI have a string "1234, abcdef, bbbbb"
    How do one get/search for the "abcdef" or "bbbb" out of the string using substring?
    If anyone has source code or references (for similar code) searching for "abcdef�(normally text enter in a text field that will be used to search through a string) It would be appreciated

    // This might help!
    String search = "test";
    Pattern pattern = Pattern.compile(".*" +search +".*");
    boolean test = pattern.matcher(testString).matches();

  • Need help doing text search of Blob

    I presently have a simple single table candidate tracking application that create records of candidate information and you attach their resume in a word format into a blob column named RESUME within the table.
    I need to be able to do a full text boolean search of the attached documents, since we're going to use this to allow us to search an internal database for people with particular skill sets.
    I'm not a DBA or a developer, I just started working with Oracle Application Express and need a simple way of creating this search feature.
    I also need to add the search feature into a region for users to input search terms.
    Any help would be greatly appreciated.
    Edited by: user10608055 on Nov 25, 2008 11:57 AM

    try
    http://tahiti.oracle.com/
    in the list of books, look at 'TEX' for Oracle text.
    Interesting you should bring this up. I'm slighly involved with US Transition team.
    Many resumes are coming in.
    Tim

  • Need an efficient solution using Informatica Power center

    Need an efficient solution using Informatica Power center-
    I have a requirement-
    1-I have a database which gets refreshed everyday.
    2-I have a workflow which gets started only if the database gets refreshed.
    So I have to design a workflow-- which will do the following things-
    a- Checks if the database has been refreshed today or not(Not hard coding...as this workflow will run everyday to check that)
    b- If the database has been refreshed then only start the workflow wf_XYZ
    ( SUGGESTION-may be after the session/task finds out that the db has been refreshed it can create a trigger file which will eventually start the next workflow) else can wait to get the db refreshed.

    Just implement the change capture process to load new data.
    That would take care of it, and you can relax.
    If helps mark
    ~ http://cool-bi.com

  • Help me in writing ajax string search from database

    {noformat}*hi everybody,
    please send me a ajax with jsp application
    suppose i enter a word in text area ajax will populate/suggest all string from database ,who started
    from that entering character(s).like a google string search.
    please send full source code
    *{noformat}

    shadab_think_globally wrote:
    {noformat}*hi everybody,
    please send me a ajax with jsp application
    suppose i enter a word in text area ajax will populate/suggest all string from database ,who started
    from that entering character(s).like a google string search.
    please send full source code
    *{noformat}how about you do it yourself?

  • Need help in String operations

    HI all,
    I need help in String operations.I am getting file path of an image as
    c:\test\img\abc.gif"
    I need to convert it in to c:/test/img/abc.gif".
    Can any one suggest the solution for this.
    Thanks,
    Durga.

    [email protected] wrote:
    I used String replace method but I am not able to do it because "/" is a special character."/" is not a special character, "\" is a special character, which needs to be escaped by "\" itself.

  • Faster string search options

    Hi
    I am an intern at a mobile technology firm assigned the job of enhancing the search performance of a tool, which is coded completely in java. This tool, along with other features basically searches for the substring in a string array by using the string.indexof() method which is very slow, since i am dealing with vast amount of data..
    Now the last two weeks all I did was try and understand the massive code. These are the alternatives I came up with. I could do with suggestions about the feasibilities of these ideas.. or any new suggestions are welcome..
    1. are there any faster ways instead of string.indexof() that i can use to do my string search?
    2. How about replacing this indexof() method with a faster search algorithm? something like boyer-moore for example??
    3. will using a the .nio package instead of .io package work???
    4. how about completely scrapping this string search and taking in the data from input stream as integer format.. and then doing the comparisons??
    thanks
    mahima

    yumjay wrote:
    1. are there any faster ways instead of string.indexof() that i can use to do my string search?You seem to have found one in section 2.
    2. How about replacing this indexof() method with a faster search algorithm? something like boyer-moore for example?? Well obviously you can't override or replace String's indexOf, but you could certainly change the code to use a different algorithm. What's stopping you?
    3. will using a the .nio package instead of .io package work??? Not sure, there may be virtue in some of the memory mapping additions, but I don't really know your requirements or situation.
    4. how about completely scrapping this string search and taking in the data from input stream as integer format.. and then doing the comparisons?? The string search is essentially doing integer comparisons on data 16 bits at a time (that's the width of a char). So first, remember that the int returned by InputStream.read() isn't really an int but a byte stuffed into an int (so they can return -1 on EOS). That said, you might experience a performance gain if you put the data into ints or longs (depending on your computer architecture) and then use something like boyer-moore, because your computer can likely do comparisons of two bytes, chars, shorts, ints (and perhaps even longs on a 64-bit architecture) in the exact same amount of time. They're probably even the same machine instruction. You also wouldn't therefore have to decode the input data into a string either. But you'd have to encode your search string into integers. So yeah you could potentially squeeze a bit more speed, not likely more than 50% though.

  • Exact string search

    i was wondering if there was a way to do an exact string search in a document.
    If i have a document that has punctuation i want to be able to find the document only if i query whith an exact string match, including stopwords and punctuations.
    if the query is
    select *
    from documents1
    where contains(OBJECT_VALUE,'{hello, world}') > 0
    I want only to return the documents that have exactly "hello, world" whith the comma included.
    If i add the comma to printjoins, it would work, But when i query for only "hello" it wont match "hello, world"
    basicaly im looking for something that does a normal search but then check if the exact string is in the match area.

    Try this
    select substr(&P, (INSTR(&P, CHR('32'), 1))+1, (INSTR(&P, CHR('32'), 1)))
    from dual
    &p is a variable
    I am sure this will help you in any of case
    chr(32) will return space, If you have any other thing except space then you just replace 32 with that ascii code. Such as for
    1(we)2 then query will be
    select substr(&P, (INSTR(&P, CHR('40'), 1))+1, (INSTR(&P, CHR('41'), 1)))
    from dual
    Try it and tell is it working fine

  • Maybe you are looking for