Results 1 to 5 of 5
Thread: search text
- 08-18-2011, 10:14 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 25
- Rep Power
- 0
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)); } }
- 08-18-2011, 10:44 AM #2
Member
- Join Date
- Oct 2010
- Posts
- 25
- Rep Power
- 0
.gif)
- 08-18-2011, 12:21 PM #3
Hi. You can use Reg Exp for this aim. you can find more information on official website Oracle.
Skype: petrarsentev
http://TrackStudio.com
- 08-18-2011, 12:24 PM #4
Member
- Join Date
- Aug 2011
- Posts
- 11
- Rep Power
- 0
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.
- 08-18-2011, 12:34 PM #5
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
-
How to Search, Compare, and Replace text in the TextBox?
By SHENGTON in forum CLDC and MIDPReplies: 4Last Post: 01-20-2011, 04:19 PM -
extract contents for a search engine (text,urls)
By nijil in forum New To JavaReplies: 4Last Post: 02-28-2010, 10:30 PM -
Search text field and combo box
By Allgorythm in forum New To JavaReplies: 2Last Post: 02-12-2010, 05:15 AM -
Search in text file
By mark-mlt in forum New To JavaReplies: 6Last Post: 04-03-2009, 04:33 PM -
Search a text file
By javanewbie1979 in forum New To JavaReplies: 15Last Post: 02-09-2009, 04:46 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks