Is it possible to search a TextFile line by line to find a specific string.
ie. Hello
It searches line by line to find if any line has Hello in it. There will only be one string per line.
Is this possible?
If so how do I attempt this?
Is it possible to search a TextFile line by line to find a specific
Share
It’s certainly easiest to load the entire file into memory. Provided that your file is small enough then you can do it like this:
I assume that when you say
that you are looking for lines that contain the search string rather than lines that equal the search string.
In a comment you ask:
That’s an easy enough variation of the above:
This function deletes all lines that contain the search string. If you only want to delete the first such line, then break out of the for loop after the call to Delete.
The loop variable is descending to allow the loop to modify the list.
If you want to use a different test, say equality rather than contains, then simply modify the if statement.