• Post Reply Bookmark Topic Watch Topic
  • New Topic

parsing out strings

 
Keith Droogie
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to parse out strings from text files. Specifically I want to be able to search for the specific string "Query" in an HTML source file. This string occurs many times in the source file and will serve as a delimiter. I want to dump all the characters that occur in between each "Query" string into individual arrays or vectors. Once i have the blocks of text in arrays or vectors i can parse out other strings etc etc.
Im using FileReader wrapped with BufferedReader to read lines at a time but Im not sure if this is efficient. However how can i search for strings if im reading char at a time. I am new to Java so any help would be appreciated. Thanks!
Keith
 
Dric Panet-Raymond
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A quick and dirty method to search for text inside a String is to use the <stringVariable>.indexOf( "<search word>" ) method.
This provides you with an index into the string which represents the first occurance of your seach word in that string.
But this wouldn't be the best solution to your problem ( although you could use it in a really kludgy way by using nested .indexOf() queries where the index returned + search word length would be used as the starting point of the next search. )
something like

That code snippet will parse out the various Query strings and print them. As I mentioned that is a hack way of doing it.
You could also use the StringTokenizer method, but this will not allow you to use the "Query" string as a delimiter ( it will take that as meaning you want the characters, q, u, e, r, and y to be the delimiters ). So it looks like your stuck with something similar to the above method.
I'd suggest putting it into something like a getQueryData() method, that would return a Vector of the Query elements.


Please forgive the ugly formatting of that code, I copied and pasted it from the message window here . Now you have a Vector containing all your Query data.
Good luck.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!