How can I search through a text file for a string and then retrieve the next line of code?
example
I am searching for hello
the text file looks like
hello, Hi how are you
bye, see you later
The program whould return hello because it is the next phrase after hello
thanks
searching through a text file
- qbasicfreak
- Veteran
- Posts: 90
- Joined: Wed Oct 22, 2008 3:27 pm
- Location: canada
- burger2227
- Veteran
- Posts: 2466
- Joined: Mon Aug 21, 2006 12:40 am
- Location: Pittsburgh, PA
HUH? I guess you want hello included.
Since the file is all text, you can use LINE INPUT #1, textline$ to get each line of text in a loop. Watch out for EOF!
Then you can check for "Hello" using start = INSTR( textline$, "Hello"). INSTR returns the first position of the word in the line. Then you can stop reading the file when it returns anything greater than 0. Zero designates a word was not found in that text line.
If you only want the text from the word to the end, just use that position in : MID$ (textline, start, LEN(textline$) - (start - 1))
Ted
Since the file is all text, you can use LINE INPUT #1, textline$ to get each line of text in a loop. Watch out for EOF!
Then you can check for "Hello" using start = INSTR( textline$, "Hello"). INSTR returns the first position of the word in the line. Then you can stop reading the file when it returns anything greater than 0. Zero designates a word was not found in that text line.
If you only want the text from the word to the end, just use that position in : MID$ (textline, start, LEN(textline$) - (start - 1))
Ted
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
- qbasicfreak
- Veteran
- Posts: 90
- Joined: Wed Oct 22, 2008 3:27 pm
- Location: canada
thanks
thanks Ted
(Hello was just an example)
(Hello was just an example)
- qbasicfreak
- Veteran
- Posts: 90
- Joined: Wed Oct 22, 2008 3:27 pm
- Location: canada
- burger2227
- Veteran
- Posts: 2466
- Joined: Mon Aug 21, 2006 12:40 am
- Location: Pittsburgh, PA
YES, naturally you can substitute a variable.
INSTR also allows you to search from a starting point:
start% = INSTR(begin%, teststring$, word$)
You can omit it if you begin at the first letter of text.
This allows you to do multiple searches for more than one repetition in a text line. Just use begin% = start% + 1 in a loop to look for more instances of a word or phrase (assuming you already found one: start% > 0).
Tell Linus I said hi, lol
Ted
INSTR also allows you to search from a starting point:
start% = INSTR(begin%, teststring$, word$)
You can omit it if you begin at the first letter of text.
This allows you to do multiple searches for more than one repetition in a text line. Just use begin% = start% + 1 in a loop to look for more instances of a word or phrase (assuming you already found one: start% > 0).
Tell Linus I said hi, lol
Ted
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0