-
VB.NET Search Text
Hey Folks,
I don't do much programming so excuse the newbie question[s]...
I have a csv file that contains our DHCP reservations. I am looking for a good way to search for a particular string, and display the line of text that contains it.
For example if I search for 10.1.2.3 in:
c:\dhcp\dhcpres.csv
10.1.2.0, 112233445566, pc-1,
10.1.2.1, 223344556677, pc-2,
10.1.2.2, 334455667788, pc-3,
10.1.2.3, 445566778899, pc-4,
I want the system to display:
10.1.2.3, 445566778899, pc-4,
in whatever field specified. Is there an easy way to do this in VB.NET?
Thanks for any suggestions.
-
First you're going to want to use string.Split(Char(13)) or something to that effect to split your incoming string into an array of strings, one for each line. Then you're going to use string.Contains("10.1.2.3") which returns a bool that tells you whether the string contains that sub string.
I personally recommend C# over VB, they both use the same libraries and object structures for the most part, however, in general, C# coders will get a somewhat higher pay for similar jobs. It all comes down to the perceived 'purpose' of the language even though they both can do the same exact thing. One will assume a C# coder has more experience, as C# is more related to the C/C++, which is used by most experts on large scale projects, while VB is often the first language a programmer learns, and is more related to simpler, business applications. But this all of course is just my opinion. ;)
-
Since you said you don't do much programming, i'll expand on mets post.
here is probably close to what you want.
Code:
string ReservationFinder(string comp)
{
TextReader tr = new StreamReader("c:\file.csv");
string input = null;
while ((input = tr.ReadLine()) != null)
{
if(string.Contains(comp))
return string;
}
}
after that, you would do the Split() on the string to break each of the comma separated values into their own.
Edit: oops, that was c#. here is the VB equivalent (May not be 100% right. havent done vb in ages)
Code:
Private Function ReservationFinder(comp As String) As String
Dim tr As TextReader = New StreamReader("c:\\file.csv")
Dim input As String = Nothing
Do While tr.Peek() <> -1
If String.Contains(comp) Then
Return String
End If
End While
End Function
-
You guys are great! I will try this when I get home.
I wouldn't mind learning C#, but I really don't code that much. Most of what I do is scripting... I think the last time I opened visual studio before I started this project was close to a year ago.
Thanks a bunch for the suggestions and examples!
-
VBScript with MS Office supports calling Excel functionality for searching and managing csv files.
I used that for a very similar script that managed MFDs across a network.
If I can find it, I'll post it so you can see the inner workings.
-
Sounds interesting. Once again, all of the help is most appreciated.