Results 1 to 5 of 5

Thread: search text

  1. #1
    lemontree45 is offline Member
    Join Date
    Oct 2010
    Posts
    25
    Rep Power
    0

    Default search text

    I have a file and would want to search for all the words starting with say "ABC" and display them.

    For eg:

    askjaas
    123_ABC1
    alkdjsad
    238_ABC222
    akjsdlksada
    asdkjlhfdsa
    233_ABC34323
    kjsdfhlsd
    .........

    I tried using ABC* but it didn't work out. Also, I want only the words ABC1,ABC222,ABC34323 to be printed out. could someone please help me solve it? I have the same code where I could search for a normal word and points out its index number.


    Java Code:
    public class NewClass {
        
    public static void main(String[] args){
    
    //Search String
    String searchText = "ABC";
    
    //File to search (in same directory as .class file)
    String fileName = "C:\\chinese1.txt";
    
    //StringBuilder allows to create a string by concatinating
    //multiple strings efficiently.
    StringBuilder sb =
    new StringBuilder();
    
    try {
    //Create the buffered input stream, which reads
    //from a file input stream
    
    BufferedInputStream bIn =
    new BufferedInputStream(
    new FileInputStream(fileName));
    
    //Holds the position of the last byte we have read
    int pos = 0;
    
    //Holds #of available bytes in our stream
    //(which is the file)
    int avl = bIn.available();
    
    //Read as long as we have something
    while ( avl != 0 ) {
    
    //Holds the bytes which we read
    byte[] buffer = new byte[avl];
    
    //Read from the file to the buffer
    // starting from <pos>, <avl> bytes.
    bIn.read(buffer, pos, avl);
    
    //Update the last read byte position
    pos += avl;
    
    //Create a new string from byte[] we read
    String strTemp =
    new String(buffer);
    
    //Append the string to the string builder
    sb.append(strTemp);
    
    //Get the next available set of bytes
    avl = bIn.available();
    }
    }
    catch(IOException ex) {
    ex.printStackTrace();
    }
    
    //Get the concatinated string from string builder
    String fileText = sb.toString();
    
    //Displays the index location in the file for a given text.
    // -1 if not found
    System.out.println(
    "Position in file : " +
    fileText.indexOf(searchText));
    }
    }

  2. #2
    lemontree45 is offline Member
    Join Date
    Oct 2010
    Posts
    25
    Rep Power
    0

    Default


  3. #3
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    10

    Default

    Hi. You can use Reg Exp for this aim. you can find more information on official website Oracle.
    Skype: petrarsentev
    http://TrackStudio.com

  4. #4
    R-J
    R-J is offline Member
    Join Date
    Aug 2011
    Posts
    11
    Rep Power
    0

    Default

    You could also use String Tokenizer and look at the first 3 characters of each token. I don't know how this method compares to using regex.

  5. #5
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    10

    Default

    StringTokenizer does work with regexp and it is used for to break a string into tokens. )) It is strange solve for search appropriate text.
    Skype: petrarsentev
    http://TrackStudio.com

Similar Threads

  1. Replies: 4
    Last Post: 01-20-2011, 04:19 PM
  2. extract contents for a search engine (text,urls)
    By nijil in forum New To Java
    Replies: 4
    Last Post: 02-28-2010, 10:30 PM
  3. Search text field and combo box
    By Allgorythm in forum New To Java
    Replies: 2
    Last Post: 02-12-2010, 05:15 AM
  4. Search in text file
    By mark-mlt in forum New To Java
    Replies: 6
    Last Post: 04-03-2009, 04:33 PM
  5. Search a text file
    By javanewbie1979 in forum New To Java
    Replies: 15
    Last Post: 02-09-2009, 04: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
  •