Visual Basic .NET » Windows Registry and File I/O
Simple Text File Problem - VB.NET -- kronso23 --


I'm new to VB.NET and I've depleted all my sources to look for help,
books, msdn, google... and can't seem to find a simple answer for a simple problem.

I need to search a text file for every instance of a string that a user provides, and I need to print those strings to a textbox, I would prefer a solution that doesn't require regular expressions.

-- BradleyPeter --

Firstly, if you search a file for every instance of the word "Peter", then it will return "PeterPeterPeter...". Do you mean that you want to simulate a grep and return every line that contains a given string?

Lastly, why don't you want to use a regex? That's what they're for.
Peter
-----Original Message-----
From: DotNetDevelopment on behalf of kronso.23 Sent: Mon 7/17/2006 7:13 PM To: DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting Cc:
Subject:
Simple Text File Problem - VB.NET
I'm new to VB.NET and I've depleted all my sources to look for help,
books, msdn, google... and can't seem to find a simple answer for a simple problem.

I need to search a text file for every instance of a string that a user provides, and I need to print those strings to a textbox, I would prefer a solution that doesn't require regular expressions.



-- Cerebrus --


I can understand your reluctance to use Regex. Regex is such a versatile technology, that initially you can be daunted by it. But believe me, once you get the hang of it, you'll realize what you've been missing.

In any case, here are the steps involved in what you want to do :

1. Create Filestream object indicating file you want to read.
2. Create StreamReader object to read the file. Associate with the Filestream.
3. Use the "StreamReader.ReadToEnd" method to read the entire contents of the file, until the end, into a string variable.
4. Now you can search this string using the InStr() method or String.IndexOf() method, providing your search text. Each time it is found, you can print it to the textbox.

See this article for a good introduction to file handling in VB.NET.
<
http://www.builderau.com.au/program/windows/0,39024644,20267367,00.htm 
>

HTH

-- kronso23 --


I guess, the book's "logic" behind the purpose of doing this is to determine if a text file contains a certain word, I'm going by what the book is telling me
Bradley, Peter wrote:
Firstly, if you search a file for every instance of the word "Peter", then it will return "PeterPeterPeter...". Do you mean that you want to simulate a grep and return every line that contains a given string?

Lastly, why don't you want to use a regex? That's what they're for.


Peter

 

-- kronso23 --


Thank you cerebrus... It's not really My preference to not use regular expressions, but the class that I am taking. I have dabbled in reg expressions with perl, and I do agree the power that they offer is incredible from what I've seen. Thanks for the feedback guys.

-- Cerebrus --


Cool, then. I hope it worked for you. :-)

-- BradleyPeter --


Then read the text file into a string and call the IndexOf(string searchStr) method on that string. It will return -1 if searchStr is not found. If it returns any other value, the string and therefore the file contains the word you're looking for.
Peter

-----Original Message-----
From: DotNetDevelopment
Re: Simple Text File Problem - VB.NET
I guess, the book's "logic" behind the purpose of doing this is to determine if a text file contains a certain word, I'm going by what the book is telling me
Bradley, Peter wrote:
Firstly, if you search a file for every instance of the word "Peter",
then it will return "PeterPeterPeter...". Do you mean that you want to simulate a grep and return every line that contains a given string? 


Lastly, why don't you want to use a regex? That's what they're for.


Peter


 

[Submit Comment]Home